-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.tsx
643 lines (605 loc) · 17.3 KB
/
index.tsx
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
/**
* External dependencies
*/
import { render, screen, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
/**
* Internal dependencies
*/
import FontSizePicker from '../';
import type { FontSize } from '../types';
describe( 'FontSizePicker', () => {
test.each( [
// Use units when initial value uses units.
{ value: '12px', expectedValue: '80px' },
// Don't use units when initial value does not use units.
{ value: 12, expectedValue: 80 },
] )(
'should call onChange( $expectedValue ) after user types 80 when value is $value',
async ( { value, expectedValue } ) => {
const user = userEvent.setup();
const onChange = jest.fn();
render( <FontSizePicker value={ value } onChange={ onChange } /> );
const input = screen.getByLabelText( 'Custom' );
await user.clear( input );
await user.type( input, '80' );
expect( onChange ).toHaveBeenCalledTimes( 3 ); // Once for the clear, then once per keystroke.
expect( onChange ).toHaveBeenCalledWith( expectedValue );
}
);
test.each( [
// Use units when first font size uses units.
{ firstFontSize: '12px', expectedValue: '80px' },
// Don't use units when first font size does not use units.
{ firstFontSize: 12, expectedValue: 80 },
] )(
'should call onChange( $expectedValue ) after user types 80 when first font size is $firstFontSize',
async ( { firstFontSize, expectedValue } ) => {
const user = userEvent.setup();
const onChange = jest.fn();
render(
<FontSizePicker
fontSizes={ [ { slug: 'slug', size: firstFontSize } ] }
onChange={ onChange }
/>
);
await user.click(
screen.getByRole( 'button', { name: 'Set custom size' } )
);
const input = screen.getByLabelText( 'Custom' );
await user.type( input, '80' );
expect( onChange ).toHaveBeenCalledTimes( 2 ); // Once per keystroke.
expect( onChange ).toHaveBeenCalledWith( expectedValue );
}
);
describe( 'with > 5 homogeneous font sizes', () => {
const fontSizes = [
{
slug: 'tiny',
name: 'Tiny',
size: '8px',
},
{
slug: 'small',
name: 'Small',
size: '12px',
},
{
slug: 'medium',
name: 'Medium',
size: '16px',
},
{
slug: 'large',
name: 'Large',
size: '20px',
},
{
slug: 'x-large',
name: 'Extra Large',
size: '30px',
},
{
slug: 'xx-large',
// no name
size: '40px',
},
];
it( 'displays a select control', async () => {
const user = userEvent.setup();
render( <FontSizePicker fontSizes={ fontSizes } /> );
await user.click(
screen.getByRole( 'button', { name: 'Font size' } )
);
const options = screen.getAllByRole( 'option' );
expect( options ).toHaveLength( 8 );
expect( options[ 0 ] ).toHaveAccessibleName( 'Default' );
expect( options[ 1 ] ).toHaveAccessibleName( 'Tiny 8' );
expect( options[ 2 ] ).toHaveAccessibleName( 'Small 12' );
expect( options[ 3 ] ).toHaveAccessibleName( 'Medium 16' );
expect( options[ 4 ] ).toHaveAccessibleName( 'Large 20' );
expect( options[ 5 ] ).toHaveAccessibleName( 'Extra Large 30' );
expect( options[ 6 ] ).toHaveAccessibleName( 'xx-large 40' );
expect( options[ 7 ] ).toHaveAccessibleName( 'Custom' );
} );
test.each( [
{ value: undefined, expectedLabel: 'Size (px)' },
{ value: '8px', expectedLabel: 'Size (px)' },
{ value: '3px', expectedLabel: 'Size Custom' },
] )(
'displays $expectedLabel as label when value is $value',
( { value, expectedLabel } ) => {
render(
<FontSizePicker fontSizes={ fontSizes } value={ value } />
);
expect(
screen.getByLabelText( expectedLabel )
).toBeInTheDocument();
}
);
test.each( [
{
option: 'Default',
value: '8px',
expectedArguments: [ undefined ],
},
{
option: 'Tiny 8',
value: undefined,
expectedArguments: [ '8px', fontSizes[ 0 ] ],
},
] )(
'calls onChange( $expectedArguments ) when $option is selected',
async ( { option, value, expectedArguments } ) => {
const user = userEvent.setup();
const onChange = jest.fn();
render(
<FontSizePicker
fontSizes={ fontSizes }
value={ value }
onChange={ onChange }
/>
);
await user.click(
screen.getByRole( 'button', { name: 'Font size' } )
);
await user.click(
screen.getByRole( 'option', { name: option } )
);
expect( onChange ).toHaveBeenCalledTimes( 1 );
expect( onChange ).toHaveBeenCalledWith( ...expectedArguments );
}
);
commonSelectTests( fontSizes );
commonTests( fontSizes );
} );
describe( 'with > 5 heterogeneous font sizes', () => {
const fontSizes = [
{
slug: 'tiny',
name: 'Tiny',
size: '8px',
},
{
slug: 'small',
name: 'Small',
size: '1em',
},
{
slug: 'medium',
name: 'Medium',
size: '2rem',
},
{
slug: 'large',
name: 'Large',
size: 'clamp(1.75rem, 3vw, 2.25rem)',
},
{
slug: 'x-large',
name: 'Extra Large',
size: '30px',
},
{
slug: 'xx-large',
// no name
size: '40px',
},
];
it( 'displays a select control', async () => {
const user = userEvent.setup();
render( <FontSizePicker fontSizes={ fontSizes } /> );
await user.click(
screen.getByRole( 'button', { name: 'Font size' } )
);
const options = screen.getAllByRole( 'option' );
expect( options ).toHaveLength( 8 );
expect( options[ 0 ] ).toHaveAccessibleName( 'Default' );
expect( options[ 1 ] ).toHaveAccessibleName( 'Tiny 8px' );
expect( options[ 2 ] ).toHaveAccessibleName( 'Small 1em' );
expect( options[ 3 ] ).toHaveAccessibleName( 'Medium 2rem' );
expect( options[ 4 ] ).toHaveAccessibleName( 'Large' );
expect( options[ 5 ] ).toHaveAccessibleName( 'Extra Large 30px' );
expect( options[ 6 ] ).toHaveAccessibleName( 'xx-large 40px' );
expect( options[ 7 ] ).toHaveAccessibleName( 'Custom' );
} );
test.each( [
{ value: undefined, option: 'Default' },
{ value: '', option: 'Default' },
{ value: '8px', option: 'Tiny' },
] )(
'defaults to $option when value is $value',
( { value, option } ) => {
render(
<FontSizePicker fontSizes={ fontSizes } value={ value } />
);
expect(
screen.getByRole( 'button', { name: 'Font size' } )
).toHaveTextContent( option );
}
);
test.each( [
{ value: undefined, expectedLabel: 'Size' },
{ value: '8px', expectedLabel: 'Size' },
{ value: '1em', expectedLabel: 'Size' },
{ value: '2rem', expectedLabel: 'Size' },
{ value: 'clamp(1.75rem, 3vw, 2.25rem)', expectedLabel: 'Size' },
{ value: '3px', expectedLabel: 'Size Custom' },
] )(
'displays $expectedLabel as label when value is $value',
( { value, expectedLabel } ) => {
render(
<FontSizePicker fontSizes={ fontSizes } value={ value } />
);
expect(
screen.getByLabelText( expectedLabel )
).toBeInTheDocument();
}
);
test.each( [
{
option: 'Default',
value: '8px',
expectedArguments: [ undefined ],
},
{
option: 'Tiny 8px',
value: undefined,
expectedArguments: [ '8px', fontSizes[ 0 ] ],
},
{
option: 'Small 1em',
value: '8px',
expectedArguments: [ '1em', fontSizes[ 1 ] ],
},
{
option: 'Medium 2rem',
value: '8px',
expectedArguments: [ '2rem', fontSizes[ 2 ] ],
},
{
option: 'Large',
value: '8px',
expectedArguments: [
'clamp(1.75rem, 3vw, 2.25rem)',
fontSizes[ 3 ],
],
},
] )(
'calls onChange( $expectedValue ) when $option is selected',
async ( { option, value, expectedArguments } ) => {
const user = userEvent.setup();
const onChange = jest.fn();
render(
<FontSizePicker
fontSizes={ fontSizes }
value={ value }
onChange={ onChange }
/>
);
await user.click(
screen.getByRole( 'button', { name: 'Font size' } )
);
await user.click(
screen.getByRole( 'option', { name: option } )
);
expect( onChange ).toHaveBeenCalledTimes( 1 );
expect( onChange ).toHaveBeenCalledWith( ...expectedArguments );
}
);
commonSelectTests( fontSizes );
commonTests( fontSizes );
} );
describe( 'with ≤ 5 homogeneous font sizes', () => {
const fontSizes = [
{
slug: 'small',
// no name
size: '12px',
},
{
slug: 'medium',
name: 'Medium',
size: '16px',
},
{
slug: 'large',
name: 'Large',
size: '22px',
},
{
slug: 'huge',
name: 'Huge',
size: '30px',
},
{
slug: 'gigantosaurus',
name: 'Gigantosaurus',
size: '40px',
},
];
it( 'displays a toggle group control with t-shirt sizes', () => {
render( <FontSizePicker fontSizes={ fontSizes } /> );
const options = screen.getAllByRole( 'radio' );
expect( options ).toHaveLength( 5 );
expect( options[ 0 ] ).toHaveTextContent( 'S' );
expect( options[ 0 ] ).toHaveAccessibleName( 'Small' );
expect( options[ 1 ] ).toHaveTextContent( 'M' );
expect( options[ 1 ] ).toHaveAccessibleName( 'Medium' );
expect( options[ 2 ] ).toHaveTextContent( 'L' );
expect( options[ 2 ] ).toHaveAccessibleName( 'Large' );
expect( options[ 3 ] ).toHaveTextContent( 'XL' );
expect( options[ 3 ] ).toHaveAccessibleName( 'Huge' );
expect( options[ 4 ] ).toHaveTextContent( 'XXL' );
expect( options[ 4 ] ).toHaveAccessibleName( 'Gigantosaurus' );
} );
test.each( [
{ value: undefined, expectedLabel: 'Size' },
{ value: '12px', expectedLabel: 'Size Small' },
{ value: '40px', expectedLabel: 'Size Gigantosaurus' },
] )(
'displays $expectedLabel as label when value is $value',
( { value, expectedLabel } ) => {
render(
<FontSizePicker fontSizes={ fontSizes } value={ value } />
);
expect(
screen.getByLabelText( expectedLabel )
).toBeInTheDocument();
}
);
it( 'calls onChange when a font size is selected', async () => {
const user = userEvent.setup();
const onChange = jest.fn();
render(
<FontSizePicker fontSizes={ fontSizes } onChange={ onChange } />
);
await user.click( screen.getByRole( 'radio', { name: 'Medium' } ) );
expect( onChange ).toHaveBeenCalledTimes( 1 );
expect( onChange ).toHaveBeenCalledWith( '16px', fontSizes[ 1 ] );
} );
commonToggleGroupTests( fontSizes );
commonTests( fontSizes );
} );
describe( 'with ≤ 5 heterogeneous font sizes', () => {
const fontSizes = [
{
slug: 'small',
name: 'Small',
size: '12px',
},
{
slug: 'medium',
name: 'Medium',
size: '1em',
},
{
slug: 'large',
name: 'Large',
size: '2rem',
},
{
slug: 'x-large',
name: 'Extra Large',
size: 'clamp(1.75rem, 3vw, 2.25rem)',
},
];
it( 'displays a toggle group control with t-shirt sizes', () => {
render( <FontSizePicker fontSizes={ fontSizes } /> );
const options = screen.getAllByRole( 'radio' );
expect( options ).toHaveLength( 4 );
expect( options[ 0 ] ).toHaveTextContent( 'S' );
expect( options[ 0 ] ).toHaveAccessibleName( 'Small' );
expect( options[ 1 ] ).toHaveTextContent( 'M' );
expect( options[ 1 ] ).toHaveAccessibleName( 'Medium' );
expect( options[ 2 ] ).toHaveTextContent( 'L' );
expect( options[ 2 ] ).toHaveAccessibleName( 'Large' );
expect( options[ 3 ] ).toHaveTextContent( 'XL' );
expect( options[ 3 ] ).toHaveAccessibleName( 'Extra Large' );
} );
test.each( [
{ value: undefined, expectedLabel: 'Size' },
{ value: '12px', expectedLabel: 'Size Small' },
{ value: '1em', expectedLabel: 'Size Medium' },
{ value: '2rem', expectedLabel: 'Size Large' },
{
value: 'clamp(1.75rem, 3vw, 2.25rem)',
expectedLabel: 'Size Extra Large',
},
] )(
'displays $expectedLabel as label when value is $value',
( { value, expectedLabel } ) => {
render(
<FontSizePicker fontSizes={ fontSizes } value={ value } />
);
expect(
screen.getByLabelText( expectedLabel )
).toBeInTheDocument();
}
);
test.each( [
{ radio: 'Small', expectedArguments: [ '12px', fontSizes[ 0 ] ] },
{ radio: 'Medium', expectedArguments: [ '1em', fontSizes[ 1 ] ] },
{ radio: 'Large', expectedArguments: [ '2rem', fontSizes[ 2 ] ] },
{
radio: 'Extra Large',
expectedArguments: [
'clamp(1.75rem, 3vw, 2.25rem)',
fontSizes[ 3 ],
],
},
] )(
'calls onChange( $expectedArguments ) when $radio is selected',
async ( { radio, expectedArguments } ) => {
const user = userEvent.setup();
const onChange = jest.fn();
render(
<FontSizePicker
fontSizes={ fontSizes }
onChange={ onChange }
/>
);
await user.click(
screen.getByRole( 'radio', { name: radio } )
);
expect( onChange ).toHaveBeenCalledTimes( 1 );
expect( onChange ).toHaveBeenCalledWith( ...expectedArguments );
}
);
commonToggleGroupTests( fontSizes );
commonTests( fontSizes );
} );
function commonToggleGroupTests( fontSizes: FontSize[] ) {
it( 'defaults to M when value is 16px', () => {
render(
<FontSizePicker
fontSizes={ fontSizes }
value={ fontSizes[ 0 ].size }
/>
);
expect(
screen.getByRole( 'radio', { checked: true } )
).toHaveTextContent( 'S' );
} );
test.each( [ undefined, '' ] )(
'has no selection when value is %p',
( value ) => {
render(
<FontSizePicker fontSizes={ fontSizes } value={ value } />
);
expect( screen.getByRole( 'radiogroup' ) ).toBeInTheDocument();
expect(
screen.queryByRole( 'radio', { checked: true } )
).not.toBeInTheDocument();
}
);
}
function commonSelectTests( fontSizes: FontSize[] ) {
it( 'shows custom input when Custom is selected', async () => {
const user = userEvent.setup();
const onChange = jest.fn();
render(
<FontSizePicker fontSizes={ fontSizes } onChange={ onChange } />
);
await user.click(
screen.getByRole( 'button', { name: 'Font size' } )
);
await user.click(
screen.getByRole( 'option', { name: 'Custom' } )
);
expect( screen.getByLabelText( 'Custom' ) ).toBeInTheDocument();
expect( onChange ).not.toHaveBeenCalled();
} );
}
function commonTests( fontSizes: FontSize[] ) {
it( 'shows custom input when value is unknown', () => {
render( <FontSizePicker fontSizes={ fontSizes } value="3px" /> );
expect( screen.getByLabelText( 'Custom' ) ).toBeInTheDocument();
} );
it( 'allows custom values by default', async () => {
const user = userEvent.setup();
const onChange = jest.fn();
render(
<FontSizePicker fontSizes={ fontSizes } onChange={ onChange } />
);
await user.click(
screen.getByRole( 'button', { name: 'Set custom size' } )
);
await user.type( screen.getByLabelText( 'Custom' ), '80' );
expect( onChange ).toHaveBeenCalledTimes( 2 ); // Once per keystroke.
expect( onChange ).toHaveBeenCalledWith( '80px' );
} );
it( 'does not allow custom values when disableCustomFontSizes is set', () => {
render(
<FontSizePicker
fontSizes={ fontSizes }
disableCustomFontSizes
/>
);
expect(
screen.queryByRole( 'button', { name: 'Set custom size' } )
).not.toBeInTheDocument();
} );
it( 'does not display a slider by default', async () => {
const user = userEvent.setup();
render( <FontSizePicker fontSizes={ fontSizes } /> );
await user.click(
screen.getByRole( 'button', { name: 'Set custom size' } )
);
expect(
screen.queryByLabelText( 'Custom Size' )
).not.toBeInTheDocument();
} );
it( 'allows a slider to be used when withSlider is set', async () => {
const user = userEvent.setup();
const onChange = jest.fn();
render(
<FontSizePicker
fontSizes={ fontSizes }
withSlider
onChange={ onChange }
/>
);
await user.click(
screen.getByRole( 'button', { name: 'Set custom size' } )
);
const sliderInput = screen.getByLabelText( 'Custom Size' );
fireEvent.change( sliderInput, {
target: { value: 80 },
} );
expect( onChange ).toHaveBeenCalledTimes( 1 );
expect( onChange ).toHaveBeenCalledWith( '80px' );
} );
it( 'allows reset by default', async () => {
const user = userEvent.setup();
const onChange = jest.fn();
render(
<FontSizePicker
fontSizes={ fontSizes }
value={ fontSizes[ 0 ].size }
onChange={ onChange }
/>
);
await user.click(
screen.getByRole( 'button', { name: 'Set custom size' } )
);
await user.click( screen.getByRole( 'button', { name: 'Reset' } ) );
expect( onChange ).toHaveBeenCalledTimes( 1 );
expect( onChange ).toHaveBeenCalledWith( undefined );
} );
it( 'does not allow reset when withReset is false', async () => {
const user = userEvent.setup();
render(
<FontSizePicker
fontSizes={ fontSizes }
value={ fontSizes[ 0 ].size }
withReset={ false }
/>
);
await user.click(
screen.getByRole( 'button', { name: 'Set custom size' } )
);
expect(
screen.queryByRole( 'button', { name: 'Reset' } )
).not.toBeInTheDocument();
} );
it( 'applies custom units to custom font size control', async () => {
const user = userEvent.setup();
render(
<FontSizePicker
fontSizes={ fontSizes }
value={ fontSizes[ 0 ].size }
units={ [ 'px', 'ch', 'ex' ] }
/>
);
await user.click(
screen.getByRole( 'button', { name: 'Set custom size' } )
);
const units = screen.getAllByRole( 'option' );
expect( units ).toHaveLength( 3 );
expect( units[ 0 ] ).toHaveAccessibleName( 'px' );
expect( units[ 1 ] ).toHaveAccessibleName( 'ch' );
expect( units[ 2 ] ).toHaveAccessibleName( 'ex' );
} );
}
} );