File size: 2,309 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
77
78
79
80
81
82
83
84
import { Page } from 'playwright';
import envVariables from '../../env-variables';

const selectors = {
	sidebarToggle: ( page: Page ) => page.getByRole( 'menuitem', { name: 'Menu' } ),
	sidebar: '#adminmenu',
};

/**
 * Component representing the sidebar in WP Admin.
 *
 */
export class WPAdminSidebarComponent {
	private page: Page;

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

	/**
	 * Waits for the WordPress.com Calypso sidebar to be ready on the page.
	 */
	async waitForSidebarInitialization(): Promise< void > {
		const sidebarOrSidebarToggle =
			envVariables.VIEWPORT_NAME === 'mobile'
				? selectors.sidebarToggle( this.page )
				: this.page.locator( selectors.sidebar );

		await Promise.all( [
			this.page.waitForLoadState( 'load', { timeout: 20 * 1000 } ),
			sidebarOrSidebarToggle.waitFor( { timeout: 20 * 1000 } ),
		] );
	}

	/* Main sidebar action */

	/**
	 * Navigates to given (sub)item of the sidebar menu.
	 *
	 * @param {string} item Plaintext representation of the top level heading.
	 * @param {string} subitem Plaintext representation of the child level heading.
	 * @returns {Promise<void>} No return value.
	 */
	async navigate( item: string, subitem?: string ): Promise< void > {
		await this.waitForSidebarInitialization();

		if ( envVariables.VIEWPORT_NAME === 'mobile' ) {
			await this.openMobileSidebar();
		}

		const menuItem = this.page.locator( selectors.sidebar ).getByRole( 'link', { name: item } );

		if ( subitem ) {
			if ( envVariables.VIEWPORT_NAME === 'mobile' ) {
				await menuItem.click();
			} else {
				await menuItem.hover();
			}

			await this.page.locator( selectors.sidebar ).getByRole( 'link', { name: subitem } ).click();
		} else {
			await menuItem.click();
		}
	}

	/**
	 * Opens the mobile variant of the sidebar into view.
	 *
	 * For mobile sized viewports, the sidebar is by default hidden off screen.
	 * In order to interact with the sidebar, the hamburger menu button on top left must first
	 * be clicked to bring the mobile sidebar into view.
	 */
	private async openMobileSidebar(): Promise< void > {
		await selectors.sidebarToggle( this.page ).click();

		await this.page.waitForSelector( selectors.sidebar );
	}
}