forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreferences.unit.test.jsx
737 lines (611 loc) · 21.1 KB
/
Preferences.unit.test.jsx
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
import React from 'react';
import { act, fireEvent, reduxRender, screen } from '../../../../test-utils';
import { initialState } from '../../reducers/preferences';
import Preferences from './index';
import * as PreferencesActions from '../../actions/preferences';
describe('<Preferences />', () => {
// For backwards compatibility, spy on each action creator to see when it was dispatched.
const props = Object.fromEntries(
Object.keys(PreferencesActions).map((name) => {
const spied = jest.spyOn(PreferencesActions, name);
return [name, spied];
})
);
const subject = (initialPreferences = {}) =>
reduxRender(<Preferences />, {
initialState: {
preferences: {
...initialState,
...initialPreferences
}
}
});
afterEach(() => {
jest.clearAllMocks();
});
describe('font tests', () => {
it('font size increase button says increase', () => {
// render the component
subject({ fontSize: 12 });
// get ahold of the button for increasing text size
const fontPlusButton = screen.getByRole('button', {
name: /increase font size/i
});
// check that button says says "Increase"
expect(fontPlusButton.textContent.toLowerCase()).toBe('increase');
});
it('increase font size by 2 when clicking plus button', () => {
// render the component with font size set to 12
subject({ fontSize: 12 });
// get ahold of the button for increasing text size
const fontPlusButton = screen.getByRole('button', {
name: /increase font size/i
});
// click the button
act(() => {
fireEvent.click(fontPlusButton);
});
// expect that setFontSize has been called once with the argument 14
expect(props.setFontSize).toHaveBeenCalledTimes(1);
expect(props.setFontSize.mock.calls[0][0]).toBe(14);
});
it('font size decrease button says decrease', () => {
// render the component with font size set to 12
subject({ fontSize: 12 });
// get ahold of the button for decreasing font size
const fontPlusButton = screen.getByRole('button', {
name: /decrease font size/i
});
// check that button says "decrease"
expect(fontPlusButton.textContent.toLowerCase()).toBe('decrease');
});
it('decrease font size by 2 when clicking minus button', () => {
// render the component with font size set to 12
subject({ fontSize: 12 });
// get ahold of the button for decreasing text size
const fontMinusButton = screen.getByRole('button', {
name: /decrease font size/i
});
// click it
act(() => {
fireEvent.click(fontMinusButton);
});
// expect that setFontSize would have been called once with argument 10
expect(props.setFontSize).toHaveBeenCalledTimes(1);
expect(props.setFontSize.mock.calls[0][0]).toBe(10);
});
it('font text field changes on manual text input', () => {
// render the component with font size set to 12
subject({ fontSize: 12 });
// get ahold of the text field
const input = screen.getByRole('textbox', { name: /font size/i });
// change input to 24
act(() => {
fireEvent.change(input, { target: { value: '24' } });
});
// submit form
act(() => {
fireEvent.submit(
screen.getByRole('form', {
name: /set font size/i
})
);
});
// expect that setFontSize was called once with 24
expect(props.setFontSize).toHaveBeenCalledTimes(1);
expect(props.setFontSize.mock.calls[0][0]).toBe(24);
});
it('font size CAN NOT go over 36', () => {
// render the component
subject({ fontSize: 12 });
// get ahold of the text field
const input = screen.getByRole('textbox', { name: /font size/i });
act(() => {
fireEvent.change(input, { target: { value: '100' } });
});
expect(input.value).toBe('100');
act(() => {
fireEvent.submit(
screen.getByRole('form', {
name: /set font size/i
})
);
});
expect(props.setFontSize).toHaveBeenCalledTimes(1);
expect(props.setFontSize.mock.calls[0][0]).toBe(36);
});
it('font size CAN NOT go under 8', () => {
// render the component
subject({ fontSize: 12 });
// get ahold of the text field
const input = screen.getByRole('textbox', { name: /font size/i });
act(() => {
fireEvent.change(input, { target: { value: '0' } });
});
expect(input.value).toBe('0');
act(() => {
fireEvent.submit(
screen.getByRole('form', {
name: /set font size/i
})
);
});
expect(props.setFontSize).toHaveBeenCalledTimes(1);
expect(props.setFontSize.mock.calls[0][0]).toBe(8);
});
// this case is a bit synthetic because we wouldn't be able to type
// h and then i, but it tests the same idea
it('font size input field does NOT take non-integers', () => {
// render the component
subject({ fontSize: 12 });
// get ahold of the text field
const input = screen.getByRole('textbox', { name: /font size/i });
act(() => {
fireEvent.change(input, { target: { value: 'hi' } });
});
// it shouldnt have changed at all
expect(input.value).toBe('12');
// we hit submit
act(() => {
fireEvent.submit(
screen.getByRole('form', {
name: /set font size/i
})
);
});
// it still sets the font size but it's still 12
expect(props.setFontSize).toHaveBeenCalledTimes(1);
expect(props.setFontSize.mock.calls[0][0]).toBe(12);
});
it('font size input field does NOT take "-"', () => {
// render the component
subject({ fontSize: 12 });
// get ahold of the text field
const input = screen.getByRole('textbox', { name: /font size/i });
act(() => {
fireEvent.change(input, { target: { value: '-' } });
});
expect(input.value).toBe('12');
act(() => {
fireEvent.submit(
screen.getByRole('form', {
name: /set font size/i
})
);
});
expect(props.setFontSize).toHaveBeenCalledTimes(1);
expect(props.setFontSize.mock.calls[0][0]).toBe(12);
});
});
const testToggle = (
checkedRadio,
uncheckedRadio,
setter,
setterExpectedArgument
) => {
// make sure one is false and the other is true
expect(checkedRadio.checked).toBe(true);
expect(uncheckedRadio.checked).toBe(false);
// click on the one already selected
act(() => {
fireEvent.click(checkedRadio);
});
// the value has not changed so OnChange has not been called
expect(setter).toHaveBeenCalledTimes(0);
// this radio buttons should still be the same
expect(checkedRadio.checked).toBe(true);
expect(uncheckedRadio.checked).toBe(false);
// now we click the other one that's not yet selected
act(() => {
fireEvent.click(uncheckedRadio);
});
// expect that the setter function was called with the value true
expect(setter).toHaveBeenCalledTimes(1);
expect(setter.mock.calls[0][0]).toBe(setterExpectedArgument);
};
describe('testing theme switching', () => {
describe('dark mode', () => {
it('switch to light', () => {
subject({ theme: 'dark' });
const themeRadioCurrent = screen.getByRole('radio', {
name: /dark theme on/i
});
expect(themeRadioCurrent.checked).toBe(true);
const themeRadioAfter = screen.getByRole('radio', {
name: /light theme on/i
});
act(() => {
fireEvent.click(themeRadioAfter);
});
expect(props.setTheme).toHaveBeenCalledTimes(1);
expect(props.setTheme.mock.calls[0][0]).toBe('light');
});
});
describe('light mode', () => {
it('switch to dark', () => {
subject({ theme: 'light' });
const themeRadioCurrent = screen.getByRole('radio', {
name: /light theme on/i
});
expect(themeRadioCurrent.checked).toBe(true);
const themeRadioAfter = screen.getByRole('radio', {
name: /dark theme on/i
});
act(() => {
fireEvent.click(themeRadioAfter);
});
expect(props.setTheme).toHaveBeenCalledTimes(1);
expect(props.setTheme.mock.calls[0][0]).toBe('dark');
});
it('switch to contrast', () => {
subject({ theme: 'light' });
const themeRadioCurrent = screen.getByRole('radio', {
name: /light theme on/i
});
expect(themeRadioCurrent.checked).toBe(true);
const themeRadioAfter = screen.getByRole('radio', {
name: /high contrast theme on/i
});
act(() => {
fireEvent.click(themeRadioAfter);
});
expect(props.setTheme).toHaveBeenCalledTimes(1);
expect(props.setTheme.mock.calls[0][0]).toBe('contrast');
});
});
});
describe('testing toggle UI elements on starting tab', () => {
it('autosave toggle, starting at false', () => {
subject({ autosave: false });
// get ahold of the radio buttons for toggling autosave
const autosaveRadioFalse = screen.getByRole('radio', {
name: /autosave off/i
});
const autosaveRadioTrue = screen.getByRole('radio', {
name: /autosave on/i
});
testToggle(
autosaveRadioFalse,
autosaveRadioTrue,
props.setAutosave,
true
);
});
it('autocloseBracketsQuotes toggle, starting at false', () => {
// render the component with autocloseBracketsQuotes prop set to false
subject({ autocloseBracketsQuotes: false });
// get ahold of the radio buttons for toggling autocloseBracketsQuotes
const autocloseRadioFalse = screen.getByRole('radio', {
name: /autoclose brackets and quotes off/i
});
const autocloseRadioTrue = screen.getByRole('radio', {
name: /autoclose brackets and quotes on/i
});
testToggle(
autocloseRadioFalse,
autocloseRadioTrue,
props.setAutocloseBracketsQuotes,
true
);
});
it('autocompleteHinter toggle, starting at false', () => {
// render the component with autocompleteHinter prop set to false
subject({ autocompleteHinter: false });
// get ahold of the radio buttons for toggling autocompleteHinter
const autocompleteRadioFalse = screen.getByRole('radio', {
name: /autocomplete hinter off/i
});
const autocompleteRadioTrue = screen.getByRole('radio', {
name: /autocomplete hinter on/i
});
testToggle(
autocompleteRadioFalse,
autocompleteRadioTrue,
props.setAutocompleteHinter,
true
);
});
describe('start autosave value at true', () => {
it('autosave toggle, starting at true', () => {
// render the component with autosave prop set to true
subject({ autosave: true });
// get ahold of the radio buttons for toggling autosave
const autosaveRadioFalse = screen.getByRole('radio', {
name: /autosave off/i
});
const autosaveRadioTrue = screen.getByRole('radio', {
name: /autosave on/i
});
testToggle(
autosaveRadioTrue,
autosaveRadioFalse,
props.setAutosave,
false
);
});
});
describe('start autoclose brackets value at true', () => {
it('autocloseBracketsQuotes toggle, starting at true', () => {
subject({ autocloseBracketsQuotes: true });
// get ahold of the radio buttons for toggling autocloseBracketsQuotes
const autocloseRadioFalse = screen.getByRole('radio', {
name: /autoclose brackets and quotes off/i
});
const autocloseRadioTrue = screen.getByRole('radio', {
name: /autoclose brackets and quotes on/i
});
testToggle(
autocloseRadioTrue,
autocloseRadioFalse,
props.setAutocloseBracketsQuotes,
false
);
});
});
describe('start autocomplete hinter value at true', () => {
it('autocompleteHinter toggle, starting at true', () => {
// render the component with autocompleteHinter prop set to true
subject({ autocompleteHinter: true });
// get ahold of the radio buttons for toggling autocompleteHinter
const autocompleteRadioFalse = screen.getByRole('radio', {
name: /autocomplete hinter off/i
});
const autocompleteRadioTrue = screen.getByRole('radio', {
name: /autocomplete hinter on/i
});
testToggle(
autocompleteRadioTrue,
autocompleteRadioFalse,
props.setAutocompleteHinter,
false
);
});
});
describe('start linewrap at false', () => {
it('linewrap toggle, starting at false', () => {
// render the component with linewrap prop set to false
subject({ linewrap: false });
// get ahold of the radio buttons for toggling linewrap
const linewrapRadioFalse = screen.getByRole('radio', {
name: /linewrap off/i
});
const linewrapRadioTrue = screen.getByRole('radio', {
name: /linewrap on/i
});
testToggle(
linewrapRadioFalse,
linewrapRadioTrue,
props.setLinewrap,
true
);
});
});
describe('start linewrap at true', () => {
it('linewrap toggle, starting at true', () => {
// render the component with linewrap prop set to false
subject({ linewrap: true });
// get ahold of the radio buttons for toggling linewrap
const linewrapRadioFalse = screen.getByRole('radio', {
name: /linewrap off/i
});
const linewrapRadioTrue = screen.getByRole('radio', {
name: /linewrap on/i
});
testToggle(
linewrapRadioTrue,
linewrapRadioFalse,
props.setLinewrap,
false
);
});
});
});
describe('can toggle between general settings and accessibility tabs successfully', () => {
it('can toggle sucessfully', () => {
// render the component with lineNumbers prop set to false
subject({ lineNumbers: false });
// switch to accessibility
act(() => {
fireEvent.click(
screen.getByRole('heading', { name: /accessibility/i })
);
});
const accessibilityElement1 = screen.getByRole('radio', {
name: /line numbers on/i
});
expect(accessibilityElement1).toBeInTheDocument();
// switch back
act(() => {
fireEvent.click(
screen.getByRole('heading', { name: /general settings/i })
);
});
const generalElement1 = screen.getByRole('radio', {
name: /linewrap on/i
});
expect(generalElement1).toBeInTheDocument();
});
});
describe('testing toggle UI elements on accessibility tab', () => {
describe('starting linenumbers at false', () => {
it('lineNumbers toggle, starting at false', () => {
// render the component with lineNumbers prop set to false
subject({ lineNumbers: false });
// switch tabs
act(() => {
fireEvent.click(
screen.getByRole('heading', { name: /accessibility/i })
);
});
// get ahold of the radio buttons for toggling linenumber settings
const lineNumbersRadioFalse = screen.getByRole('radio', {
name: /line numbers off/i
});
const lineNumbersRadioTrue = screen.getByRole('radio', {
name: /line numbers on/i
});
testToggle(
lineNumbersRadioFalse,
lineNumbersRadioTrue,
props.setLineNumbers,
true
);
});
});
describe('starting linenumbers at true', () => {
it('lineNumbers toggle, starting at true', () => {
// render the component with lineNumbers prop set to false
subject({ lineNumbers: true });
// switch tabs
act(() => {
fireEvent.click(
screen.getByRole('heading', { name: /accessibility/i })
);
});
// get ahold of the radio buttons for toggling linenumber settings
const lineNumbersRadioFalse = screen.getByRole('radio', {
name: /line numbers off/i
});
const lineNumbersRadioTrue = screen.getByRole('radio', {
name: /line numbers on/i
});
testToggle(
lineNumbersRadioTrue,
lineNumbersRadioFalse,
props.setLineNumbers,
false
);
});
});
describe('starting lintWarning at false', () => {
it('lintWarning toggle, starting at false', () => {
// render the component with lintWarning prop set to false
subject({ lintWarning: false });
// switch tabs
act(() => {
fireEvent.click(
screen.getByRole('heading', { name: /accessibility/i })
);
});
// get ahold of the radio buttons for toggling line warning
const lintWarningRadioFalse = screen.getByRole('radio', {
name: /lint warning off/i
});
const lintWarningRadioTrue = screen.getByRole('radio', {
name: /lint warning on/i
});
testToggle(
lintWarningRadioFalse,
lintWarningRadioTrue,
props.setLintWarning,
true
);
});
});
describe('starting lintWarning at true', () => {
it('lintWarning toggle, starting at true', () => {
// render the component with lintWarning prop set to false
subject({ lintWarning: true });
// switch tabs
act(() => {
fireEvent.click(
screen.getByRole('heading', { name: /accessibility/i })
);
});
// get ahold of the radio buttons for toggling line warning
const lintWarningRadioFalse = screen.getByRole('radio', {
name: /lint warning off/i
});
const lintWarningRadioTrue = screen.getByRole('radio', {
name: /lint warning on/i
});
testToggle(
lintWarningRadioTrue,
lintWarningRadioFalse,
props.setLintWarning,
false
);
});
});
const testCheckbox = (arialabel, startState, setter) => {
subject({
textOutput: startState && arialabel === 'text output on',
soundOutput: startState && arialabel === 'sound output on',
gridOutput: startState && arialabel === 'table output on'
});
// switch tabs
act(() => {
fireEvent.click(
screen.getByRole('heading', { name: /accessibility/i })
);
});
const testedCheckbox = screen.getByRole('checkbox', { name: arialabel });
if (startState) {
expect(testedCheckbox.checked).toBe(true);
} else {
expect(testedCheckbox.checked).toBe(false);
}
act(() => {
fireEvent.click(testedCheckbox);
});
expect(props[setter]).toHaveBeenCalledTimes(1);
expect(props[setter].mock.calls[0][0]).toBe(!startState);
};
it('clicking on text output checkbox to unselect it', () => {
testCheckbox('text output on', true, 'setTextOutput');
});
it('clicking on text output checkbox to select it', () => {
testCheckbox('text output on', false, 'setTextOutput');
});
it('clicking on grid output checkbox to unselect it', () => {
testCheckbox('table output on', true, 'setGridOutput');
});
it('clicking on grid output checkbox to select it', () => {
testCheckbox('table output on', false, 'setGridOutput');
});
describe('multiple checkboxes can be selected', () => {
it('multiple checkboxes can be selected', () => {
subject({
textOutput: true,
gridOutput: true
});
// switch tabs
act(() => {
fireEvent.click(
screen.getByRole('heading', { name: /accessibility/i })
);
});
const textOutputCheckbox = screen.getByRole('checkbox', {
name: 'text output on'
});
const gridOutputCheckbox = screen.getByRole('checkbox', {
name: 'table output on'
});
expect(textOutputCheckbox.checked).toBe(true);
expect(gridOutputCheckbox.checked).toBe(true);
});
});
describe('none of the checkboxes can be selected', () => {
it('none of the checkboxes can be selected', () => {
subject({
textOutput: false,
gridOutput: false
});
// switch tabs
act(() => {
fireEvent.click(
screen.getByRole('heading', { name: /accessibility/i })
);
});
const textOutputCheckbox = screen.getByRole('checkbox', {
name: 'text output on'
});
const gridOutputCheckbox = screen.getByRole('checkbox', {
name: 'table output on'
});
expect(textOutputCheckbox.checked).toBe(false);
expect(gridOutputCheckbox.checked).toBe(false);
});
});
});
});