-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathnavigation-list-view.spec.js
More file actions
579 lines (470 loc) · 14.9 KB
/
navigation-list-view.spec.js
File metadata and controls
579 lines (470 loc) · 14.9 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
test.describe( 'Navigation block - List view editing', () => {
const navMenuBlocksFixture = {
title: 'Test Menu',
content: `<!-- wp:navigation-link {"label":"Top Level Item 1","type":"page","id":250,"url":"http://localhost:8888/quod-error-esse-nemo-corporis-rerum-repellendus/","kind":"post-type"} /-->
<!-- wp:navigation-submenu {"label":"Top Level Item 2","type":"page","id":250,"url":"http://localhost:8888/quod-error-esse-nemo-corporis-rerum-repellendus/","kind":"post-type"} -->
<!-- wp:navigation-link {"label":"Test Submenu Item","type":"page","id":270,"url":"http://localhost:8888/et-aspernatur-recusandae-non-sint/","kind":"post-type"} /-->
<!-- /wp:navigation-submenu -->`,
};
test.beforeAll( async ( { requestUtils } ) => {
// We need pages to be published so the Link Control can return pages
await requestUtils.createPage( {
title: 'Test Page 1',
status: 'publish',
} );
await requestUtils.createPage( {
title: 'Test Page 2',
status: 'publish',
} );
await requestUtils.createPage( {
title: 'Test Page 3',
status: 'publish',
} );
} );
test.beforeEach( async ( { admin } ) => {
await admin.createNewPost();
} );
test.afterAll( async ( { requestUtils } ) => {
await Promise.all( [
requestUtils.deleteAllPages(),
requestUtils.deleteAllPosts(),
requestUtils.deleteAllMenus(),
] );
} );
test.use( {
linkControl: async ( { page }, use ) => {
await use( new LinkControl( { page } ) );
},
} );
test( 'show a list view in the inspector controls', async ( {
page,
editor,
requestUtils,
} ) => {
await requestUtils.createNavigationMenu( navMenuBlocksFixture );
await editor.insertBlock( { name: 'core/navigation' } );
await editor.openDocumentSettingsSidebar();
await expect(
page.getByRole( 'tab', {
name: 'List View',
} )
).toBeVisible();
const listViewPanel = page.getByRole( 'tabpanel', {
name: 'List View',
} );
await expect( listViewPanel ).toBeVisible();
await expect(
listViewPanel.getByRole( 'heading', {
name: 'Menu',
} )
).toBeVisible();
await expect(
listViewPanel.getByRole( 'treegrid', {
name: 'Block navigation structure',
description: 'Structure for navigation menu: Test Menu',
} )
).toBeVisible();
} );
test( `list view should correctly reflect navigation items' structure`, async ( {
page,
editor,
requestUtils,
} ) => {
await requestUtils.createNavigationMenu( navMenuBlocksFixture );
await editor.insertBlock( { name: 'core/navigation' } );
await editor.openDocumentSettingsSidebar();
const listView = page.getByRole( 'treegrid', {
name: 'Block navigation structure',
description: 'Structure for navigation menu: Test Menu',
} );
// Check the structure of the individual menu items matches the one that was created.
await expect(
listView
.getByRole( 'gridcell', {
name: 'Page Link',
} )
.filter( {
hasText: 'Block 1 of 2, Level 1', // proxy for filtering by description.
} )
.getByText( 'Top Level Item 1' )
).toBeVisible();
await expect(
listView
.getByRole( 'gridcell', {
name: 'Submenu',
} )
.filter( {
hasText: 'Block 2 of 2, Level 1', // proxy for filtering by description.
} )
.getByText( 'Top Level Item 2' )
).toBeVisible();
await expect(
listView
.getByRole( 'gridcell', {
name: 'Page Link',
} )
.filter( {
hasText: 'Block 1 of 1, Level 2', // proxy for filtering by description.
} )
.getByText( 'Test Submenu Item' )
).toBeVisible();
} );
test( `can add new menu items`, async ( {
page,
editor,
requestUtils,
linkControl,
} ) => {
const { id: menuId } = await requestUtils.createNavigationMenu(
navMenuBlocksFixture
);
// Insert x2 blocks as a stress test as several bugs have been found with inserting
// blocks into the navigation block when there are multiple blocks referencing the
// **same** menu.
await editor.insertBlock( {
name: 'core/navigation',
attributes: {
ref: menuId,
},
} );
await editor.insertBlock( {
name: 'core/navigation',
attributes: {
ref: menuId,
},
} );
await editor.openDocumentSettingsSidebar();
const listView = page.getByRole( 'treegrid', {
name: 'Block navigation structure',
description: 'Structure for navigation menu: Test Menu',
} );
const appender = listView.getByRole( 'button', {
name: 'Add block',
} );
await expect( appender ).toBeVisible();
await appender.click();
// Expect to see the block inserter.
await expect(
page.getByRole( 'searchbox', {
name: 'Search for blocks and patterns',
} )
).toBeFocused();
const blockResults = page.getByRole( 'listbox', {
name: 'Blocks',
} );
await expect( blockResults ).toBeVisible();
const blockResultOptions = blockResults.getByRole( 'option' );
// Expect to see the Page Link and Custom Link blocks as the nth(0) and nth(1) results.
// This is important for usability as the Page Link block is the most likely to be used.
await expect( blockResultOptions.nth( 0 ) ).toHaveText( 'Page Link' );
await expect( blockResultOptions.nth( 1 ) ).toHaveText( 'Custom Link' );
// Select the Page Link option.
const pageLinkResult = blockResultOptions.nth( 0 );
await pageLinkResult.click();
// Expect to see the Link creation UI be focused.
const linkUIInput = linkControl.getSearchInput();
// Coverage for bug whereby Link UI input would be incorrectly prepopulated.
// It should:
// - be focused - should not be in "preview" mode but rather ready to accept input.
// - be empty - not pre-populated
// See: https://github.com/WordPress/gutenberg/issues/50733
await expect( linkUIInput ).toBeFocused();
await expect( linkUIInput ).toBeEmpty();
const firstResult = await linkControl.getNthSearchResult( 0 );
// Grab the text from the first result so we can check (later on) that it was inserted.
const firstResultText = await linkControl.getSearchResultText(
firstResult
);
// Create the link.
await firstResult.click();
// Check the new menu item was inserted at the end of the existing menu.
await expect(
listView
.getByRole( 'gridcell', {
name: 'Page Link',
} )
.filter( {
hasText: 'Block 3 of 3, Level 1', // proxy for filtering by description.
} )
.getByText( firstResultText )
).toBeVisible();
} );
test( `can remove menu items`, async ( { page, editor, requestUtils } ) => {
await requestUtils.createNavigationMenu( navMenuBlocksFixture );
await editor.insertBlock( { name: 'core/navigation' } );
await editor.openDocumentSettingsSidebar();
const listView = page.getByRole( 'treegrid', {
name: 'Block navigation structure',
description: 'Structure for navigation menu: Test Menu',
} );
const submenuOptions = listView.getByRole( 'button', {
name: 'Options for Submenu',
} );
// Open the options menu.
await submenuOptions.click();
// usage of `page` is required because the options menu is rendered into a slot
// outside of the treegrid.
const removeBlockOption = page
.getByRole( 'menu', {
name: 'Options for Submenu',
} )
.getByRole( 'menuitem', {
name: 'Remove Top Level Item 2',
} );
await removeBlockOption.click();
// Check the menu item was removed.
await expect(
listView
.getByRole( 'gridcell', {
name: 'Submenu',
} )
.filter( {
hasText: 'Block 2 of 2, Level 1', // proxy for filtering by description.
} )
.getByText( 'Top Level Item 2' )
).toBeHidden();
} );
test( `can edit menu items`, async ( { page, editor, requestUtils } ) => {
await requestUtils.createNavigationMenu( navMenuBlocksFixture );
await editor.insertBlock( { name: 'core/navigation' } );
await editor.openDocumentSettingsSidebar();
const listView = page.getByRole( 'treegrid', {
name: 'Block navigation structure',
description: 'Structure for navigation menu: Test Menu',
} );
// Click on the first menu item to open its settings.
const firstMenuItemAnchor = listView
.getByRole( 'link', {
name: 'Page',
includeHidden: true,
} )
.getByText( 'Top Level Item 1' );
await firstMenuItemAnchor.click();
// Get the settings panel.
const blockSettings = page.getByRole( 'region', {
name: 'Editor settings',
} );
await expect( blockSettings ).toBeVisible();
await expect(
blockSettings.getByRole( 'heading', {
name: 'Page Link',
} )
).toBeVisible();
await expect(
blockSettings.getByRole( 'tab', {
name: 'Settings',
selected: true,
} )
).toBeVisible();
await expect(
blockSettings
.getByRole( 'tabpanel', {
name: 'Settings',
} )
.getByRole( 'heading', {
name: 'Settings',
} )
).toBeVisible();
const labelInput = blockSettings.getByRole( 'textbox', {
name: 'Label',
} );
await expect( labelInput ).toHaveValue( 'Top Level Item 1' );
await labelInput.focus();
await page.keyboard.type( 'Changed label' );
// Click the back button to go back to the Nav block.
await blockSettings
.getByRole( 'button', {
name: 'Go to parent Navigation block',
} )
.click();
// Check we're back on the Nav block list view.
const listViewPanel = page.getByRole( 'tabpanel', {
name: 'List View',
} );
await expect( listViewPanel ).toBeVisible();
// Check the label was updated.
await expect(
listViewPanel
.getByRole( 'gridcell', {
name: 'Page Link',
} )
.filter( {
hasText: 'Block 1 of 2, Level 1', // proxy for filtering by description.
} )
.getByText( 'Changed label' ) // new label text
).toBeVisible();
} );
test( `can add submenus`, async ( {
page,
editor,
requestUtils,
linkControl,
} ) => {
await requestUtils.createNavigationMenu( navMenuBlocksFixture );
await editor.insertBlock( { name: 'core/navigation' } );
await editor.openDocumentSettingsSidebar();
const listView = page.getByRole( 'treegrid', {
name: 'Block navigation structure',
description: 'Structure for navigation menu: Test Menu',
} );
// click on options menu for the first menu item and select remove.
const firstMenuItem = listView
.getByRole( 'gridcell', {
name: 'Page Link',
} )
.filter( {
hasText: 'Block 1 of 2, Level 1', // proxy for filtering by description.
} );
// The options menu button is a sibling of the menu item gridcell.
const firstItemOptions = firstMenuItem
.locator( '..' ) // parent selector.
.getByRole( 'button', {
name: 'Options for Page Link',
} );
// Open the options menu.
await firstItemOptions.click();
// Add the submenu.
// usage of `page` is required because the options menu is rendered into a slot
// outside of the treegrid.
const addSubmenuOption = page
.getByRole( 'menu', {
name: 'Options for Page Link',
} )
.getByRole( 'menuitem', {
name: 'Add submenu',
} );
await addSubmenuOption.click();
await linkControl.searchFor( 'https://wordpress.org' );
await page.keyboard.press( 'Enter' );
// Check the new item was inserted in the correct place.
await expect(
listView
.getByRole( 'gridcell', {
name: 'Custom Link',
} )
.filter( {
hasText: 'Block 1 of 1, Level 2', // proxy for filtering by description.
} )
.getByText( 'wordpress.org' )
).toBeVisible();
// Check that the original item is still there but that it is now
// a submenu item.
await expect(
listView
.getByRole( 'gridcell', {
name: 'Submenu',
} )
.filter( {
hasText: 'Block 1 of 2, Level 1', // proxy for filtering by description.
} )
.getByText( 'Top Level Item 1' )
).toBeVisible();
} );
test( `does not display link interface for blocks that have not just been inserted`, async ( {
page,
editor,
requestUtils,
linkControl,
} ) => {
// Provides coverage for a bug whereby the Link UI would be unexpectedly displayed for the last
// inserted block even if the block had been deselected and then reselected.
// See: https://github.com/WordPress/gutenberg/issues/50601
const { id: menuId } = await requestUtils.createNavigationMenu(
navMenuBlocksFixture
);
// Insert x2 blocks as a stress test as several bugs have been found with inserting
// blocks into the navigation block when there are multiple blocks referencing the
// **same** menu.
await editor.insertBlock( {
name: 'core/navigation',
attributes: {
ref: menuId,
},
} );
await editor.insertBlock( {
name: 'core/navigation',
attributes: {
ref: menuId,
},
} );
await editor.openDocumentSettingsSidebar();
const listView = page.getByRole( 'treegrid', {
name: 'Block navigation structure',
description: 'Structure for navigation menu: Test Menu',
} );
await listView
.getByRole( 'button', {
name: 'Add block',
} )
.click();
const blockResults = page.getByRole( 'listbox', {
name: 'Blocks',
} );
await expect( blockResults ).toBeVisible();
const blockResultOptions = blockResults.getByRole( 'option' );
// Select the Page Link option.
await blockResultOptions.nth( 0 ).click();
// Immediately dismiss the Link UI thereby not populating the `url` attribute
// of the block.
await page.keyboard.press( 'Escape' );
// Get the Inspector Tabs.
const blockSettings = page.getByRole( 'region', {
name: 'Editor settings',
} );
// Trigger "unmount" of the List View.
await blockSettings
.getByRole( 'tab', {
name: 'Settings',
} )
.click();
// "Remount" the List View.
// this is where the bug previously occurred.
await blockSettings
.getByRole( 'tab', {
name: 'List View',
} )
.click();
// Check that despite being the last inserted block, the Link UI is not displayed
// in this scenario because it was not **just** inserted into the List View (i.e.
// we have unmounted the list view and then remounted it).
await expect( linkControl.getSearchInput() ).toBeHidden();
} );
} );
class LinkControl {
constructor( { page } ) {
this.page = page;
}
getSearchInput() {
return this.page.getByRole( 'combobox', {
name: 'Link',
} );
}
async getSearchResults() {
const searchInput = this.getSearchInput();
const resultsRef = await searchInput.getAttribute( 'aria-owns' );
const linkUIResults = this.page.locator( `#${ resultsRef }` );
await expect( linkUIResults ).toBeVisible();
return linkUIResults.getByRole( 'option' );
}
async getNthSearchResult( index = 0 ) {
const results = await this.getSearchResults();
return results.nth( index );
}
async searchFor( searchTerm = 'https://wordpress.org' ) {
const input = this.getSearchInput();
await expect( input ).toBeFocused();
await this.page.keyboard.type( searchTerm );
await expect( input ).toHaveValue( searchTerm );
return input;
}
async getSearchResultText( result ) {
await expect( result ).toBeVisible();
return result
.locator( '.components-menu-item__item' ) // this is the only way to get the label text without the URL.
.innerText();
}
}