File size: 11,048 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
 * @group editor-tracking
 */

import {
	DataHelper,
	envVariables,
	getTestAccountByFeature,
	envToFeatureKey,
	TestAccount,
	EditorTracksEventManager,
	FullSiteEditorPage,
} from '@automattic/calypso-e2e';
import { Browser, Page } from 'playwright';
import { skipDescribeIf } from '../../jest-helpers';

declare const browser: Browser;

describe( DataHelper.createSuiteTitle( 'Editor tracking: Global styles events' ), function () {
	const features = envToFeatureKey( envVariables );
	const accountName = getTestAccountByFeature( { ...features, variant: 'siteEditor' } );

	describe( 'wpcom_block_editor_global_styles_panel_toggle', function () {
		let page: Page;
		let testAccount: TestAccount;
		let fullSiteEditorPage: FullSiteEditorPage;
		let editorTracksEventManager: EditorTracksEventManager;

		beforeAll( async () => {
			page = await browser.newPage();

			testAccount = new TestAccount( accountName );
			await testAccount.authenticate( page );

			editorTracksEventManager = new EditorTracksEventManager( page );
			fullSiteEditorPage = new FullSiteEditorPage( page );
		} );

		it( 'Visit the site editor', async function () {
			await fullSiteEditorPage.visit( testAccount.getSiteURL( { protocol: true } ) );
			await fullSiteEditorPage.prepareForInteraction( { leaveWithoutSaving: true } );
		} );

		it( 'Close the navigation sidebar', async function () {
			await fullSiteEditorPage.closeNavSidebar();
		} );

		it( 'Open site styles', async function () {
			await fullSiteEditorPage.openSiteStyles();
		} );

		it( '"wpcom_block_editor_global_styles_panel_toggle" event fires with "open" === true', async function () {
			const eventDidFire = await editorTracksEventManager.didEventFire(
				'wpcom_block_editor_global_styles_panel_toggle',
				{
					matchingProperties: {
						open: true,
					},
				}
			);
			expect( eventDidFire ).toBe( true );
		} );

		it( 'Close site styles', async function () {
			await fullSiteEditorPage.closeSiteStyles();
		} );

		it( '"wpcom_block_editor_global_styles_panel_toggle" event fires with "open" === false', async function () {
			const eventDidFire = await editorTracksEventManager.didEventFire(
				'wpcom_block_editor_global_styles_panel_toggle',
				{
					matchingProperties: {
						open: false,
					},
				}
			);
			expect( eventDidFire ).toBe( true );
		} );

		it( 'Close the page', async function () {
			await page.close();
		} );
	} );

	describe( 'wpcom_block_editor_global_styles_menu_selected', function () {
		let page: Page;
		let testAccount: TestAccount;
		let fullSiteEditorPage: FullSiteEditorPage;
		let editorTracksEventManager: EditorTracksEventManager;

		beforeAll( async () => {
			page = await browser.newPage();

			testAccount = new TestAccount( accountName );
			await testAccount.authenticate( page );

			editorTracksEventManager = new EditorTracksEventManager( page );
			fullSiteEditorPage = new FullSiteEditorPage( page );
		} );

		it( 'Visit the site editor', async function () {
			await fullSiteEditorPage.visit( testAccount.getSiteURL( { protocol: true } ) );
			await fullSiteEditorPage.prepareForInteraction( { leaveWithoutSaving: true } );
		} );

		it( 'Close the navigation sidebar', async function () {
			await fullSiteEditorPage.closeNavSidebar();
		} );

		it( 'Open site styles', async function () {
			await fullSiteEditorPage.openSiteStyles();
		} );

		it( 'Click on "Typography" menu button', async function () {
			await fullSiteEditorPage.clickStylesMenuButton( 'Typography' );
		} );

		it( '"wpcom_block_editor_global_styles_menu_selected" event fires with "menu" === "typography"', async function () {
			const eventDidFire = await editorTracksEventManager.didEventFire(
				'wpcom_block_editor_global_styles_menu_selected',
				{
					matchingProperties: {
						menu: 'typography',
					},
				}
			);
			expect( eventDidFire ).toBe( true );
		} );

		it( 'Return to top menu level', async function () {
			await fullSiteEditorPage.returnToStylesTopMenu();
		} );

		it( 'Click on "Blocks" menu button', async function () {
			await fullSiteEditorPage.clickStylesMenuButton( 'Blocks' );
		} );

		it( '"wpcom_block_editor_global_styles_menu_selected" event fires with "menu" === "blocks"', async function () {
			const eventDidFire = await editorTracksEventManager.didEventFire(
				'wpcom_block_editor_global_styles_menu_selected',
				{
					matchingProperties: {
						menu: 'blocks',
					},
				}
			);
			expect( eventDidFire ).toBe( true );
		} );

		it( 'Close the page', async function () {
			await page.close();
		} );
	} );

	describe( 'wpcom_block_editor_global_styles_update', function () {
		let page: Page;
		let testAccount: TestAccount;
		let fullSiteEditorPage: FullSiteEditorPage;
		let editorTracksEventManager: EditorTracksEventManager;

		beforeAll( async () => {
			page = await browser.newPage();

			testAccount = new TestAccount( accountName );
			await testAccount.authenticate( page );

			editorTracksEventManager = new EditorTracksEventManager( page );
			fullSiteEditorPage = new FullSiteEditorPage( page );
		} );

		it( 'Visit the site editor', async function () {
			await fullSiteEditorPage.visit( testAccount.getSiteURL( { protocol: true } ) );
			await fullSiteEditorPage.prepareForInteraction( { leaveWithoutSaving: true } );
		} );

		it( 'Close the navigation sidebar', async function () {
			await fullSiteEditorPage.closeNavSidebar();
		} );

		it( 'Open site styles', async function () {
			await fullSiteEditorPage.openSiteStyles();
		} );

		describe( 'Updating styles directly', function () {
			it( 'Update the global background color', async function () {
				// We can always guarantee a target color event if we click a different one first.
				await fullSiteEditorPage.setGlobalColorStyle( 'Background', {
					colorName: 'Primary',
				} );
				await fullSiteEditorPage.setGlobalColorStyle( 'Background', {
					colorName: 'Background',
				} );
			} );

			it( '"wpcom_block_editor_global_styles_update" event fires with correct color properties', async function () {
				const eventDidFire = await editorTracksEventManager.didEventFire(
					'wpcom_block_editor_global_styles_update',
					{
						matchingProperties: {
							section: 'color',
							field: 'background',
							field_value: 'var:preset|color|background',
						},
						waitForEventMs: 2 * 1000, // Style update events are debounced
					}
				);
				expect( eventDidFire ).toBe( true );
			} );

			it( 'Update the font appearance for the Heading block', async function () {
				// We can always guarantee a target font appearance event if we select a different one first.
				await fullSiteEditorPage.setBlockTypographyStyle( 'Heading', {
					fontAppearance: 'Thin',
				} );
				await fullSiteEditorPage.setBlockTypographyStyle( 'Heading', {
					fontAppearance: 'Medium',
				} );
			} );

			it( '"wpcom_block_editor_global_styles_update" event fires with correct typography properties', async function () {
				const eventDidFire = await editorTracksEventManager.didEventFire(
					'wpcom_block_editor_global_styles_update',
					{
						matchingProperties: {
							block_type: 'core/heading',
							section: 'typography',
							field: 'fontWeight',
							field_value: '500',
						},
						waitForEventMs: 2 * 1000, // Style update events are debounced
					}
				);
				expect( eventDidFire ).toBe( true );
			} );
		} );

		// Can't reset to defaults on mobile
		skipDescribeIf( envVariables.VIEWPORT_NAME === 'mobile' )(
			'Updating by resetting to defaults',
			function () {
				it( 'Reset the Tracks events for a clean slate', async function () {
					await editorTracksEventManager.clearEvents();
				} );

				it( 'Reset styles to defaults for theme', async function () {
					await fullSiteEditorPage.resetStylesToDefaults();
				} );

				it( '"wpcom_block_editor_global_styles_update" event fires with "field_value" === "reset"', async function () {
					const eventDidFire = await editorTracksEventManager.didEventFire(
						'wpcom_block_editor_global_styles_update',
						{
							matchingProperties: {
								field_value: 'reset',
							},
							waitForEventMs: 2 * 1000, // Style update events are debounced
						}
					);
					expect( eventDidFire ).toBe( true );
				} );
			}
		);

		it( 'Close the page', async function () {
			await page.close();
		} );
	} );

	describe( 'wpcom_block_editor_global_styles_save', function () {
		let page: Page;
		let testAccount: TestAccount;
		let fullSiteEditorPage: FullSiteEditorPage;
		let editorTracksEventManager: EditorTracksEventManager;

		// We must make sure we have a new value to trigger the editor "dirty" state and enable saving.
		// The main way we do that is by resetting the layouts at the end of each run.
		// But, for the rare race condition we open the editor right as another test run is in the between state,
		// we add some randomness to our padding. This makes it extraordinarily rare to have an unsavable state!
		const padding = DataHelper.getRandomInteger( 1, 32 );

		beforeAll( async () => {
			page = await browser.newPage();

			testAccount = new TestAccount( accountName );
			await testAccount.authenticate( page );

			editorTracksEventManager = new EditorTracksEventManager( page );
			fullSiteEditorPage = new FullSiteEditorPage( page );
		} );

		afterAll( async function () {
			// Reset the layout back to empty to protect future runs.
			// You can reset an already empty layout, so this is safe to do even if saving didn't go through.
			await fullSiteEditorPage.openSiteStyles();
			await fullSiteEditorPage.resetGlobalLayoutStyle();
			await fullSiteEditorPage.closeSiteStyles();
			await fullSiteEditorPage.save();
		} );

		it( 'Visit the site editor', async function () {
			await fullSiteEditorPage.visit( testAccount.getSiteURL( { protocol: true } ) );
			await fullSiteEditorPage.prepareForInteraction( { leaveWithoutSaving: true } );
		} );

		it( 'Close the navigation sidebar', async function () {
			await fullSiteEditorPage.closeNavSidebar();
		} );

		it( 'Open site styles', async function () {
			await fullSiteEditorPage.openSiteStyles();
		} );

		it( 'Set global layout style', async function () {
			await fullSiteEditorPage.setGlobalLayoutStyle( { padding: padding } );
		} );

		it( 'Save the editor', async function () {
			// On mobile, site styles is a popover panel that blocks the Save button.
			// So let's always close site styles first to be safe. :)
			await fullSiteEditorPage.closeSiteStyles();
			await fullSiteEditorPage.save();
		} );

		it( '"wpcom_block_editor_global_styles_save" event fires with correct style properties', async function () {
			const eventDidFire = await editorTracksEventManager.didEventFire(
				'wpcom_block_editor_global_styles_save',
				{
					matchingProperties: {
						section: 'spacing',
						field: 'padding',
						field_value: `${ padding }px`,
					},
				}
			);
			expect( eventDidFire ).toBe( true );
		} );

		it( 'Close the page', async function () {
			await page.close();
		} );
	} );
} );