File size: 1,953 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
import { Page } from 'playwright';

export type DashboardTabs = 'At a Glance' | 'My Plan';
export type SettingsTabs =
	| 'Security'
	| 'Performance'
	| 'Writing'
	| 'Sharing'
	| 'Discussion'
	| 'Traffic'
	| 'Newsletter'
	| 'Monetize';
// Discriminated union type.
type JetpackTabs =
	| { view: 'Dashboard'; tab: DashboardTabs }
	| { view: 'Settings'; tab: SettingsTabs };

/**
 * Represents the Jetpack pages in WP-Admin.
 */
export class JetpackDashboardPage {
	private page: Page;

	/**
	 * Constructs an instance of the page.
	 *
	 * @param {Page} page Instance of the Page object.
	 */
	constructor( page: Page ) {
		this.page = page;
	}

	/**
	 * Navigates to the Jetpack dashboard landing page for a site.
	 *
	 * Note that this method will not work for non-AT sites.
	 *
	 * @param {string} siteSlug Site slug.
	 */
	async visit( siteSlug: string ) {
		await this.page.goto( `https://${ siteSlug }/wp-admin/admin.php?page=jetpack#/dashboard`, {
			timeout: 15 * 1000,
		} );
	}

	/**
	 * Given a discriminated union type parameter `param`, first clicks on the specified view,
	 * then clicks on the specified tab.
	 *
	 * @param {JetpackTabs} param View and tab to click on.
	 */
	async clickTab( param: JetpackTabs ) {
		// Switch to the correct view (Dashboard/Settings) if required.
		await this.page
			.getByRole( 'main' )
			.getByRole( 'link', { name: param.view, exact: true } )
			.click();
		await this.page.waitForURL( new RegExp( `page=jetpack#/${ param.view }`, 'i' ) );

		// Click on the tab.
		await this.page
			.getByRole( 'main' )
			.getByRole( 'menuitem', { name: param.tab, exact: true } )
			.click();

		// Filter the nav tabs to elements that have `.is-selected` (should be only one),
		// and verify the resulting element is the tab that was clicked on earlier.
		await this.page
			.getByRole( 'main' )
			.filter( { has: this.page.locator( '.is-selected' ) } )
			.filter( { hasText: param.tab } )
			.waitFor();
	}
}