File size: 2,293 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { Page } from 'playwright';

type NoticeType = 'Success' | 'Error' | 'Info';

const parent = 'div.calypso-notice';

const selectors = {
	notificationToast: ( text: string ) => `${ parent }.is-dismissable:has-text("${ text }")`,
};

/**
 * Represents the Notification component.
 *
 * There are two types of notifications in Calypso:
 * 	- dismissable
 * 	- permanent (banner)
 *
 * Dismissable type appear to float on top of the main DOM,
 * typically conveying fleeting information. Examples include
 * saving changes in the Settings page or deletion of a post.
 *
 * Permanent/banner type appears on pages conveying messages that
 * contain some permanent element. Examples include notification if
 * user is on a legacy WordPress.com plan.
 */
export class NoticeComponent {
	private page: Page;

	/**
	 * Creates an instance of the component.
	 *
	 * @param {Page} page Object representing the base page.
	 */
	constructor( page: Page ) {
		this.page = page;
	}

	/**
	 * Verifies the content in a notification on the page.
	 *
	 * This method requires either full or partial text of
	 * the notification to be supplied as parameter.
	 *
	 * Optionally, it is possible to specify the `type` parameter to limit
	 * validation to a certain type of notifications eg. `error`.
	 *
	 * @param {string} text Full or partial text to validate on page.
	 * @param param1 Optional parameters.
	 * @param {NoticeType} param1.type Type of notice to limit validation to.
	 * @param {number} param1.timeout Custom timeout value.
	 */
	async noticeShown(
		text: string,
		{ type, timeout }: { type?: NoticeType; timeout?: number } = {}
	): Promise< void > {
		const noticeType = type ? `.is-${ type?.toLowerCase() }` : '';

		const selector = `div.calypso-notice${ noticeType } :text("${ text }")`;

		const locator = this.page.locator( selector );
		await locator.waitFor( { state: 'visible', timeout: timeout } );
	}

	/**
	 * Locate and dismiss a notification of the dismisable type.
	 *
	 * @param {string} text Banner text to locate.
	 */
	async dismiss( text: string ): Promise< void > {
		const bannerLocator = this.page.locator( selectors.notificationToast( text ) );

		const dismissLocator = bannerLocator.locator( 'button[aria-label="Dismiss"]' );
		await dismissLocator.click();
	}
}