-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
389 lines (319 loc) · 11.1 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
/**
* External dependencies
*/
import { render, screen, fireEvent } from '@testing-library/react';
/**
* Internal dependencies
*/
import { ToolsPanel, ToolsPanelItem } from '../';
const resetAll = jest.fn();
// Default props for the tools panel.
const defaultProps = {
header: 'Panel header',
label: 'Display options',
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>
</>
);
};
// 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 className="wrapped-panel-item-container">
<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>
);
};
// Attempts to find the tools panel via its CSS class.
const getPanel = ( container ) =>
container.querySelector( '.components-tools-panel' );
// 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>
);
};
// Helper to find the menu button and simulate a user click.
const openDropdownMenu = () => {
const menuButton = screen.getByLabelText( defaultProps.label );
fireEvent.click( menuButton );
};
// Opens dropdown then selects the menu item by label before simulating a click.
const selectMenuItem = async ( label ) => {
openDropdownMenu();
const menuItem = await screen.findByText( label );
fireEvent.click( menuItem );
};
describe( 'ToolsPanel', () => {
describe( 'basic rendering', () => {
it( 'should render panel', () => {
const { container } = renderPanel();
expect( getPanel( container ) ).toBeInTheDocument();
} );
it( 'should render non panel item child', () => {
renderPanel();
const nonPanelItem = screen.queryByText( 'Visible' );
expect( nonPanelItem ).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', () => {
renderPanel();
const menuButton = screen.getByLabelText( defaultProps.label );
expect( menuButton ).toBeInTheDocument();
} );
it( 'should render reset all item in menu', async () => {
renderPanel();
openDropdownMenu();
const resetAllItem = await screen.findByRole( 'menuitem' );
expect( resetAllItem ).toBeInTheDocument();
} );
it( 'should render panel menu items correctly', async () => {
renderPanel();
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 header', () => {
renderPanel();
const header = screen.getByText( defaultProps.header );
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 selectMenuItem( altControlProps.label );
const control = await screen.findByText( 'Alt control' );
expect( control ).toBeInTheDocument();
} );
it( 'should prevent panel item rendering when toggled off via menu item', async () => {
renderPanel();
await selectMenuItem( controlProps.label );
const control = screen.queryByText( 'Example control' );
expect( control ).not.toBeInTheDocument();
} );
it( 'should prevent shown by default item rendering when 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 selectMenuItem( controlProps.label );
const resetControl = screen.queryByText( 'Default control' );
expect( resetControl ).not.toBeInTheDocument();
} );
} );
describe( 'callbacks on menu item selection', () => {
beforeEach( () => {
jest.clearAllMocks();
controlProps.attributes.value = true;
} );
it( 'should call onDeselect callback when menu item is toggled off', async () => {
renderPanel();
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 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 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();
} );
} );
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();
openDropdownMenu();
const defaultItem = screen.getByText( 'Nested Control 1' );
const defaultMenuItem = defaultItem.parentNode;
const altItem = screen.getByText( 'Nested Control 2' );
const altMenuItem = altItem.parentNode;
expect( defaultItem ).toBeInTheDocument();
expect( defaultMenuItem ).toHaveAttribute( 'aria-checked', 'true' );
expect( altItem ).toBeInTheDocument();
expect( altMenuItem ).toHaveAttribute( 'aria-checked', 'false' );
} );
} );
describe( 'wrapped panel items within custom components', () => {
it( 'should render wrapped items correctly', () => {
const { container } = renderWrappedItemInPanel();
const wrappers = container.querySelectorAll(
'.wrapped-panel-item-container'
);
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', () => {
renderWrappedItemInPanel();
openDropdownMenu();
const defaultItem = screen.getByText( 'Nested Control 1' );
const defaultMenuItem = defaultItem.parentNode;
const altItem = screen.getByText( 'Nested Control 2' );
const altMenuItem = altItem.parentNode;
expect( defaultItem ).toBeInTheDocument();
expect( defaultMenuItem ).toHaveAttribute( 'aria-checked', 'true' );
expect( altItem ).toBeInTheDocument();
expect( altMenuItem ).toHaveAttribute( 'aria-checked', 'false' );
} );
} );
} );