-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
1189 lines (1002 loc) · 36.9 KB
/
index.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
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
/**
* External dependencies
*/
import { render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
/**
* Internal dependencies
*/
import { ToolsPanel, ToolsPanelContext, ToolsPanelItem } from '../';
import { createSlotFill, Provider as SlotFillProvider } from '../../slot-fill';
const { Fill: ToolsPanelItems, Slot } = createSlotFill( 'ToolsPanelSlot' );
const resetAll = jest.fn();
const noop = () => undefined;
// Default props for the tools panel.
const defaultProps = {
label: 'Panel header',
resetAll,
};
// Default props for an enabled control to be rendered within panel.
const controlProps = {
attributes: { value: true },
hasValue: jest.fn().mockImplementation( () => {
return !! controlProps.attributes.value;
} ),
label: 'Example',
onDeselect: jest.fn().mockImplementation( () => {
controlProps.attributes.value = undefined;
} ),
onSelect: jest.fn(),
};
// Default props without a value for an alternate control to be rendered within
// the panel.
const altControlProps = {
attributes: { value: false },
hasValue: jest.fn().mockImplementation( () => {
return !! altControlProps.attributes.value;
} ),
label: 'Alt',
onDeselect: jest.fn(),
onSelect: jest.fn(),
};
// Default props for wrapped or grouped panel items.
const nestedControlProps = {
attributes: { value: true },
hasValue: jest.fn().mockImplementation( () => {
return !! nestedControlProps.attributes.value;
} ),
label: 'Nested Control 1',
onDeselect: jest.fn().mockImplementation( () => {
nestedControlProps.attributes.value = undefined;
} ),
onSelect: jest.fn(),
isShownByDefault: true,
};
// Alternative props for wrapped or grouped panel items.
const altNestedControlProps = {
attributes: { value: false },
hasValue: jest.fn().mockImplementation( () => {
return !! altNestedControlProps.attributes.value;
} ),
label: 'Nested Control 2',
onDeselect: jest.fn(),
onSelect: jest.fn(),
};
// Simple custom component grouping panel items. Used to test panel item
// registration and display when not an immediate child of `ToolsPanel`.
const GroupedItems = ( {
defaultGroupedProps = nestedControlProps,
altGroupedProps = altNestedControlProps,
} ) => {
return (
<>
<ToolsPanelItem { ...defaultGroupedProps }>
<div>Grouped control</div>
</ToolsPanelItem>
<ToolsPanelItem { ...altGroupedProps }>
<div>Alt grouped control</div>
</ToolsPanelItem>
</>
);
};
// This context object is used to help simulate different scenarios in which
// `ToolsPanelItem` registration or deregistration requires testing.
const panelContext = {
panelId: '1234',
menuItems: {
default: {},
optional: { [ altControlProps.label ]: true },
},
hasMenuItems: false,
isResetting: false,
shouldRenderPlaceholderItems: false,
registerPanelItem: jest.fn(),
deregisterPanelItem: jest.fn(),
flagItemCustomization: noop,
areAllOptionalControlsHidden: true,
};
// Renders a tools panel including panel items that have been grouped within
// a custom component.
const renderGroupedItemsInPanel = () => {
return render(
<ToolsPanel { ...defaultProps }>
<GroupedItems />
</ToolsPanel>
);
};
// Custom component rendering a panel item within a wrapping element. Also used
// to test panel item registration and rendering.
const WrappedItem = ( { text, ...props } ) => {
return (
<div>
<span>Wrapper</span>
<ToolsPanelItem { ...props }>
<div>{ text }</div>
</ToolsPanelItem>
</div>
);
};
// Renders a `ToolsPanel` with single wrapped panel item via a custom component.
const renderWrappedItemInPanel = () => {
return render(
<ToolsPanel { ...defaultProps }>
<WrappedItem { ...nestedControlProps } text="Wrapped 1" />
<WrappedItem { ...altNestedControlProps } text="Wrapped 2" />
</ToolsPanel>
);
};
// Renders a default tools panel including children that are
// not to be represented within the panel's menu.
const renderPanel = () => {
return render(
<ToolsPanel { ...defaultProps }>
{ false && <div>Hidden</div> }
<ToolsPanelItem { ...controlProps }>
<div>Example control</div>
</ToolsPanelItem>
<ToolsPanelItem { ...altControlProps }>
<div>Alt control</div>
</ToolsPanelItem>
<span>Visible</span>
</ToolsPanel>
);
};
/**
* Retrieves the panel's dropdown menu toggle button.
*
* @return {HTMLElement} The menu button.
*/
const getMenuButton = () => {
return screen.getByRole( 'button', {
name: /Panel([\w\s]+)header([\w\s]+)options/i,
} );
};
/**
* Helper to find the menu button and simulate a user click.
*
*/
const openDropdownMenu = async () => {
const user = userEvent.setup();
const menuButton = getMenuButton();
await user.click( menuButton );
return menuButton;
};
// Opens dropdown then selects the menu item by label before simulating a click.
const selectMenuItem = async ( label ) => {
const user = userEvent.setup();
const menuItem = await screen.findByText( label );
await user.click( menuItem );
};
describe( 'ToolsPanel', () => {
afterEach( () => {
controlProps.attributes.value = true;
altControlProps.attributes.value = false;
} );
describe( 'basic rendering', () => {
it( 'should render panel', () => {
renderPanel();
const menuButton = getMenuButton();
const label = screen.getByText( defaultProps.label );
const control = screen.getByText( 'Example control' );
const nonToolsPanelItem = screen.getByText( 'Visible' );
expect( menuButton ).toBeInTheDocument();
expect( label ).toBeInTheDocument();
expect( control ).toBeInTheDocument();
expect( nonToolsPanelItem ).toBeInTheDocument();
} );
it( 'should render panel item flagged as default control even without value', () => {
render(
<ToolsPanel { ...defaultProps }>
<ToolsPanelItem { ...controlProps }>
<div>Example control</div>
</ToolsPanelItem>
<ToolsPanelItem
{ ...altControlProps }
isShownByDefault={ true }
>
<div>Alt control</div>
</ToolsPanelItem>
</ToolsPanel>
);
const altControl = screen.getByText( 'Alt control' );
expect( altControl ).toBeInTheDocument();
} );
it( 'should not render panel menu when there are no panel items', () => {
render(
<ToolsPanel { ...defaultProps }>
{ false && (
<ToolsPanelItem>Should not show</ToolsPanelItem>
) }
{ false && (
<ToolsPanelItem>Not shown either</ToolsPanelItem>
) }
<span>Visible but insignificant</span>
</ToolsPanel>
);
const menu = screen.queryByLabelText( defaultProps.label );
expect( menu ).not.toBeInTheDocument();
} );
it( 'should render panel menu when at least one panel item', async () => {
renderPanel();
const menuButton = await openDropdownMenu();
expect( menuButton ).toBeInTheDocument();
} );
it( 'should render reset all item in menu', async () => {
renderPanel();
await openDropdownMenu();
const resetAllItem = await screen.findByRole( 'menuitem' );
expect( resetAllItem ).toBeInTheDocument();
} );
it( 'should render panel menu items correctly', async () => {
renderPanel();
await openDropdownMenu();
const menuItems = await screen.findAllByRole( 'menuitemcheckbox' );
expect( menuItems.length ).toEqual( 2 );
expect( menuItems[ 0 ] ).toHaveAttribute( 'aria-checked', 'true' );
expect( menuItems[ 1 ] ).toHaveAttribute( 'aria-checked', 'false' );
} );
it( 'should render panel label as header text', () => {
renderPanel();
const header = screen.getByText( defaultProps.label );
expect( header ).toBeInTheDocument();
} );
} );
describe( 'conditional rendering of panel items', () => {
it( 'should render panel item when it has a value', () => {
renderPanel();
const exampleControl = screen.getByText( 'Example control' );
const altControl = screen.queryByText( 'Alt control' );
expect( exampleControl ).toBeInTheDocument();
expect( altControl ).not.toBeInTheDocument();
} );
it( 'should render panel item when corresponding menu item is selected', async () => {
renderPanel();
await openDropdownMenu();
await selectMenuItem( altControlProps.label );
const control = await screen.findByText( 'Alt control' );
expect( control ).toBeInTheDocument();
// Test the aria live announcement.
const announcement = screen.getByText( 'Alt is now visible' );
expect( announcement ).toHaveAttribute( 'aria-live', 'assertive' );
} );
it( 'should prevent optional panel item rendering when toggled off via menu item', async () => {
renderPanel();
await openDropdownMenu();
await selectMenuItem( controlProps.label );
const control = screen.queryByText( 'Example control' );
expect( control ).not.toBeInTheDocument();
// Test the aria live announcement.
const announcement = screen.getByText(
'Example hidden and reset to default'
);
expect( announcement ).toHaveAttribute( 'aria-live', 'assertive' );
} );
it( 'should render optional panel item when value is updated externally and panel has an ID', async () => {
const ToolsPanelOptional = ( { toolsPanelItemValue } ) => {
const itemProps = {
attributes: { value: toolsPanelItemValue },
hasValue: () => !! toolsPanelItemValue,
label: 'Alt',
onDeselect: jest.fn(),
onSelect: jest.fn(),
};
return (
<ToolsPanel { ...defaultProps }>
<ToolsPanelItem { ...itemProps }>
<div>Optional control</div>
</ToolsPanelItem>
</ToolsPanel>
);
};
const { rerender } = render( <ToolsPanelOptional /> );
const control = screen.queryByText( 'Optional control' );
expect( control ).not.toBeInTheDocument();
rerender( <ToolsPanelOptional toolsPanelItemValue={ 100 } /> );
const controlRerendered = screen.getByText( 'Optional control' );
expect( controlRerendered ).toBeInTheDocument();
} );
it( 'should render optional item when value is updated externally and panelId is null', async () => {
// This test partially covers: https://github.com/WordPress/gutenberg/issues/47368
const ToolsPanelOptional = ( { toolsPanelItemValue } ) => {
const itemProps = {
attributes: { value: toolsPanelItemValue },
hasValue: () => !! toolsPanelItemValue,
label: 'Alt',
onDeselect: jest.fn(),
onSelect: jest.fn(),
};
// The null panelId below simulates the panel prop when there
// are multiple blocks selected.
return (
<ToolsPanel { ...defaultProps } panelId={ null }>
<ToolsPanelItem { ...itemProps }>
<div>Optional control</div>
</ToolsPanelItem>
</ToolsPanel>
);
};
const { rerender } = render( <ToolsPanelOptional /> );
const control = screen.queryByText( 'Optional control' );
expect( control ).not.toBeInTheDocument();
rerender( <ToolsPanelOptional toolsPanelItemValue={ 99 } /> );
const controlRerendered = screen.getByText( 'Optional control' );
expect( controlRerendered ).toBeInTheDocument();
} );
it( 'should continue to render shown by default item after it is toggled off via menu item', async () => {
render(
<ToolsPanel { ...defaultProps }>
<ToolsPanelItem
{ ...controlProps }
isShownByDefault={ true }
>
<div>Default control</div>
</ToolsPanelItem>
</ToolsPanel>
);
const control = screen.getByText( 'Default control' );
expect( control ).toBeInTheDocument();
await openDropdownMenu();
await selectMenuItem( controlProps.label );
const resetControl = screen.getByText( 'Default control' );
expect( resetControl ).toBeInTheDocument();
// Test the aria live announcement.
const announcement = screen.getByText( 'Example reset to default' );
expect( announcement ).toHaveAttribute( 'aria-live', 'assertive' );
} );
it( 'should render appropriate menu groups', async () => {
render(
<ToolsPanel { ...defaultProps }>
<ToolsPanelItem
{ ...controlProps }
isShownByDefault={ true }
>
<div>Default control</div>
</ToolsPanelItem>
<ToolsPanelItem { ...altControlProps }>
<div>Optional control</div>
</ToolsPanelItem>
</ToolsPanel>
);
await openDropdownMenu();
const menuGroups = screen.getAllByRole( 'group' );
// Groups should be: default controls, optional controls & reset all.
expect( menuGroups.length ).toEqual( 3 );
} );
it( 'should not render contents of items when in placeholder state', () => {
render(
<ToolsPanel
{ ...defaultProps }
shouldRenderPlaceholderItems={ true }
>
<ToolsPanelItem { ...altControlProps }>
<div>Optional control</div>
</ToolsPanelItem>
</ToolsPanel>
);
const optionalItem = screen.queryByText( 'Optional control' );
// When rendered as a placeholder a ToolsPanelItem will just omit
// all the item's children. So the container element will still be
// there holding its position but the inner text etc should not be
// there.
expect( optionalItem ).not.toBeInTheDocument();
} );
it( 'should render default controls with conditional isShownByDefault', async () => {
const linkedControlProps = {
attributes: { value: false },
hasValue: jest.fn().mockImplementation( () => {
return !! linkedControlProps.attributes.value;
} ),
label: 'Linked',
onDeselect: jest.fn(),
onSelect: jest.fn(),
};
const TestPanel = () => (
<ToolsPanel { ...defaultProps }>
<ToolsPanelItem
{ ...altControlProps }
isShownByDefault={ true }
>
<div>Default control</div>
</ToolsPanelItem>
<ToolsPanelItem
{ ...linkedControlProps }
isShownByDefault={ !! altControlProps.attributes.value }
>
<div>Linked control</div>
</ToolsPanelItem>
</ToolsPanel>
);
const { rerender } = render( <TestPanel /> );
// The linked control should start out as an optional control and is
// not rendered because it does not have a value.
let linkedItem = screen.queryByText( 'Linked control' );
expect( linkedItem ).not.toBeInTheDocument();
await openDropdownMenu();
// The linked control should initially appear in the optional controls
// menu group. There should be three menu groups: default controls,
// optional controls, and the group to reset all options.
let menuGroups = screen.getAllByRole( 'group' );
expect( menuGroups.length ).toEqual( 3 );
// The linked control should be in the second group, of optional controls.
let optionalItem = within( menuGroups[ 1 ] ).getByText( 'Linked' );
expect( optionalItem ).toBeInTheDocument();
// Simulate the main control having a value set which should
// trigger the linked control becoming a default control via the
// conditional `isShownByDefault` prop.
altControlProps.attributes.value = true;
rerender( <TestPanel /> );
// The linked control should now be a default control and rendered
// despite not having a value.
linkedItem = screen.getByText( 'Linked control' );
expect( linkedItem ).toBeInTheDocument();
// The linked control should now appear in the default controls
// menu group and have been removed from the optional group.
menuGroups = screen.getAllByRole( 'group' );
// There should now only be two groups. The default controls and
// and the group for the reset all option.
expect( menuGroups.length ).toEqual( 2 );
// The new default control item for the Linked control should be
// within the first menu group.
const defaultItem = within( menuGroups[ 0 ] ).getByText( 'Linked' );
expect( defaultItem ).toBeInTheDocument();
// Optional controls have an additional aria-label. This can be used
// to confirm the conditional default control has been removed from
// the optional menu item group.
optionalItem = screen.queryByRole( 'menuitemcheckbox', {
name: 'Show Linked',
} );
expect( optionalItem ).not.toBeInTheDocument();
} );
it( 'should handle conditionally rendered default control', async () => {
const conditionalControlProps = {
attributes: { value: false },
hasValue: jest.fn().mockImplementation( () => {
return !! conditionalControlProps.attributes.value;
} ),
label: 'Conditional',
onDeselect: jest.fn(),
onSelect: jest.fn(),
};
const TestPanel = () => (
<ToolsPanel { ...defaultProps }>
<ToolsPanelItem
{ ...altControlProps }
isShownByDefault={ true }
>
<div>Default control</div>
</ToolsPanelItem>
{ !! altControlProps.attributes.value && (
<ToolsPanelItem
{ ...conditionalControlProps }
isShownByDefault={ true }
>
<div>Conditional control</div>
</ToolsPanelItem>
) }
</ToolsPanel>
);
const { rerender } = render( <TestPanel /> );
// The conditional control should not yet be rendered.
let conditionalItem = screen.queryByText( 'Conditional control' );
expect( conditionalItem ).not.toBeInTheDocument();
// The conditional control should not yet appear in the default controls
// menu group.
await openDropdownMenu();
let menuGroups = screen.getAllByRole( 'group' );
let defaultItem = within( menuGroups[ 0 ] ).queryByText(
'Conditional'
);
expect( defaultItem ).not.toBeInTheDocument();
// Simulate the main control having a value set which will now
// render the new default control into the ToolsPanel.
altControlProps.attributes.value = true;
rerender( <TestPanel /> );
// The conditional control should now be rendered and included in
// the panel's menu.
conditionalItem = screen.getByText( 'Conditional control' );
expect( conditionalItem ).toBeInTheDocument();
// The conditional control should now appear in the default controls
// menu group.
menuGroups = screen.getAllByRole( 'group' );
// The new default control item for the Conditional control should
// be within the first menu group.
defaultItem = within( menuGroups[ 0 ] ).getByText( 'Conditional' );
expect( defaultItem ).toBeInTheDocument();
} );
} );
describe( 'registration of panel items', () => {
beforeEach( () => {
jest.clearAllMocks();
} );
it( 'should register and deregister items when panelId changes', () => {
// This test simulates switching block selection, which causes the
// `ToolsPanel` to rerender with a new panelId, necessitating the
// registration and deregistration of appropriate `ToolsPanelItem`
// children.
//
// When the `panelId` changes, only items matching the new ID register
// themselves, while those for the old panelId deregister.
//
// See: https://github.com/WordPress/gutenberg/pull/36588
const context = { ...panelContext };
const TestPanel = () => (
<ToolsPanelContext.Provider value={ context }>
<ToolsPanelItem { ...altControlProps } panelId="1234">
<div>Item</div>
</ToolsPanelItem>
</ToolsPanelContext.Provider>
);
// On the initial render of the panel, the ToolsPanelItem should
// be registered.
const { rerender } = render( <TestPanel /> );
expect( context.registerPanelItem ).toHaveBeenCalledWith(
expect.objectContaining( {
label: altControlProps.label,
panelId: '1234',
} )
);
expect( context.deregisterPanelItem ).not.toHaveBeenCalled();
// Simulate a change in panel, e.g. a switch of block selection.
context.panelId = '4321';
context.menuItems.optional[ altControlProps.label ] = false;
// Rerender the panel item. Because we have a new panelId, this
// panelItem should NOT be registered, but it SHOULD be
// deregistered.
rerender( <TestPanel /> );
// registerPanelItem has still only been called once.
expect( context.registerPanelItem ).toHaveBeenCalledTimes( 1 );
// deregisterPanelItem is called, given that we have switched panels.
expect( context.deregisterPanelItem ).toHaveBeenCalledWith(
altControlProps.label
);
// Simulate switching back to the original panelId, e.g. by selecting
// the original block again.
context.panelId = '1234';
context.menuItems.optional[ altControlProps.label ] = true;
// Rerender the panel and ensure that the panelItem is registered
// again, and it is not de-registered.
rerender( <TestPanel /> );
expect( context.registerPanelItem ).toHaveBeenCalledWith(
expect.objectContaining( {
label: altControlProps.label,
panelId: '1234',
} )
);
expect( context.registerPanelItem ).toHaveBeenCalledTimes( 2 );
// deregisterPanelItem has still only been called once.
expect( context.deregisterPanelItem ).toHaveBeenCalledTimes( 1 );
} );
it( 'should register items when ToolsPanel panelId is null', () => {
// This test simulates when a panel spans multiple block selections.
// Multi-selection means a panel can't have a single id to match
// against the item's. Instead the panel gets an id of `null` and
// individual items should still render themselves in this case.
//
// See: https://github.com/WordPress/gutenberg/pull/37216
const context = { ...panelContext, panelId: null };
const TestPanel = () => (
<ToolsPanelContext.Provider value={ context }>
<ToolsPanelItem { ...altControlProps } panelId="1234">
<div>Item</div>
</ToolsPanelItem>
</ToolsPanelContext.Provider>
);
// On the initial render of the panel, the ToolsPanelItem should
// be registered.
const { rerender, unmount } = render( <TestPanel /> );
expect( context.registerPanelItem ).toHaveBeenCalledWith(
expect.objectContaining( {
label: altControlProps.label,
panelId: '1234',
} )
);
expect( context.deregisterPanelItem ).not.toHaveBeenCalled();
// Simulate a further block selection being added to the
// multi-selection. The panelId will remain `null` in this case.
rerender( <TestPanel /> );
expect( context.registerPanelItem ).toHaveBeenCalledTimes( 1 );
expect( context.deregisterPanelItem ).not.toHaveBeenCalled();
// Simulate a change in panel back to single block selection for
// which the item matches panelId.
context.panelId = '1234';
rerender( <TestPanel /> );
expect( context.registerPanelItem ).toHaveBeenCalledTimes( 1 );
expect( context.deregisterPanelItem ).not.toHaveBeenCalled();
// Simulate another multi-selection where the panelId is `null`.
// Item should re-register itself after it deregistered as the
// multi-selection occurred.
context.panelId = null;
rerender( <TestPanel /> );
expect( context.registerPanelItem ).toHaveBeenCalledTimes( 2 );
expect( context.deregisterPanelItem ).toHaveBeenCalledTimes( 1 );
// Simulate a change in panel e.g. back to a single block selection
// Where the item's panelId is not a match.
context.panelId = '4321';
rerender( <TestPanel /> );
// As the item no longer matches the panelId it should not have
// registered again but instead deregistered.
unmount();
expect( context.registerPanelItem ).toHaveBeenCalledTimes( 2 );
expect( context.deregisterPanelItem ).toHaveBeenCalledTimes( 2 );
} );
} );
describe( 'callbacks on menu item selection', () => {
beforeEach( () => {
jest.clearAllMocks();
} );
it( 'should call onDeselect callback when menu item is toggled off', async () => {
renderPanel();
await openDropdownMenu();
await selectMenuItem( controlProps.label );
expect( controlProps.onSelect ).not.toHaveBeenCalled();
expect( controlProps.onDeselect ).toHaveBeenCalledTimes( 1 );
} );
it( 'should call onSelect callback when menu item is toggled on', async () => {
renderPanel();
await openDropdownMenu();
await selectMenuItem( altControlProps.label );
expect( altControlProps.onSelect ).toHaveBeenCalledTimes( 1 );
expect( altControlProps.onDeselect ).not.toHaveBeenCalled();
} );
it( 'should call resetAll callback when its menu item is selected', async () => {
renderPanel();
await openDropdownMenu();
await selectMenuItem( 'Reset all' );
expect( resetAll ).toHaveBeenCalledTimes( 1 );
expect( controlProps.onSelect ).not.toHaveBeenCalled();
expect( controlProps.onDeselect ).not.toHaveBeenCalled();
expect( altControlProps.onSelect ).not.toHaveBeenCalled();
expect( altControlProps.onDeselect ).not.toHaveBeenCalled();
} );
// This confirms the internal `isResetting` state when resetting all
// controls does not prevent subsequent individual reset requests.
// i.e. onDeselect callbacks are called correctly after a resetAll.
it( 'should call onDeselect after previous reset all', async () => {
renderPanel();
await openDropdownMenu();
await selectMenuItem( 'Reset all' ); // Initial control is displayed by default.
await selectMenuItem( controlProps.label ); // Re-display control.
expect( controlProps.onDeselect ).not.toHaveBeenCalled();
await selectMenuItem( controlProps.label ); // Reset control.
expect( controlProps.onDeselect ).toHaveBeenCalled();
} );
} );
describe( 'grouped panel items within custom components', () => {
it( 'should render grouped items correctly', () => {
renderGroupedItemsInPanel();
const defaultItem = screen.getByText( 'Grouped control' );
const altItem = screen.queryByText( 'Alt grouped control' );
expect( defaultItem ).toBeInTheDocument();
expect( altItem ).not.toBeInTheDocument();
} );
it( 'should render grouped items within the menu', async () => {
renderGroupedItemsInPanel();
await openDropdownMenu();
const defaultItem = screen.getByText( 'Nested Control 1' );
const defaultMenuItem = screen.getByRole( 'menuitem', {
name: 'Reset Nested Control 1',
} );
const altItem = screen.getByText( 'Nested Control 2' );
const altMenuItem = screen.getByRole( 'menuitemcheckbox', {
name: 'Show Nested Control 2',
checked: false,
} );
expect( defaultItem ).toBeInTheDocument();
expect( defaultMenuItem ).toBeInTheDocument();
expect( altItem ).toBeInTheDocument();
expect( altMenuItem ).toBeInTheDocument();
} );
} );
describe( 'wrapped panel items within custom components', () => {
it( 'should render wrapped items correctly', () => {
renderWrappedItemInPanel();
const wrappers = screen.getAllByText( 'Wrapper' );
const defaultItem = screen.getByText( 'Wrapped 1' );
const altItem = screen.queryByText( 'Wrapped 2' );
// Both wrappers should be rendered but only the panel item
// displayed by default should be within the document.
expect( wrappers.length ).toEqual( 2 );
expect( defaultItem ).toBeInTheDocument();
expect( altItem ).not.toBeInTheDocument();
} );
it( 'should render wrapped items within the menu', async () => {
renderWrappedItemInPanel();
await openDropdownMenu();
const defaultItem = screen.getByText( 'Nested Control 1' );
const defaultMenuItem = screen.getByRole( 'menuitem', {
name: 'Reset Nested Control 1',
} );
const altItem = screen.getByText( 'Nested Control 2' );
const altMenuItem = screen.getByRole( 'menuitemcheckbox', {
name: 'Show Nested Control 2',
checked: false,
} );
expect( defaultItem ).toBeInTheDocument();
expect( defaultMenuItem ).toBeInTheDocument();
expect( altItem ).toBeInTheDocument();
expect( altMenuItem ).toBeInTheDocument();
} );
} );
describe( 'rendering via SlotFills', () => {
beforeEach( () => {
jest.clearAllMocks();
} );
it( 'should maintain visual order of controls when toggled on and off', async () => {
// Multiple fills are added to better simulate panel items being
// injected from different locations.
render(
<SlotFillProvider>
<ToolsPanelItems>
<ToolsPanelItem { ...altControlProps }>
<div>Item 1</div>
</ToolsPanelItem>
</ToolsPanelItems>
<ToolsPanelItems>
<ToolsPanelItem { ...controlProps }>
<div>Item 2</div>
</ToolsPanelItem>
</ToolsPanelItems>
<ToolsPanel { ...defaultProps }>
<Slot />
</ToolsPanel>
</SlotFillProvider>
);
// Only the second item should be shown initially as it has a value.
const firstItem = screen.queryByText( 'Item 1' );
const secondItem = screen.getByText( 'Item 2' );
expect( firstItem ).not.toBeInTheDocument();
expect( secondItem ).toBeInTheDocument();
// Toggle on the first item.
await openDropdownMenu();
await selectMenuItem( altControlProps.label );
// The order of items should be as per their original source order.
let items = screen.getAllByText( /Item [1-2]/ );
expect( items ).toHaveLength( 2 );
expect( items[ 0 ] ).toHaveTextContent( 'Item 1' );
expect( items[ 1 ] ).toHaveTextContent( 'Item 2' );
// Then toggle off both items.
await selectMenuItem( controlProps.label );
await selectMenuItem( altControlProps.label );
// Toggle on controls again and ensure order remains.
await selectMenuItem( controlProps.label );
await selectMenuItem( altControlProps.label );
items = screen.getAllByText( /Item [1-2]/ );
expect( items ).toHaveLength( 2 );
expect( items[ 0 ] ).toHaveTextContent( 'Item 1' );
expect( items[ 1 ] ).toHaveTextContent( 'Item 2' );
} );
it( 'should not trigger callback when fill has not updated yet when panel has', () => {
// Fill provided controls can update independently to the panel.
// A `panelId` prop was added to both panels and items
// so it could prevent erroneous registrations and calls to
// `onDeselect` etc.
//
// See: https://github.com/WordPress/gutenberg/pull/35375
//
// This test simulates this issue by rendering an item within a
// contrived `ToolsPanelContext` to reflect the changes the panel
// item needs to protect against.
const context = {
panelId: '1234',
menuItems: {
default: {},
optional: { [ altControlProps.label ]: true },
},
hasMenuItems: false,
isResetting: false,
shouldRenderPlaceholderItems: false,
registerPanelItem: noop,
deregisterPanelItem: noop,
flagItemCustomization: noop,
areAllOptionalControlsHidden: true,
};
// This initial render gives the tools panel item a chance to
// set its internal state to reflect it was previously selected.
// This later forms part of the condition used to determine if an
// item is being deselected and thus call the onDeselect callback.
const { rerender } = render(
<ToolsPanelContext.Provider value={ context }>
<ToolsPanelItem { ...altControlProps } panelId="1234">
<div>Item</div>
</ToolsPanelItem>
</ToolsPanelContext.Provider>
);
// Simulate a change in panel separate to the rendering of fills.
// e.g. a switch of block selection.
context.panelId = '4321';
context.menuItems.optional[ altControlProps.label ] = false;
// Rerender the panel item and ensure that it skips any check
// for deselection given it still belongs to a different panelId.
rerender(
<ToolsPanelContext.Provider value={ context }>
<ToolsPanelItem { ...altControlProps } panelId="1234">
<div>Item</div>
</ToolsPanelItem>
</ToolsPanelContext.Provider>
);
expect( altControlProps.onDeselect ).not.toHaveBeenCalled();
} );
it( 'should not contain orphaned menu items when panelId changes', async () => {
// As fills and the panel can update independently this aims to
// test that no orphaned items appear registered in the panel menu.
//
// See: https://github.com/WordPress/gutenberg/pull/34085
const TestSlotFillPanel = ( { panelId } ) => (
<SlotFillProvider>
<ToolsPanelItems>
<ToolsPanelItem { ...altControlProps } panelId="1234">
<div>Item 1</div>
</ToolsPanelItem>
</ToolsPanelItems>
<ToolsPanelItems>
<ToolsPanelItem { ...controlProps } panelId="9999">
<div>Item 2</div>
</ToolsPanelItem>
</ToolsPanelItems>
<ToolsPanel { ...defaultProps } panelId={ panelId }>
<Slot />
</ToolsPanel>
</SlotFillProvider>
);