File size: 6,994 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { Locator, Page } from 'playwright';

export type ResultsCategory = 'Docs' | 'Links';

declare const configData: Record< string, unknown >;

/**
 * Represents the Help Center popover.
 */
export class HelpCenterComponent {
	private page: Page;
	private popup: Locator;
	private isWpAdmin: boolean;

	/**
	 * Constructs an instance of the component.
	 *
	 * @param {Page} page The underlying page.
	 */
	constructor( page: Page ) {
		this.page = page;
		this.popup = this.page.locator( '.help-center__container' );
		this.isWpAdmin = page.url().includes( 'wp-admin' );
	}

	/**
	 * Get the help center container locator.
	 *
	 * @returns {Locator} The help center container locator.
	 */
	getHelpCenterLocator(): Locator {
		return this.popup;
	}

	/**
	 * Check if the help center is loaded.
	 */
	async isVisible(): Promise< boolean > {
		return Boolean( await this.popup.count() );
	}

	/**
	 * Opens the support popover from the closed state.
	 *
	 * @returns {Promise<void>}
	 */
	async openPopover(): Promise< void > {
		// Return if its already open.
		if ( await this.isVisible() ) {
			return;
		}

		if ( this.isWpAdmin ) {
			await this.page.locator( '#wp-admin-bar-help-center' ).click();
		} else {
			await this.page.getByRole( 'button', { name: 'Help', exact: true } ).click();
		}

		await this.popup.locator( '.placeholder-lines__help-center' ).waitFor( { state: 'detached' } );
		await this.popup.waitFor( { state: 'visible' } );
	}

	/**
	 * Closes the support popover from the open state.
	 *
	 * @returns {Promise<void>}
	 */
	async closePopover(): Promise< void > {
		// Return if its already closed.
		if ( ! ( await this.isVisible() ) ) {
			return;
		}

		const closeButton = await this.popup.getByRole( 'button', {
			name: 'Close Help Center',
			exact: true,
		} );
		await closeButton.click();
		await this.popup.waitFor( { state: 'detached' } );
	}

	/**
	 * Minimizes the support popover from the open state.
	 *
	 * @returns {Promise<void>}
	 */
	async minimizePopover(): Promise< void > {
		const minimizeButton = await this.popup.getByRole( 'button', {
			name: 'Minimize Help Center',
			exact: true,
		} );

		await minimizeButton.click();
		await this.popup.locator( '.help-center__container-content' ).waitFor( { state: 'hidden' } );
	}

	/**
	 * Maximizes the support popover.
	 *
	 * @returns {Promise<void>}
	 */
	async maximizePopover(): Promise< void > {
		const maximizeButton = await this.popup.getByRole( 'button', {
			name: 'Maximize Help Center',
			exact: true,
		} );

		await maximizeButton.click();
		await this.popup.locator( '.help-center__container-content' ).waitFor( { state: 'visible' } );
	}

	/**
	 * Go back to the previous page.
	 */
	async goBack(): Promise< void > {
		await this.popup.getByTestId( 'help-center-back-button' ).click();
	}

	/**
	 * Check the presence of the Help Center popover.
	 *
	 * @returns {boolean} Whether the popover is shown.
	 */
	async isPopoverShown(): Promise< boolean > {
		const isVisible = await this.isVisible();
		const popupBoundingBox = await this.popup.boundingBox();
		const viewport = await this.page.viewportSize();

		const isShowing =
			isVisible &&
			popupBoundingBox &&
			viewport &&
			popupBoundingBox?.x >= 0 &&
			popupBoundingBox?.y >= 0 &&
			popupBoundingBox?.x + popupBoundingBox?.width <= viewport?.width &&
			popupBoundingBox?.y + popupBoundingBox?.height <= viewport?.height;

		return Boolean( isShowing );
	}

	/**
	 * Get the articles locator.
	 *
	 * @returns {Locator} The articles locator.
	 */
	getArticles(): Locator {
		return this.popup
			.getByRole( 'list', { name: 'Recommended Resources' } )
			.getByRole( 'listitem' );
	}

	/**
	 * Get Odie chat
	 *
	 * @returns {Locator} The Odie chat locator.
	 */
	getOdieChat(): Locator {
		return this.popup.locator( '#odie-messages-container' );
	}

	/**
	 * Fills the support popover search input and waits for the query to complete.
	 *
	 * @param {string} query Search keyword to be entered into the search field.
	 */
	async search( query: string ): Promise< void > {
		await Promise.all( [
			this.page.waitForResponse(
				( response ) => {
					return (
						response.request().url().includes( '/help/search/wpcom' ) &&
						response
							.request()
							.url()
							.includes( `query=${ encodeURIComponent( query ) }` )
					);
				},
				{ timeout: 15 * 1000 }
			),
			this.popup.getByPlaceholder( 'Search for help' ).fill( query ),
		] );

		await this.popup.locator( '.placeholder-lines__help-center' ).waitFor( { state: 'detached' } );
	}

	/**
	 * Start the AI chat.
	 *
	 * @param {string} query Search keyword to be entered into the message field.
	 */
	async startAIChat( query: string ): Promise< void > {
		const sendMessageForm = this.popup.locator( '.odie-send-message-input-container' );

		await Promise.all( [
			this.page.waitForResponse(
				( response ) =>
					response.url().includes( '/odie/chat/wpcom-support-chat' ) && response.status() === 200
			),
			sendMessageForm
				.locator( 'textarea' )
				.fill( query )
				// Programmatically trigger the form submission to avoid issues with the cookie banner.
				.then( () => sendMessageForm.dispatchEvent( 'submit' ) ),
		] );
	}

	/**
	 * Set Zendesk to staging environment.
	 *
	 * @returns {Promise<void>}
	 */
	async setZendeskStaging(): Promise< void > {
		// Rewrite the authentication request to avoid calling Zendesk API in test environment.
		await this.page.route( '**/authenticate/chat?*', ( route, request ) => {
			const url = new URL( request.url() );

			if ( url.searchParams.get( 'type' ) === 'zendesk' ) {
				url.searchParams.set( 'test_mode', 'true' );
			}

			route.continue( { url: url.toString() } );
		} );

		await this.page.evaluate(
			( _constants ) => {
				if ( typeof configData !== 'undefined' ) {
					configData.zendesk_support_chat_key = _constants.ZENDESK_STAGING_SUPPORT_CHAT_KEY;
				}
			},
			{
				// This should be imported from `@automattic/zendesk-client` but the import has problems with the `@automattic/calypso-config` dependency.
				ZENDESK_STAGING_SUPPORT_CHAT_KEY: '715f17a8-4a28-4a7f-8447-0ef8f06c70d7',
			}
		);
	}

	/**
	 * Set Odie to Test mode.
	 *
	 * @returns {Promise<void>}
	 */
	async setOdieTestMode(): Promise< void > {
		// Rewrite the Odie POST request to make sure it's in test mode.
		await this.page.route( '**/odie/chat/*', async ( route, request ) => {
			const postBody = JSON.parse( request.postData() || '{}' );

			// Add Test Mode to the request.
			postBody.test = true;

			// Continue with the modified post data
			await route.continue( {
				postData: JSON.stringify( postBody ),
				headers: {
					...request.headers(),
					'Content-Type': 'application/json',
				},
			} );
		} );
	}

	/**
	 * Get the Contact Support button.
	 *
	 * @returns {Locator} The Contact Support button locator.
	 */
	getContactSupportButton(): Locator {
		return this.popup.getByRole( 'button', { name: 'Contact WordPress.com Support' } ).last();
	}
}