File size: 7,922 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { Page } from 'playwright';
import { getCalypsoURL } from '../../data-helper';
import envVariables from '../../env-variables';
import { NavbarComponent } from './navbar-component';

type FocusType = 'Sites' | 'Sidebar';

const selectors = {
	sidebar: '.sidebar',
	sidebarNoticeButton: ( name: string ) =>
		`.sidebar .current-site__notices button:text("${ name }")`,
	collapsedSidebar: '.is-sidebar-collapsed',
	focusedLayout: ( focus: FocusType ) => `.layout.focus-${ focus.toLowerCase() }`,

	// Buttons and links within Sidebar
	linkWithText: ( text: string ) => `a:has-text("${ text }")`,
	planName: ':text-is("Upgrades"):visible .sidebar__inline-text',
};

/**
 * Component representing the sidebar on the dashboard of WPCOM.
 *
 */
export class SidebarComponent {
	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 sidebarLocator = this.page.locator( selectors.sidebar );

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

		// If the sidebar is collapsed (via the Collapse Menu toggle),
		// re-expand the sidebar.
		if ( await this.sidebarIsCollapsed() ) {
			const sidebarCollapseToggle = this.page.locator( selectors.linkWithText( 'Collapse menu' ) );
			// Wait until the collapsed sidebar CSS is detached from DOM, ie. it is no longer collapsed.
			await Promise.all( [
				this.page.waitForSelector( selectors.collapsedSidebar, { state: 'detached' } ),
				sidebarCollapseToggle.dispatchEvent( 'click' ),
			] );
		}
	}

	/* 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();
		}

		// Top level menu item selector.
		const itemSelector = `${ selectors.sidebar } :text-is("${ item }"):visible`;
		await this.page.dispatchEvent( itemSelector, 'click' );

		// Sub-level menu item selector.
		if ( subitem ) {
			const subitemSelector = `.is-toggle-open :text-is("${ subitem }"):visible, .wp-menu-open .wp-submenu :text-is("${ subitem }"):visible`;
			await Promise.all( [
				this.page.waitForNavigation( { timeout: 30 * 1000 } ),
				this.page.dispatchEvent( subitemSelector, 'click' ),
			] );
		}

		const currentURL = this.page.url();
		// Do not verify selected menu items or retry if navigation takes user out of Calypso (eg. WP-Admin, Widgets editor)...
		if ( ! currentURL.startsWith( getCalypsoURL() ) ) {
			return;
		}
		// ... or to a page in Calypso that closes the sidebar.
		if ( currentURL.match( /\/(post|page|site-editor)\// ) ) {
			return;
		}

		// Some menu items (eg. Comments, Stats) do not have a submenu. In these cases,
		// the `.selected` class is applied to the top level menu.
		let selectedMenuItem = `${ selectors.sidebar } .selected :text-is("${ item }")`;

		if ( subitem ) {
			selectedMenuItem = `${ selectors.sidebar } .selected :text-is("${ subitem }")`;
		}

		// Verify the expected item or subitem is selected.
		const locator = this.page.locator( selectedMenuItem );
		await locator.waitFor( { state: 'attached' } );
	}

	/**
	 * Open a notice of the sidebar menu.
	 *
	 * @param {string} noticeButtonName Name of the notice button where the click will be performed.
	 * @param {string} expectedUrl Expected URL after clicking on the notice.
	 * @returns {Promise<void>} No return value.
	 */
	async openNotice( noticeButtonName: string, expectedUrl?: string ): Promise< void > {
		await this.waitForSidebarInitialization();

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

		// Top level menu item selector.
		const itemSelector = selectors.sidebarNoticeButton( noticeButtonName );
		await this.page.dispatchEvent( itemSelector, 'click' );

		const currentURL = this.page.url();
		// Do not verify selected menu items or retry if navigation takes user out of Calypso (eg. WP-Admin, Widgets editor)...
		if ( ! currentURL.startsWith( getCalypsoURL() ) ) {
			return;
		}
		// ... or to a page in Calypso that closes the sidebar.
		if ( currentURL.match( /\/(post|page|site-editor)\// ) ) {
			return;
		}

		if ( expectedUrl ) {
			await this.page.waitForURL( expectedUrl );
		}
	}

	/* Miscellaneous actions on sidebar */

	/**
	 * Clicks on the switch site menu item in the sidebar.
	 *
	 * @returns {Promise<void>} No return value.
	 */
	async switchSite(): Promise< void > {
		await this.waitForSidebarInitialization();

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

		await this.page.click( selectors.linkWithText( 'Switch Site' ) );
		await this.page.waitForSelector( selectors.focusedLayout( 'Sites' ) );
	}

	/**
	 * Clicks on the Add Site button at bottom of the site selector within the sidebar.
	 *
	 * @returns {Promise<void>} No return value.
	 */
	async addSite(): Promise< void > {
		await Promise.all( [
			this.page.waitForNavigation(),
			this.page.click( selectors.linkWithText( 'Add New Site' ) ),
		] );
	}

	/**
	 * Returns the current plan name as shown on the sidebar.
	 *
	 * Note, the plan name shown in the sidebar may not always be
	 * accurate due to race conditions. Consider this method as
	 * secondary validation.
	 *
	 * Additionally, on mobile viewports if the current page is
	 * Upgrades > Plans the plan name is not visible in the sidebar.
	 * This method should be used after navigating away from the
	 * Plans page.
	 *
	 * @returns {Promise<string>} Name of the plan.
	 * @throws {Error} If the viewport is mobile and the currently displayed page is Upgrades > Plans.
	 */
	async getCurrentPlanName(): Promise< string > {
		await this.waitForSidebarInitialization();

		if ( envVariables.VIEWPORT_NAME === 'mobile' ) {
			if ( this.page.url().includes( getCalypsoURL( 'plans' ) ) ) {
				throw new Error(
					'Unable to retrieve current plan name on mobile sidebar.\nNavigate away from Upgrades > Plans page and try again.'
				);
			}

			await this.openMobileSidebar();
		}

		const planNameLocator = this.page.locator( selectors.planName );
		return await planNameLocator.innerText();
	}

	/* Viewport-specific methods */

	/**
	 * Determines whether the sidebar is in a collapsed state for desktop viewports.
	 *
	 * This check is part of the self-healing process for the sidebar-related end-to-end
	 * tests, as rest of the Sidebar methods rely on the sidebar in a non-collapsed
	 * state.
	 */
	private async sidebarIsCollapsed(): Promise< boolean > {
		const collapsedSidebarLocator = this.page.locator( selectors.collapsedSidebar );
		return ( await collapsedSidebarLocator.count() ) > 0;
	}

	/**
	 * 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, My Sites button on top left must first
	 * be clicked to bring the mobile sidebar into view.
	 */
	private async openMobileSidebar(): Promise< void > {
		await this.waitForSidebarInitialization();

		const navbarComponent = new NavbarComponent( this.page );
		await navbarComponent.clickMobileMenu();

		// `focus-sidebar` attribute is added to the main layout screen.
		const layoutElement = await this.page.waitForSelector( selectors.focusedLayout( 'Sidebar' ) );
		await layoutElement.waitForElementState( 'stable' );
	}
}