-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
editing-widgets.test.js
948 lines (819 loc) · 26.4 KB
/
editing-widgets.test.js
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
/**
* WordPress dependencies
*/
import {
__experimentalActivatePlugin as activatePlugin,
activateTheme,
clickBlockToolbarButton,
__experimentalDeactivatePlugin as deactivatePlugin,
showBlockToolbar,
visitAdminPage,
deleteAllWidgets,
pressKeyWithModifier,
__experimentalRest as rest,
openListView,
closeListView,
openGlobalBlockInserter,
searchForBlock,
closeGlobalBlockInserter,
setBrowserViewport,
} from '@wordpress/e2e-test-utils';
/**
* External dependencies
*/
// eslint-disable-next-line no-restricted-imports
import { find, findAll } from 'puppeteer-testing-library';
import { groupBy, mapValues } from 'lodash';
describe( 'Widgets screen', () => {
beforeEach( async () => {
await visitWidgetsScreen();
// Disable welcome guide if it is enabled.
const isWelcomeGuideActive = await page.evaluate( () =>
wp.data
.select( 'core/interface' )
.isFeatureActive( 'core/edit-widgets', 'welcomeGuide' )
);
if ( isWelcomeGuideActive ) {
await page.evaluate( () =>
wp.data
.dispatch( 'core/interface' )
.toggleFeature( 'core/edit-widgets', 'welcomeGuide' )
);
}
// Wait for the widget areas to load.
await findAll( {
role: 'document',
name: 'Block: Widget Area',
} );
} );
afterEach( async () => {
await deleteAllWidgets();
} );
beforeAll( async () => {
// TODO: Ideally we can bundle our test theme directly in the repo.
await activateTheme( 'twentytwenty' );
await deleteAllWidgets();
} );
afterAll( async () => {
await activateTheme( 'twentytwentyone' );
} );
async function getBlockInGlobalInserter( blockName ) {
await openGlobalBlockInserter();
const blockLibrary = await find( {
role: 'region',
name: 'Block Library',
} );
// Check that there are categorizations in the inserter (#26329).
const categoryHeaders = await findAll(
{
role: 'heading',
level: 2,
},
{
root: blockLibrary,
}
);
expect( categoryHeaders.length > 0 ).toBe( true );
const searchBox = await find( {
role: 'searchbox',
name: 'Search for blocks and patterns',
} );
await searchBox.type( blockName );
await searchForBlock( blockName );
return await page.waitForXPath(
`//button//span[contains(text(), '${ blockName }')]`
);
}
async function expectInsertionPointIndicatorToBeBelowLastBlock(
widgetArea
) {
const childBlocks = await findAll(
{ selector: '[data-block]' },
{ root: widgetArea }
);
// The initial block appender also has the [data-block] property, adding to the count.
const lastBlock = childBlocks[ childBlocks.length - 2 ];
const lastBlockBoundingBox = await lastBlock.boundingBox();
const insertionPointIndicator = await page.$(
'.block-editor-block-list__insertion-point-indicator'
);
const insertionPointIndicatorBoundingBox = await insertionPointIndicator.boundingBox();
expect(
insertionPointIndicatorBoundingBox.y > lastBlockBoundingBox.y
).toBe( true );
}
it.skip( 'Should insert content using the global inserter', async () => {
const updateButton = await find( {
role: 'button',
name: 'Update',
} );
// Update button should start out disabled.
expect(
await updateButton.evaluate( ( button ) => button.disabled )
).toBe( true );
const widgetAreas = await findAll( {
role: 'document',
name: 'Block: Widget Area',
} );
const [ firstWidgetArea, secondWidgetArea ] = widgetAreas;
let addParagraphBlock = await getBlockInGlobalInserter( 'Paragraph' );
await addParagraphBlock.hover();
// FIXME: The insertion point indicator is not showing when the widget area has no blocks.
// await expectInsertionPointIndicatorToBeBelowLastBlock(
// firstWidgetArea
// );
await addParagraphBlock.click();
// Adding content should enable the Update button.
expect(
await updateButton.evaluate( ( button ) => button.disabled )
).toBe( false );
let addedParagraphBlockInFirstWidgetArea = await find(
{
name: /^Empty block/,
selector: '[data-block][data-type="core/paragraph"]',
},
{
root: firstWidgetArea,
}
);
await addedParagraphBlockInFirstWidgetArea.focus();
await page.keyboard.type( 'First Paragraph' );
addParagraphBlock = await getBlockInGlobalInserter( 'Paragraph' );
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Tab' );
await expectInsertionPointIndicatorToBeBelowLastBlock(
firstWidgetArea
);
await addParagraphBlock.focus();
await addParagraphBlock.click();
addedParagraphBlockInFirstWidgetArea = await firstWidgetArea.$(
'[data-block][data-type="core/paragraph"][aria-label^="Empty block"]'
);
await addedParagraphBlockInFirstWidgetArea.focus();
await page.keyboard.type( 'Second Paragraph' );
const addShortCodeBlock = await getBlockInGlobalInserter( 'Shortcode' );
await addShortCodeBlock.click();
const shortCodeInput = await page.waitForSelector(
'textarea[aria-label="Shortcode text"]'
);
await shortCodeInput.focus();
// The famous Big Buck Bunny video.
await shortCodeInput.type(
'[video src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"]'
);
// Add to the second widget area.
await secondWidgetArea.click();
addParagraphBlock = await getBlockInGlobalInserter( 'Paragraph' );
await addParagraphBlock.click();
const addedParagraphBlockInSecondWidgetArea = await secondWidgetArea.$(
'[data-block][data-type="core/paragraph"][aria-label^="Empty block"]'
);
await addedParagraphBlockInSecondWidgetArea.focus();
await page.keyboard.type( 'Third Paragraph' );
await saveWidgets();
// The Update button should be disabled again after saving.
expect(
await updateButton.evaluate( ( button ) => button.disabled )
).toBe( true );
const serializedWidgetAreas = await getSerializedWidgetAreas();
expect( serializedWidgetAreas ).toMatchInlineSnapshot( `
Object {
"sidebar-1": "<div class=\\"widget widget_block widget_text\\"><div class=\\"widget-content\\">
<p>First Paragraph</p>
</div></div>
<div class=\\"widget widget_block widget_text\\"><div class=\\"widget-content\\">
<p>Second Paragraph</p>
</div></div>
<div class=\\"widget widget_block\\"><div class=\\"widget-content\\"><p><div style=\\"width: 580px;\\" class=\\"wp-video\\"><!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->
<video class=\\"wp-video-shortcode\\" id=\\"video-0-1\\" width=\\"580\\" height=\\"326\\" preload=\\"metadata\\" controls=\\"controls\\"><source type=\\"video/mp4\\" src=\\"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4?_=1\\" /><a href=\\"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4\\">http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4</a></video></div></p>
</div></div>",
"sidebar-2": "<div class=\\"widget widget_block widget_text\\"><div class=\\"widget-content\\">
<p>Third Paragraph</p>
</div></div>",
}
` );
} );
it.skip( 'Should insert content using the inline inserter', async () => {
const [ firstWidgetArea ] = await findAll( {
role: 'document',
name: 'Block: Widget Area',
} );
// Scroll to the end of the first widget area.
await firstWidgetArea.evaluate( ( node ) =>
node.scrollIntoView( { block: 'end' } )
);
const firstWidgetAreaBoundingBox = await firstWidgetArea.boundingBox();
// Click near the end of the widget area to select it.
await page.mouse.click(
firstWidgetAreaBoundingBox.x + firstWidgetAreaBoundingBox.width / 2,
firstWidgetAreaBoundingBox.y +
firstWidgetAreaBoundingBox.height -
10
);
let inlineInserterButton = await find( {
role: 'combobox',
name: 'Add block',
} );
await inlineInserterButton.click();
let inlineQuickInserter = await find( {
role: 'listbox',
name: 'Blocks',
} );
const paragraphBlock = await find(
{
role: 'option',
name: 'Paragraph',
},
{
root: inlineQuickInserter,
}
);
await paragraphBlock.click();
const firstParagraphBlock = await find(
{
name: /^Empty block/,
selector: '[data-block][data-type="core/paragraph"]',
},
{
root: firstWidgetArea,
}
);
await firstParagraphBlock.focus();
await page.keyboard.type( 'First Paragraph' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'Second Paragraph' );
const secondParagraphBlock = await page.evaluateHandle(
() => document.activeElement
);
await expect( secondParagraphBlock ).not.toBeElement(
firstParagraphBlock
);
const secondParagraphBlockBoundingBox = await secondParagraphBlock.boundingBox();
// Click outside the block to move the focus back to the widget area.
await page.mouse.click(
secondParagraphBlockBoundingBox.x +
firstWidgetAreaBoundingBox.width / 2,
secondParagraphBlockBoundingBox.y +
secondParagraphBlockBoundingBox.height +
10
);
// Hover above the last block to trigger the inline inserter between blocks.
await page.mouse.move(
secondParagraphBlockBoundingBox.x +
secondParagraphBlockBoundingBox.width / 2,
secondParagraphBlockBoundingBox.y - 10
);
// There will be 2 matches here.
// One is the in-between inserter,
// and the other one is the button block appender.
[ inlineInserterButton ] = await findAll( {
role: 'combobox',
name: 'Add block',
} );
await inlineInserterButton.click();
const inserterSearchBox = await find( {
role: 'searchbox',
name: 'Search for blocks and patterns',
} );
await expect( inserterSearchBox ).toHaveFocus();
await page.keyboard.type( 'Heading' );
inlineQuickInserter = await find( {
role: 'listbox',
name: 'Blocks',
} );
const headingBlockOption = await find(
{
role: 'option',
name: 'Heading',
},
{
root: inlineQuickInserter,
}
);
await headingBlockOption.click();
// Get the added heading block as second last block.
const addedHeadingBlock = await secondParagraphBlock.evaluateHandle(
( node ) => node.previousSibling
);
await expect( addedHeadingBlock ).toHaveFocus();
await page.keyboard.type( 'My Heading' );
await expect( addedHeadingBlock ).toMatchQuery( {
name: 'Block: Heading',
level: 2,
value: 'My Heading',
} );
await saveWidgets();
const serializedWidgetAreas = await getSerializedWidgetAreas();
expect( serializedWidgetAreas ).toMatchInlineSnapshot( `
Object {
"sidebar-1": "<div class=\\"widget widget_block widget_text\\"><div class=\\"widget-content\\">
<p>First Paragraph</p>
</div></div>
<div class=\\"widget widget_block\\"><div class=\\"widget-content\\">
<h2 id=\\"my-heading\\">My Heading</h2>
</div></div>
<div class=\\"widget widget_block widget_text\\"><div class=\\"widget-content\\">
<p>Second Paragraph</p>
</div></div>",
}
` );
} );
describe( 'Function widgets', () => {
async function addMarquee( nbExpectedMarquees ) {
const marqueeBlock = await getBlockInGlobalInserter(
'Marquee Greeting'
);
await marqueeBlock.click();
await page.waitForFunction(
( expectedMarquees ) => {
return (
document.querySelectorAll(
'[data-testid="marquee-greeting"]'
).length === expectedMarquees
);
},
{},
nbExpectedMarquees
);
}
async function deleteExistingMarquees() {
const widgetAreasHoldingMarqueeWidgets = await page.$x(
'//input[@data-testid="marquee-greeting"]/ancestor::div[@aria-label="Block: Widget Area"]'
);
for ( const widgetArea of widgetAreasHoldingMarqueeWidgets ) {
const closedPanelBody = await widgetArea.$(
'.components-panel__body:not(.is-opened)'
);
if ( closedPanelBody ) {
await closedPanelBody.focus();
await closedPanelBody.click();
}
const [ existingMarqueeWidgets ] = await widgetArea.$x(
'//input[@data-testid="marquee-greeting"]/ancestor::div[@data-block][contains(@class, "wp-block-legacy-widget")]'
);
if ( existingMarqueeWidgets ) {
await existingMarqueeWidgets.focus();
await pressKeyWithModifier( 'access', 'z' );
}
}
}
beforeAll( async () => {
await activatePlugin( 'gutenberg-test-marquee-widget' );
} );
beforeEach( async () => {
await deleteExistingMarquees();
} );
afterAll( async () => {
await deactivatePlugin( 'gutenberg-test-marquee-widget' );
} );
it( 'Should add and save the marquee widget', async () => {
await addMarquee( 1 );
// Use find because the input might not be visible at first.
const marqueeInput = await find( {
selector: '[data-testid="marquee-greeting"]',
} );
// Clear the input.
await marqueeInput.evaluate( ( input ) => {
input.value = '';
} );
await marqueeInput.type( 'Howdy' );
// The first marquee is saved after clicking the form save button.
const marqueeSaveButton = await marqueeInput.evaluateHandle(
( input ) =>
input
// Get the parent block element.
.closest( '[data-block]' )
// Get the save button.
.querySelector( 'button[type="submit"]' )
);
await marqueeSaveButton.click();
await saveWidgets();
let editedSerializedWidgetAreas = await getSerializedWidgetAreas();
await expect( editedSerializedWidgetAreas ).toMatchInlineSnapshot( `
Object {
"sidebar-1": "<marquee>Howdy</marquee>",
}
` );
await page.reload();
editedSerializedWidgetAreas = await getSerializedWidgetAreas();
await expect( editedSerializedWidgetAreas ).toMatchInlineSnapshot( `
Object {
"sidebar-1": "<marquee>Howdy</marquee>",
}
` );
await addMarquee( 2 );
const marqueeInputs = await page.$$(
'[data-testid="marquee-greeting"]'
);
expect( marqueeInputs ).toHaveLength( 2 );
await marqueeInputs[ 0 ].type( 'first ' );
await marqueeInputs[ 1 ].type( 'Second ' );
// No marquee should be changed without clicking on their "save" button.
// The second marquee shouldn't be stored as a widget.
// See #32978 for more info.
await saveWidgets();
editedSerializedWidgetAreas = await getSerializedWidgetAreas();
await expect( editedSerializedWidgetAreas ).toMatchInlineSnapshot( `
Object {
"sidebar-1": "<marquee>Howdy</marquee>",
}
` );
await page.reload();
const marqueesAfter = await findAll( {
selector: '[data-testid="marquee-greeting"]',
} );
expect( marqueesAfter ).toHaveLength( 1 );
} );
} );
it.skip( 'Should duplicate the widgets', async () => {
let [ firstWidgetArea ] = await findAll( {
role: 'document',
name: 'Block: Widget Area',
} );
const addParagraphBlock = await getBlockInGlobalInserter( 'Paragraph' );
await addParagraphBlock.click();
let firstParagraphBlock = await find(
{
role: 'document',
name: /^Empty block/,
},
{ root: firstWidgetArea }
);
await firstParagraphBlock.focus();
await page.keyboard.type( 'First Paragraph' );
await saveWidgets();
await page.reload();
// Wait for the widget areas to load.
[ firstWidgetArea ] = await findAll( {
role: 'document',
name: 'Block: Widget Area',
} );
const initialSerializedWidgetAreas = await getSerializedWidgetAreas();
expect( initialSerializedWidgetAreas ).toMatchInlineSnapshot( `
Object {
"sidebar-1": "<div class=\\"widget widget_block widget_text\\"><div class=\\"widget-content\\">
<p>First Paragraph</p>
</div></div>",
}
` );
firstParagraphBlock = await firstWidgetArea.$(
'[data-block][data-type="core/paragraph"]'
);
await firstParagraphBlock.focus();
await showBlockToolbar();
await clickBlockToolbarButton( 'Options' );
const duplicateButton = await find( {
role: 'menuitem',
name: /^Duplicate/,
} );
await duplicateButton.click();
await page.waitForFunction(
( paragraph ) =>
paragraph.nextSibling &&
paragraph.nextSibling.matches( '[data-block]' ),
{},
firstParagraphBlock
);
const duplicatedParagraphBlock = await firstParagraphBlock.evaluateHandle(
( paragraph ) => paragraph.nextSibling
);
const firstParagraphBlockClientId = await firstParagraphBlock.evaluate(
( node ) => node.dataset.block
);
const duplicatedParagraphBlockClientId = await duplicatedParagraphBlock.evaluate(
( node ) => node.dataset.block
);
expect( firstParagraphBlockClientId ).not.toBe(
duplicatedParagraphBlockClientId
);
await expect( duplicatedParagraphBlock ).toMatchQuery( {
text: 'First Paragraph',
} );
await expect( duplicatedParagraphBlock ).toHaveFocus();
await saveWidgets();
const editedSerializedWidgetAreas = await getSerializedWidgetAreas();
expect( editedSerializedWidgetAreas ).toMatchInlineSnapshot( `
Object {
"sidebar-1": "<div class=\\"widget widget_block widget_text\\"><div class=\\"widget-content\\">
<p>First Paragraph</p>
</div></div>
<div class=\\"widget widget_block widget_text\\"><div class=\\"widget-content\\">
<p>First Paragraph</p>
</div></div>",
}
` );
expect( editedSerializedWidgetAreas[ 'sidebar-1' ] ).toBe(
[
initialSerializedWidgetAreas[ 'sidebar-1' ],
initialSerializedWidgetAreas[ 'sidebar-1' ],
].join( '\n' )
);
} );
it.skip( 'Should display legacy widgets', async () => {
// Get the default empty instance of a legacy search widget.
const { instance: defaultSearchInstance } = await rest( {
method: 'POST',
path: '/wp/v2/widget-types/search/encode',
data: { instance: {} },
} );
// Create a search widget in the first sidebar using the default instance.
await rest( {
method: 'POST',
path: '/wp/v2/widgets',
data: {
id_base: 'search',
sidebar: 'sidebar-1',
instance: defaultSearchInstance,
},
} );
await page.reload();
// Wait for the Legacy Widget block's preview iframe to load.
const frame = await new Promise( ( resolve ) => {
const checkFrame = async () => {
const frameElement = await page.$(
'iframe.wp-block-legacy-widget__edit-preview-iframe'
);
if ( frameElement ) {
page.off( 'frameattached', checkFrame );
page.off( 'framenavigated', checkFrame );
resolve( frameElement.contentFrame() );
}
};
page.on( 'frameattached', checkFrame );
page.on( 'framenavigated', checkFrame );
} );
// Expect to have search input.
await find(
{
role: 'searchbox',
},
{
page: frame,
}
);
// Focus the Legacy Widget block.
const legacyWidget = await find( {
role: 'document',
name: 'Block: Legacy Widget',
} );
await legacyWidget.focus();
// There should be a title at the top of the widget form.
const legacyWidgetName = await find(
{
role: 'heading',
level: 3,
},
{
root: legacyWidget,
}
);
expect( legacyWidgetName ).toMatchQuery( { text: 'Search' } );
// Update the widget form.
// TODO: Convert to find() API from puppeteer-testing-library.
const titleInput = await page.waitForSelector(
`#widget-search-1-title`
);
await titleInput.type( 'Search Title' );
// Switch to Navigation mode.
await page.keyboard.press( 'Escape' );
// Wait for the preview to update.
await frame.waitForNavigation();
// Expect to have search title.
await find(
{
role: 'heading',
name: 'Search Title',
},
{
page: frame,
}
);
await saveWidgets();
const serializedWidgetAreas = await getSerializedWidgetAreas();
expect( serializedWidgetAreas ).toMatchInlineSnapshot( `
Object {
"sidebar-1": "<div class=\\"widget widget_search\\"><div class=\\"widget-content\\"><h2 class=\\"widget-title subheading heading-size-3\\">Search Title</h2><form role=\\"search\\" method=\\"get\\" class=\\"search-form\\" action=\\"http://localhost:8889/\\">
<label for=\\"search-form-1\\">
<span class=\\"screen-reader-text\\">Search for:</span>
<input type=\\"search\\" id=\\"search-form-1\\" class=\\"search-field\\" placeholder=\\"Search …\\" value=\\"\\" name=\\"s\\" />
</label>
<input type=\\"submit\\" class=\\"search-submit\\" value=\\"Search\\" />
</form>
</div></div>",
}
` );
} );
it.skip( 'allows widgets to be moved between widget areas using the dropdown in the block toolbar', async () => {
const widgetAreas = await findAll( {
role: 'document',
name: 'Block: Widget Area',
} );
const [ firstWidgetArea, secondWidgetArea ] = widgetAreas;
// Insert a paragraph it should be in the first widget area.
const inserterParagraphBlock = await getBlockInGlobalInserter(
'Paragraph'
);
await inserterParagraphBlock.hover();
await inserterParagraphBlock.click();
const addedParagraphBlockInFirstWidgetArea = await find(
{
role: 'document',
name: /^Empty block/,
},
{ root: firstWidgetArea }
);
await addedParagraphBlockInFirstWidgetArea.focus();
await page.keyboard.type( 'First Paragraph' );
// Check that the block exists in the first widget area.
await find(
{
role: 'document',
name: 'Paragraph block',
value: 'First Paragraph',
},
{
root: firstWidgetArea,
}
);
// Move the block to the second widget area.
await showBlockToolbar();
await clickBlockToolbarButton( 'Move to widget area' );
const widgetAreaButton = await find( {
role: 'menuitemradio',
name: /^Footer #2/,
} );
await widgetAreaButton.click();
// Check that the block exists in the second widget area.
await find(
{
role: 'document',
name: 'Paragraph block',
value: 'First Paragraph',
},
{
root: secondWidgetArea,
}
);
// Assert that the serialized widget areas shows the block as in the second widget area.
await saveWidgets();
const serializedWidgetAreas2 = await getSerializedWidgetAreas();
expect( serializedWidgetAreas2 ).toMatchInlineSnapshot( `
Object {
"sidebar-2": "<div class=\\"widget widget_block widget_text\\"><div class=\\"widget-content\\">
<p>First Paragraph</p>
</div></div>",
}
` );
} );
it( 'Allows widget deletion to be undone', async () => {
const [ firstWidgetArea ] = await findAll( {
role: 'document',
name: 'Block: Widget Area',
} );
let addParagraphBlock = await getBlockInGlobalInserter( 'Paragraph' );
await addParagraphBlock.click();
let addedParagraphBlockInFirstWidgetArea = await find(
{
name: /^Empty block/,
selector: '[data-block][data-type="core/paragraph"]',
},
{
root: firstWidgetArea,
}
);
await addedParagraphBlockInFirstWidgetArea.focus();
await page.keyboard.type( 'First Paragraph' );
addParagraphBlock = await getBlockInGlobalInserter( 'Paragraph' );
await addParagraphBlock.click();
addedParagraphBlockInFirstWidgetArea = await firstWidgetArea.$(
'[data-block][data-type="core/paragraph"][aria-label^="Empty block"]'
);
await addedParagraphBlockInFirstWidgetArea.focus();
await page.keyboard.type( 'Second Paragraph' );
await saveWidgets();
await page.focus( '.block-editor-writing-flow' );
// Delete the last block and save again.
await pressKeyWithModifier( 'access', 'z' );
await saveWidgets();
// To do: clicking on the Snackbar causes focus loss.
await page.focus( '.block-editor-writing-flow' );
// Undo block deletion and save again
await pressKeyWithModifier( 'primary', 'z' );
await saveWidgets();
// Reload the page to make sure changes were actually saved.
await page.reload();
const serializedWidgetAreas = await getSerializedWidgetAreas();
expect( serializedWidgetAreas ).toMatchInlineSnapshot( `
Object {
"sidebar-1": "<div class=\\"widget widget_block widget_text\\"><div class=\\"widget-content\\">
<p>First Paragraph</p>
</div></div>
<div class=\\"widget widget_block widget_text\\"><div class=\\"widget-content\\">
<p>Second Paragraph</p>
</div></div>",
}
` );
} );
it( 'can toggle sidebar list view', async () => {
const widgetAreas = await findAll( {
role: 'document',
name: 'Block: Widget Area',
} );
await openListView();
const listItems = await page.$$(
'.edit-widgets-editor__list-view-panel .block-editor-list-view-leaf'
);
expect( listItems.length >= widgetAreas.length ).toEqual( true );
await closeListView();
} );
// Check for regressions of https://github.com/WordPress/gutenberg/issues/38002.
it( 'allows blocks to be added on mobile viewports', async () => {
await setBrowserViewport( 'small' );
const [ firstWidgetArea ] = await findAll( {
role: 'document',
name: 'Block: Widget Area',
} );
const addParagraphBlock = await getBlockInGlobalInserter( 'Paragraph' );
await addParagraphBlock.click();
const addedParagraphBlockInFirstWidgetArea = await find(
{
name: /^Empty block/,
selector: '[data-block][data-type="core/paragraph"]',
},
{
root: firstWidgetArea,
}
);
await addedParagraphBlockInFirstWidgetArea.focus();
await page.keyboard.type( 'First Paragraph' );
const updatedParagraphBlockInFirstWidgetArea = await find(
{
name: 'Paragraph block',
value: 'First Paragraph',
},
{
root: firstWidgetArea,
}
);
expect( updatedParagraphBlockInFirstWidgetArea ).toBeTruthy();
} );
} );
/**
* Visit the widgets screen via link clicking. The widgets screen currently
* has different URLs during the integration to core.
* We should be able to refactor it once it's fully merged into core.
*/
async function visitWidgetsScreen() {
// Visit the Appearance page.
await visitAdminPage( 'themes.php' );
// Go to the Widgets page.
const appearanceMenu = await page.$( '#menu-appearance' );
await appearanceMenu.hover();
const widgetsLink = await find(
{ role: 'link', name: 'Widgets' },
{ root: appearanceMenu }
);
await Promise.all( [
page.waitForNavigation(),
// Click on the link no matter if it's visible or not.
widgetsLink.evaluate( ( link ) => {
link.click();
} ),
] );
}
async function saveWidgets() {
await closeListView();
await closeGlobalBlockInserter();
const updateButton = await find( {
role: 'button',
name: 'Update',
} );
await updateButton.click();
// Expect the "Widgets saved." snackbar to appear.
const savedSnackbarQuery = {
role: 'button',
name: 'Dismiss this notice',
text: 'Widgets saved.',
};
await expect( savedSnackbarQuery ).toBeFound();
// Close the snackbar.
const savedSnackbar = await find( savedSnackbarQuery );
await savedSnackbar.click();
// Expect focus not to be lost.
await expect(
await page.evaluate( () => document.activeElement.className )
).toBe( 'components-snackbar-list edit-widgets-notices__snackbar' );
await expect( savedSnackbarQuery ).not.toBeFound();
}
async function getSerializedWidgetAreas() {
const widgets = await rest( { path: '/wp/v2/widgets' } );
const serializedWidgetAreas = mapValues(
groupBy( widgets, 'sidebar' ),
( sidebarWidgets ) =>
sidebarWidgets
.map( ( widget ) => widget.rendered )
.filter( Boolean )
.join( '\n' )
);
return serializedWidgetAreas;
}