File size: 9,592 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
/**
 * @group editor-tracking
 */

import {
	DataHelper,
	envVariables,
	getTestAccountByFeature,
	envToFeatureKey,
	TestAccount,
	EditorTracksEventManager,
	FullSiteEditorPage,
	TemplatePartBlock,
	ElementHelper,
	HeaderBlock,
} from '@automattic/calypso-e2e';
import { Browser, Page } from 'playwright';

declare const browser: Browser;

const createTemplatePartName = () =>
	`TP-${ DataHelper.getTimestamp() }-${ DataHelper.getRandomInteger( 0, 100 ) }`;

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

		const createdTemplateParts: string[] = [];

		afterAll( async function () {
			if ( createdTemplateParts.length > 0 ) {
				// Template part data cleanup
				const page = await browser.newPage();

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

				const fullSiteEditorPage = new FullSiteEditorPage( page );
				await fullSiteEditorPage.visit( testAccount.getSiteURL( { protocol: true } ) );
				await fullSiteEditorPage.prepareForInteraction( { leaveWithoutSaving: true } );

				console.info( `Deleting created template parts: ${ createdTemplateParts.join( ', ' ) }` );
				await fullSiteEditorPage.deleteTemplateParts( createdTemplateParts );
			}
		} );

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

			let templatePartName: string;

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

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

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

				templatePartName = createTemplatePartName();
			} );

			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( 'Add a Template Part block', async function () {
				const block = await fullSiteEditorPage.addBlockFromSidebar(
					TemplatePartBlock.blockName,
					TemplatePartBlock.blockEditorSelector
				);
				templatePartBlock = new TemplatePartBlock( page, block );
			} );

			it( 'Create a new template part', async function () {
				await templatePartBlock.clickStartBlank();
				await fullSiteEditorPage.nameAndFinalizeTemplatePart( templatePartName );
				createdTemplateParts.push( templatePartName );
			} );

			it( '"wpcom_block_editor_create_template_part" event fires', async function () {
				const eventDidFire = await editorTracksEventManager.didEventFire(
					'wpcom_block_editor_create_template_part'
				);
				expect( eventDidFire ).toBe( true );
			} );

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

		describe( 'wpcom_block_editor_template_part_choose_existing/replace', function () {
			let page: Page;
			let testAccount: TestAccount;
			let fullSiteEditorPage: FullSiteEditorPage;
			let editorTracksEventManager: EditorTracksEventManager;
			let headerBlock: TemplatePartBlock;

			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( 'Add a Header block', async function () {
				// It's just a Template Part block with a different name in the sidebar, so we can re-use that POM class.
				const block = await fullSiteEditorPage.addBlockFromSidebar(
					HeaderBlock.blockName,
					HeaderBlock.blockEditorSelector
				);
				headerBlock = new HeaderBlock( page, block );
			} );

			it( 'Choose an existing template ("Header (Dark, small)")', async function () {
				await headerBlock.clickChoose();
				await fullSiteEditorPage.selectExistingTemplatePartFromModal( 'Header (Dark, small)' );
			} );

			it( '"wpcom_block_editor_template_part_choose_existing" event fires', async function () {
				const eventDidFire = await editorTracksEventManager.didEventFire(
					'wpcom_block_editor_template_part_choose_existing'
				);
				expect( eventDidFire ).toBe( true );
			} );

			it( '"wpcom_block_editor_template_part_replace" event does NOT fire', async function () {
				const eventDidFire = await editorTracksEventManager.didEventFire(
					'wpcom_block_editor_template_part_replace'
				);
				expect( eventDidFire ).toBe( false );
			} );

			it( 'Clear events for a clean slate', async function () {
				await editorTracksEventManager.clearEvents();
			} );

			it( 'Replace template with a different template ("Header (Dark, large)")', async function () {
				// First, let's make sure we have the right block focused!
				const blockId = await ElementHelper.getIdFromBlock( headerBlock.block );
				await fullSiteEditorPage.focusBlock( `#${ blockId }` );

				// Then we can take block toolbar actions.
				await fullSiteEditorPage.clickBlockToolbarOption( 'Replace Header (Dark, small)' );
				await fullSiteEditorPage.selectExistingTemplatePartFromModal( 'Header (Dark, large)' );
			} );

			it( '"wpcom_block_editor_template_part_replace" event fires', async function () {
				const eventDidFire = await editorTracksEventManager.didEventFire(
					'wpcom_block_editor_template_part_replace'
				);
				expect( eventDidFire ).toBe( true );
			} );

			it( '"wpcom_block_editor_template_part_choose_existing" event does NOT fire', async function () {
				const eventDidFire = await editorTracksEventManager.didEventFire(
					'wpcom_block_editor_template_part_choose_existing'
				);
				expect( eventDidFire ).toBe( false );
			} );

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

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

			let templatePartName: string;

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

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

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

				templatePartName = createTemplatePartName();
			} );

			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( 'Add a Page List block', async function () {
				await fullSiteEditorPage.addBlockFromSidebar(
					'Page List',
					'[aria-label="Block: Page List"]'
				);
			} );

			it( 'Convert to a template part', async function () {
				// Page List block should already be focused due to just adding it.
				await fullSiteEditorPage.clickBlockToolbarOption( 'Create Template part' );
				await fullSiteEditorPage.nameAndFinalizeTemplatePart( templatePartName );
				// This toast in unique to conversion. It doesn't fire during other creation flows..
				await fullSiteEditorPage.waitForConfirmationToast( 'Template part created' );
				createdTemplateParts.push( templatePartName );
			} );

			it( '"wpcom_block_editor_convert_to_template_part" event fires with correct "block_names"', async function () {
				const eventDidFire = await editorTracksEventManager.didEventFire(
					'wpcom_block_editor_convert_to_template_part',
					{
						matchingProperties: {
							block_names: 'core/page-list',
						},
					}
				);
				expect( eventDidFire ).toBe( true );
			} );

			it( 'Detach the blocks from the newly create template part', async function () {
				// After creation, the new Template Part block should already be focused.
				await fullSiteEditorPage.clickBlockToolbarOption( 'Detach blocks from template part' );
			} );

			it( '"wpcom_block_editor_template_part_detach_blocks" event fires with correct "block_names" and "template_part_id"', async function () {
				const eventDidFire = await editorTracksEventManager.didEventFire(
					'wpcom_block_editor_template_part_detach_blocks',
					{
						matchingProperties: {
							block_names: 'core/page-list',
							// Event property values are always lower case.
							template_part_id: `pub/twentytwentytwo//${ templatePartName.toLowerCase() }`,
						},
					}
				);
				expect( eventDidFire ).toBe( true );
			} );

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