-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathnavigation.spec.js
More file actions
1310 lines (1102 loc) · 37.8 KB
/
navigation.spec.js
File metadata and controls
1310 lines (1102 loc) · 37.8 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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
test.describe( 'Navigation block', () => {
test.beforeAll( async ( { requestUtils } ) => {
//TT3 is preferable to emptytheme because it already has the navigation block on its templates.
await requestUtils.activateTheme( 'twentytwentythree' );
} );
test.beforeEach( async ( { requestUtils } ) => {
await Promise.all( [ requestUtils.deleteAllMenus() ] );
} );
test.afterAll( async ( { requestUtils } ) => {
await Promise.all( [
requestUtils.deleteAllMenus(),
requestUtils.activateTheme( 'twentytwentyone' ),
] );
} );
test.afterEach( async ( { requestUtils } ) => {
await requestUtils.deleteAllPosts();
await requestUtils.deleteAllMenus();
} );
test.describe( 'As a user I want the navigation block to fallback to the best possible default', () => {
test( 'default to a list of pages if there are no menus', async ( {
admin,
editor,
} ) => {
await admin.createNewPost();
await editor.insertBlock( { name: 'core/navigation' } );
const pageListBlock = editor.canvas.getByRole( 'document', {
name: 'Block: Page List',
} );
await expect( pageListBlock ).toBeVisible( {
// Wait for the Nav and Page List block API requests to resolve.
// Note: avoid waiting on network requests as these are not perceivable
// to the user.
// See: https://github.com/WordPress/gutenberg/pull/45070#issuecomment-1373712007.
timeout: 10000,
} );
// Check the markup of the block is correct.
await editor.publishPost();
const content = await editor.getEditedPostContent();
expect( content ).toMatch( /<!-- wp:navigation {"ref":\d+} \/-->/ );
} );
test( 'default to my only existing menu', async ( {
admin,
editor,
page,
requestUtils,
} ) => {
await admin.createNewPost();
const createdMenu = await requestUtils.createNavigationMenu( {
title: 'Test Menu 1',
content:
'<!-- wp:navigation-link {"label":"WordPress","type":"custom","url":"http://www.wordpress.org/","kind":"custom"} /-->',
} );
await editor.insertBlock( { name: 'core/navigation' } );
// Check the block in the canvas.
await expect(
editor.canvas.locator(
`role=textbox[name="Navigation link text"i] >> text="WordPress"`
)
).toBeVisible();
// Check the markup of the block is correct.
await editor.publishPost();
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/navigation',
attributes: { ref: createdMenu.id },
},
] );
await page.locator( 'role=button[name="Close panel"i]' ).click();
// Check the block in the frontend.
await page.goto( '/' );
await expect(
page.locator(
`role=navigation >> role=link[name="WordPress"i]`
)
).toBeVisible();
} );
test( 'default to the only existing classic menu if there are no block menus', async ( {
admin,
page,
editor,
requestUtils,
} ) => {
// Create a classic menu.
await requestUtils.createClassicMenu( 'Test Classic 1' );
await admin.createNewPost();
await editor.insertBlock( { name: 'core/navigation' } );
// We need to check the canvas after inserting the navigation block to be able to target the block.
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/navigation',
},
] );
// Check the block in the canvas.
await expect(
editor.canvas.locator(
`role=textbox[name="Navigation link text"i] >> text="Custom link"`
)
).toBeVisible( { timeout: 10000 } ); // allow time for network request.
// Check the block in the frontend.
await page.goto( '/' );
await expect(
page.locator(
`role=navigation >> role=link[name="Custom link"i]`
)
).toBeVisible();
} );
test( 'default to my most recently created menu', async ( {
admin,
page,
editor,
requestUtils,
} ) => {
await admin.createNewPost();
await requestUtils.createNavigationMenu( {
title: 'Test Menu 1',
content:
'<!-- wp:navigation-link {"label":"Menu 1 Link","type":"custom","url":"http://localhost:8889/#menu-1-link","kind":"custom"} /-->',
} );
//FIXME this is needed because if the two menus are created at the same time, the API will return them in the wrong order.
//https://core.trac.wordpress.org/ticket/57914
await editor.page.waitForTimeout( 1000 );
const latestMenu = await requestUtils.createNavigationMenu( {
title: 'Test Menu 2',
content:
'<!-- wp:navigation-link {"label":"Menu 2 Link","type":"custom","url":"http://localhost:8889/#menu-2-link","kind":"custom"} /-->',
} );
await editor.insertBlock( { name: 'core/navigation' } );
// Check the markup of the block is correct.
await editor.publishPost();
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/navigation',
attributes: { ref: latestMenu.id },
},
] );
await page.locator( 'role=button[name="Close panel"i]' ).click();
// Check the block in the canvas.
await expect(
editor.canvas.locator(
`role=textbox[name="Navigation link text"i] >> text="Menu 2 Link"`
)
).toBeVisible();
// Check the block in the frontend.
await page.goto( '/' );
await expect(
page.locator(
`role=navigation >> role=link[name="Menu 2 Link"i]`
)
).toBeVisible();
} );
} );
test.describe( 'As a user I want to create submenus using the navigation block', () => {
test.beforeAll( async ( { requestUtils } ) => {
//TT3 is preferable to emptytheme because it already has the navigation block on its templates.
await requestUtils.activateTheme( 'twentytwentythree' );
} );
test.beforeEach( async ( { requestUtils } ) => {
await Promise.all( [ requestUtils.deleteAllMenus() ] );
} );
test.afterAll( async ( { requestUtils } ) => {
await Promise.all( [
requestUtils.deleteAllMenus(),
requestUtils.activateTheme( 'twentytwentyone' ),
] );
} );
test.afterEach( async ( { requestUtils } ) => {
await requestUtils.deleteAllPosts();
} );
test( 'create a submenu', async ( {
admin,
page,
editor,
requestUtils,
} ) => {
await admin.createNewPost();
await requestUtils.createNavigationMenu( {
title: 'Test Menu',
content: '',
} );
await editor.insertBlock( { name: 'core/navigation' } );
const navBlockInserter = editor.canvas.getByRole( 'button', {
name: 'Add block',
} );
await navBlockInserter.click();
await page.keyboard.type( 'https://example.com' );
await page.keyboard.press( 'Enter' );
const addSubmenuButton = page.getByRole( 'button', {
name: 'Add submenu',
} );
await addSubmenuButton.click();
await editor.publishPost();
await page.locator( 'role=button[name="Close panel"i]' ).click();
await page.goto( '/' );
await expect(
page.locator(
`role=navigation >> role=button[name="example.com submenu "i]`
)
).toBeVisible();
} );
test( 'submenu converts to link automatically', async ( {
admin,
pageUtils,
editor,
requestUtils,
} ) => {
await admin.createNewPost();
await requestUtils.createNavigationMenu( {
title: 'Test Menu',
content:
'<!-- wp:navigation-submenu {"label":"WordPress","type":"custom","url":"http://www.wordpress.org/","kind":"custom"} --><!-- wp:navigation-link {"label":"WordPress Child","type":"custom","url":"http://www.wordpress.org/","kind":"custom"} /--><!-- /wp:navigation-submenu -->',
} );
await editor.insertBlock( { name: 'core/navigation' } );
await expect(
editor.canvas.locator(
`role=textbox[name="Navigation link text"i] >> text="WordPress"`
)
).toBeVisible();
const navigationBlock = editor.canvas.getByRole( 'document', {
name: 'Block: Navigation',
} );
await editor.selectBlocks( navigationBlock );
const submenuBlock1 = editor.canvas.getByRole( 'document', {
name: 'Block: Submenu',
} );
await expect( submenuBlock1 ).toBeVisible();
// select the child link via keyboard
await pageUtils.pressKeys( 'ArrowDown' );
await pageUtils.pressKeys( 'ArrowDown' );
await pageUtils.pressKeys( 'ArrowDown' );
// remove the child link
await pageUtils.pressKeys( 'access+z' );
const submenuBlock2 = editor.canvas.getByRole( 'document', {
name: 'Block: Submenu',
} );
await expect( submenuBlock2 ).toBeHidden();
} );
} );
test.describe( 'As a user I want to see a warning if the menu referenced by a navigation block is not available', () => {
test.beforeEach( async ( { admin } ) => {
await admin.createNewPost();
} );
test( 'warning message shows when given an unknown ref', async ( {
editor,
} ) => {
await editor.insertBlock( {
name: 'core/navigation',
attributes: {
ref: 1,
},
} );
// Check the markup of the block is correct.
await editor.publishPost();
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/navigation',
attributes: { ref: 1 },
},
] );
// Find the warning message
const warningMessage = editor.canvas
.getByRole( 'document', { name: 'Block: Navigation' } )
.getByText(
'Navigation menu has been deleted or is unavailable.'
);
await expect( warningMessage ).toBeVisible();
} );
} );
test.describe( 'Existing blocks', () => {
test( 'adding new links to a block with existing inner blocks triggers creation of a single Navigation Menu', async ( {
admin,
page,
editor,
requestUtils,
} ) => {
// As this test depends on there being no menus,
// we need to delete any existing menus as an explicit
// precondition rather than rely on global test setup.
await requestUtils.deleteAllMenus();
// Ensure that there are no menus before beginning the test.
expect(
await requestUtils.getNavigationMenus( {
status: [ 'publish', 'draft' ],
} )
).toHaveLength( 0 );
await admin.createNewPost();
await editor.insertBlock( {
name: 'core/navigation',
attributes: {},
innerBlocks: [
{
name: 'core/page-list',
},
],
} );
const navBlock = editor.canvas.getByRole( 'document', {
name: 'Block: Navigation',
} );
await expect(
editor.canvas.getByRole( 'document', {
name: 'Block: Page List',
} )
).toBeVisible();
await expect( navBlock ).toBeVisible();
await editor.selectBlocks( navBlock );
await navBlock.getByRole( 'button', { name: 'Add block' } ).click();
// This relies on network so allow additional time for
// the request to complete.
await expect(
page.getByRole( 'button', {
name: 'Dismiss this notice',
text: 'Navigation Menu successfully created',
} )
).toBeVisible( { timeout: 10000 } );
// The creattion Navigaiton Menu will be a draft
// so we need to check for both publish and draft.
expect(
await requestUtils.getNavigationMenus( {
status: [ 'publish', 'draft' ],
} )
).toHaveLength( 1 );
} );
} );
test.describe( '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.afterAll( async ( { requestUtils } ) => {
await requestUtils.deleteAllPages();
} );
test.use( {
linkControl: async ( { page }, use ) => {
await use( new LinkControl( { page } ) );
},
} );
test( 'show a list view in the inspector controls', async ( {
admin,
page,
editor,
requestUtils,
} ) => {
await admin.createNewPost();
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 ( {
admin,
page,
editor,
requestUtils,
} ) => {
await admin.createNewPost();
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 ( {
admin,
page,
editor,
requestUtils,
linkControl,
} ) => {
await admin.createNewPost();
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 ( {
admin,
page,
editor,
requestUtils,
} ) => {
await admin.createNewPost();
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' )
).not.toBeVisible();
} );
test( `can edit menu items`, async ( {
admin,
page,
editor,
requestUtils,
} ) => {
await admin.createNewPost();
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 ( {
admin,
page,
editor,
requestUtils,
linkControl,
} ) => {
await admin.createNewPost();
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 ( {
admin,
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
await admin.createNewPost();
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 linkControl.pressCancel();
// 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() ).not.toBeVisible();
} );
} );
} );
test.describe( 'Navigation block - Frontend interactivity', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'emptytheme' );
await requestUtils.deleteAllTemplates( 'wp_template_part' );
await requestUtils.deleteAllPages();
await requestUtils.deleteAllMenus();
} );
test.afterAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyone' );
} );
test.afterEach( async ( { requestUtils } ) => {
await requestUtils.deleteAllTemplates( 'wp_template_part' );
await requestUtils.deleteAllPages();
await requestUtils.deleteAllMenus();
} );
test.describe( 'Overlay menu', () => {
test.beforeEach( async ( { admin, editor, requestUtils } ) => {
await admin.visitSiteEditor( {
postId: 'emptytheme//header',
postType: 'wp_template_part',
} );
await editor.canvas.click( 'body' );
await requestUtils.createNavigationMenu( {
title: 'Hidden menu',
content: `
<!-- wp:navigation-link {"label":"Item 1","type":"custom","url":"http://www.wordpress.org/"} /-->
<!-- wp:navigation-link {"label":"Item 2","type":"custom","url":"http://www.wordpress.org/"} /-->
`,
} );
await editor.insertBlock( {
name: 'core/navigation',
attributes: { overlayMenu: 'always' },
} );
await editor.saveSiteEditorEntities();
} );
test( 'Overlay menu interactions', async ( { page, pageUtils } ) => {
await page.goto( '/' );
const overlayMenuFirstElement = page.getByRole( 'link', {
name: 'Item 1',
} );
const openMenuButton = page.getByRole( 'button', {
name: 'Open menu',
} );
const closeMenuButton = page.getByRole( 'button', {
name: 'Close menu',
} );
// Test: overlay menu opens on click on open menu button
await expect( overlayMenuFirstElement ).toBeHidden();
await openMenuButton.click();
await expect( overlayMenuFirstElement ).toBeVisible();
// Test: overlay menu focuses on first element after opening
await expect( overlayMenuFirstElement ).toBeFocused();
// Test: overlay menu traps focus
await pageUtils.pressKeys( 'Tab', { times: 2, delay: 50 } );
await expect( closeMenuButton ).toBeFocused();
await pageUtils.pressKeys( 'Shift+Tab', { times: 2, delay: 50 } );
await expect( overlayMenuFirstElement ).toBeFocused();