@instructure/ui-date-input 10.19.2-snapshot-3 → 10.19.2-snapshot-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,867 +0,0 @@
1
- /*
2
- * The MIT License (MIT)
3
- *
4
- * Copyright (c) 2015 - present Instructure, Inc.
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
-
25
- import { fireEvent, render, screen, waitFor } from '@testing-library/react'
26
- import { vi } from 'vitest'
27
- import userEvent from '@testing-library/user-event'
28
- import '@testing-library/jest-dom'
29
-
30
- // eslint-disable-next-line no-restricted-imports
31
- import { generateA11yTests } from '@instructure/ui-scripts/lib/test/generateA11yTests'
32
- import { runAxeCheck } from '@instructure/ui-axe-check'
33
- import { Calendar } from '@instructure/ui-calendar'
34
-
35
- import Examples from '../__examples__/DateInput.examples'
36
- import { DateInput } from '../index'
37
-
38
- describe('<DateInput />', () => {
39
- const weekdayLabels = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
40
-
41
- const generateDays = (count = Calendar.DAY_COUNT) => {
42
- const days = []
43
- const date = new Date('2019-07-28')
44
-
45
- while (days.length < count) {
46
- days.push(
47
- <Calendar.Day
48
- key={date.toISOString()}
49
- date={date.toISOString()}
50
- label={date.toISOString()}
51
- >
52
- {date.getDate()}
53
- </Calendar.Day>
54
- )
55
- date.setDate(date.getDate() + 1)
56
- }
57
-
58
- return days
59
- }
60
-
61
- let consoleWarningMock: ReturnType<typeof vi.spyOn>
62
- let consoleErrorMock: ReturnType<typeof vi.spyOn>
63
-
64
- beforeEach(() => {
65
- // Mocking console to prevent test output pollution
66
- consoleWarningMock = vi
67
- .spyOn(console, 'warn')
68
- .mockImplementation(() => {}) as any
69
- consoleErrorMock = vi
70
- .spyOn(console, 'error')
71
- .mockImplementation(() => {}) as any
72
- })
73
-
74
- afterEach(() => {
75
- consoleWarningMock.mockRestore()
76
- consoleErrorMock.mockRestore()
77
- })
78
-
79
- it('should render an input and a calendar', async () => {
80
- const { container, findByRole } = render(
81
- <DateInput
82
- renderLabel="date-input"
83
- renderWeekdayLabels={weekdayLabels}
84
- isShowingCalendar
85
- >
86
- {generateDays()}
87
- </DateInput>
88
- )
89
- const dateInput = container.querySelector('input')
90
-
91
- expect(dateInput).toBeInTheDocument()
92
- expect(dateInput).toHaveAttribute('type', 'text')
93
-
94
- await userEvent.click(dateInput!)
95
- const calendarTable = await findByRole('listbox')
96
- const calendarWrapper = await document.querySelector(
97
- '[id^="Selectable_"][id$="-list"]'
98
- )
99
-
100
- await waitFor(() => {
101
- expect(calendarWrapper).toBeInTheDocument()
102
- expect(calendarTable).toBeInTheDocument()
103
- })
104
- })
105
-
106
- describe('input', () => {
107
- it('should render a label', () => {
108
- const labelText = 'label text'
109
-
110
- render(
111
- <DateInput renderLabel={labelText} renderWeekdayLabels={weekdayLabels}>
112
- {generateDays()}
113
- </DateInput>
114
- )
115
- const dateInput = screen.getByLabelText('label text')
116
-
117
- expect(dateInput).toBeInTheDocument()
118
- })
119
-
120
- it('should set value', () => {
121
- const value = 'January 5'
122
-
123
- render(
124
- <DateInput
125
- renderLabel="Choose date"
126
- value={value}
127
- onChange={vi.fn()}
128
- renderWeekdayLabels={weekdayLabels}
129
- >
130
- {generateDays()}
131
- </DateInput>
132
- )
133
- const dateInput = screen.getByLabelText('Choose date')
134
-
135
- expect(dateInput).toHaveValue(value)
136
- })
137
-
138
- it('should call onChange with the updated value', async () => {
139
- const onChange = vi.fn()
140
- const value = 'May 18, 2022'
141
-
142
- render(
143
- <DateInput
144
- renderLabel="Choose date"
145
- renderWeekdayLabels={weekdayLabels}
146
- onChange={onChange}
147
- >
148
- {generateDays()}
149
- </DateInput>
150
- )
151
- const dateInput = screen.getByLabelText('Choose date')
152
-
153
- fireEvent.change(dateInput, { target: { value: value } })
154
- fireEvent.keyDown(dateInput, { key: 'Enter', code: 'Enter' })
155
- fireEvent.blur(dateInput)
156
-
157
- await waitFor(() => {
158
- expect(onChange).toHaveBeenCalledTimes(1)
159
- expect(onChange.mock.calls[0][1].value).toEqual(
160
- expect.stringContaining(value)
161
- )
162
- })
163
- })
164
-
165
- it('should call onBlur', () => {
166
- const onBlur = vi.fn()
167
-
168
- render(
169
- <DateInput
170
- renderLabel="Choose date"
171
- renderWeekdayLabels={weekdayLabels}
172
- onBlur={onBlur}
173
- >
174
- {generateDays()}
175
- </DateInput>
176
- )
177
- const dateInput = screen.getByLabelText('Choose date')
178
-
179
- fireEvent.blur(dateInput)
180
-
181
- expect(onBlur).toHaveBeenCalledTimes(1)
182
- })
183
-
184
- it('should correctly set interaction type', async () => {
185
- const { rerender } = render(
186
- <DateInput
187
- renderLabel="Choose date"
188
- renderWeekdayLabels={weekdayLabels}
189
- interaction="disabled"
190
- >
191
- {generateDays()}
192
- </DateInput>
193
- )
194
- const dateInput = screen.getByLabelText('Choose date')
195
- expect(dateInput).toHaveAttribute('disabled')
196
-
197
- rerender(
198
- <DateInput
199
- renderLabel="Choose date"
200
- renderWeekdayLabels={weekdayLabels}
201
- interaction="readonly"
202
- >
203
- {generateDays()}
204
- </DateInput>
205
- )
206
-
207
- const dateInputAfterUpdate = screen.getByLabelText('Choose date')
208
-
209
- await waitFor(() => {
210
- expect(dateInputAfterUpdate).toHaveAttribute('readonly')
211
- })
212
- })
213
-
214
- it('should correctly set disabled', () => {
215
- render(
216
- <DateInput
217
- renderLabel="Choose date"
218
- renderWeekdayLabels={weekdayLabels}
219
- disabled
220
- >
221
- {generateDays()}
222
- </DateInput>
223
- )
224
-
225
- const dateInput = screen.getByLabelText('Choose date')
226
-
227
- expect(dateInput).toHaveAttribute('disabled')
228
- })
229
-
230
- it('should correctly set readOnly', () => {
231
- render(
232
- <DateInput
233
- renderLabel="Choose date"
234
- renderWeekdayLabels={weekdayLabels}
235
- readOnly
236
- >
237
- {generateDays()}
238
- </DateInput>
239
- )
240
-
241
- const dateInput = screen.getByLabelText('Choose date')
242
-
243
- expect(dateInput).toHaveAttribute('readOnly')
244
- })
245
-
246
- it('should set placeholder', () => {
247
- const placeholder = 'Start typing to choose a date'
248
-
249
- render(
250
- <DateInput
251
- renderLabel="Choose date"
252
- renderWeekdayLabels={weekdayLabels}
253
- placeholder={placeholder}
254
- >
255
- {generateDays()}
256
- </DateInput>
257
- )
258
- const dateInput = screen.getByLabelText('Choose date')
259
-
260
- expect(dateInput).toHaveAttribute('placeholder', placeholder)
261
- })
262
-
263
- it('should be requireable', () => {
264
- render(
265
- <DateInput
266
- renderLabel="Choose date"
267
- renderWeekdayLabels={weekdayLabels}
268
- isRequired
269
- >
270
- {generateDays()}
271
- </DateInput>
272
- )
273
- const dateInput = screen.getByLabelText('Choose date *')
274
-
275
- expect(dateInput).toHaveAttribute('required')
276
- })
277
-
278
- it('should provide inputRef', () => {
279
- const inputRef = vi.fn()
280
-
281
- render(
282
- <DateInput
283
- renderLabel="Choose date"
284
- renderWeekdayLabels={weekdayLabels}
285
- inputRef={inputRef}
286
- >
287
- {generateDays()}
288
- </DateInput>
289
- )
290
- const dateInput = screen.getByLabelText('Choose date')
291
-
292
- expect(inputRef).toHaveBeenCalledWith(dateInput)
293
- })
294
-
295
- it('should render messages', () => {
296
- const text = 'The selected date is invalid'
297
-
298
- const { container } = render(
299
- <DateInput
300
- renderLabel="Choose date"
301
- renderWeekdayLabels={weekdayLabels}
302
- messages={[{ type: 'error', text }]}
303
- >
304
- {generateDays()}
305
- </DateInput>
306
- )
307
-
308
- expect(container).toHaveTextContent(text)
309
- })
310
-
311
- it('should allow custom props to pass through', () => {
312
- render(
313
- <DateInput
314
- renderLabel="Choose date"
315
- renderWeekdayLabels={weekdayLabels}
316
- data-custom-attr="custom value"
317
- name="my name"
318
- >
319
- {generateDays()}
320
- </DateInput>
321
- )
322
- const dateInput = screen.getByLabelText('Choose date')
323
-
324
- expect(dateInput).toHaveAttribute('data-custom-attr', 'custom value')
325
- expect(dateInput).toHaveAttribute('name', 'my name')
326
- })
327
- })
328
-
329
- describe('Calendar', () => {
330
- it('should show calendar when `isShowingCalendar` is set', async () => {
331
- const { rerender, queryByRole } = render(
332
- <DateInput
333
- renderLabel="Choose date"
334
- renderWeekdayLabels={weekdayLabels}
335
- >
336
- {generateDays()}
337
- </DateInput>
338
- )
339
- const dateInput = screen.getByLabelText('Choose date')
340
- const calendarTable = await queryByRole('listbox')
341
- const calendarWrapper = await document.querySelector(
342
- '[id^="Selectable_"][id$="-list"]'
343
- )
344
-
345
- expect(dateInput).toBeInTheDocument()
346
- expect(calendarTable).not.toBeInTheDocument()
347
- expect(calendarWrapper).not.toBeInTheDocument()
348
-
349
- rerender(
350
- <DateInput
351
- renderLabel="Choose date"
352
- renderWeekdayLabels={weekdayLabels}
353
- isShowingCalendar
354
- >
355
- {generateDays()}
356
- </DateInput>
357
- )
358
- const dateInputAfterUpdate = screen.getByLabelText('Choose date')
359
- const calendarTableAfterUpdate = await queryByRole('listbox')
360
- const calendarWrapperAfterUpdate = await document.querySelector(
361
- '[id^="Selectable_"][id$="-list"]'
362
- )
363
-
364
- await waitFor(() => {
365
- expect(dateInputAfterUpdate).toBeInTheDocument()
366
- expect(calendarTableAfterUpdate).toBeInTheDocument()
367
- expect(calendarWrapperAfterUpdate).toBeInTheDocument()
368
- })
369
- })
370
-
371
- describe('onRequestShowCalendar', () => {
372
- it('should call onRequestShowCalendar when label is clicked', async () => {
373
- const onRequestShowCalendar = vi.fn()
374
-
375
- const { container } = render(
376
- <DateInput
377
- renderLabel="Choose date"
378
- renderWeekdayLabels={weekdayLabels}
379
- onRequestShowCalendar={onRequestShowCalendar}
380
- >
381
- {generateDays()}
382
- </DateInput>
383
- )
384
- const dateInput = container.querySelector(
385
- 'span[class$="-formFieldLayout__label"]'
386
- )
387
-
388
- expect(dateInput).toHaveTextContent('Choose date')
389
-
390
- await userEvent.click(dateInput!)
391
-
392
- await waitFor(() => {
393
- expect(onRequestShowCalendar).toHaveBeenCalled()
394
- })
395
- })
396
-
397
- it('should call onRequestShowCalendar when input is clicked', async () => {
398
- const onRequestShowCalendar = vi.fn()
399
-
400
- const { container } = render(
401
- <DateInput
402
- renderLabel="Choose date"
403
- renderWeekdayLabels={weekdayLabels}
404
- onRequestShowCalendar={onRequestShowCalendar}
405
- >
406
- {generateDays()}
407
- </DateInput>
408
- )
409
- const dateInput = container.querySelector('input')
410
-
411
- await userEvent.click(dateInput!)
412
-
413
- await waitFor(() => {
414
- expect(onRequestShowCalendar).toHaveBeenCalledTimes(1)
415
- })
416
- })
417
-
418
- it('should call onRequestShowCalendar when input receives space event', async () => {
419
- const onRequestShowCalendar = vi.fn()
420
-
421
- render(
422
- <DateInput
423
- renderLabel="Choose date"
424
- renderWeekdayLabels={weekdayLabels}
425
- onRequestShowCalendar={onRequestShowCalendar}
426
- >
427
- {generateDays()}
428
- </DateInput>
429
- )
430
- const dateInput = screen.getByLabelText('Choose date')
431
-
432
- await userEvent.type(dateInput, '{space}')
433
-
434
- await waitFor(() => {
435
- expect(onRequestShowCalendar).toHaveBeenCalledTimes(1)
436
- })
437
- })
438
-
439
- it('should not call onRequestShowCalendar when input receives space event if calendar is already showing', async () => {
440
- const onRequestShowCalendar = vi.fn()
441
-
442
- render(
443
- <DateInput
444
- renderLabel="Choose date"
445
- renderWeekdayLabels={weekdayLabels}
446
- onRequestShowCalendar={onRequestShowCalendar}
447
- isShowingCalendar
448
- >
449
- {generateDays()}
450
- </DateInput>
451
- )
452
- const dateInput = screen.getByLabelText('Choose date')
453
-
454
- await userEvent.type(dateInput, '{space}')
455
-
456
- await waitFor(() => {
457
- expect(onRequestShowCalendar).not.toHaveBeenCalled()
458
- })
459
- })
460
-
461
- it('should call onRequestShowCalendar when input receives down arrow event', async () => {
462
- const onRequestShowCalendar = vi.fn()
463
-
464
- render(
465
- <DateInput
466
- renderLabel="Choose date"
467
- renderWeekdayLabels={weekdayLabels}
468
- onRequestShowCalendar={onRequestShowCalendar}
469
- >
470
- {generateDays()}
471
- </DateInput>
472
- )
473
- const dateInput = screen.getByLabelText('Choose date')
474
-
475
- await userEvent.type(dateInput, '{arrowdown}')
476
-
477
- await waitFor(() => {
478
- expect(onRequestShowCalendar).toHaveBeenCalledTimes(1)
479
- })
480
- })
481
-
482
- it('should not call onRequestShowCalendar when input receives down arrow event if calendar is already showing', async () => {
483
- const onRequestShowCalendar = vi.fn()
484
-
485
- render(
486
- <DateInput
487
- renderLabel="Choose date"
488
- renderWeekdayLabels={weekdayLabels}
489
- onRequestShowCalendar={onRequestShowCalendar}
490
- isShowingCalendar
491
- >
492
- {generateDays()}
493
- </DateInput>
494
- )
495
- const dateInput = screen.getByLabelText('Choose date')
496
-
497
- await userEvent.type(dateInput, '{arrowdown}')
498
-
499
- await waitFor(() => {
500
- expect(onRequestShowCalendar).not.toHaveBeenCalled()
501
- })
502
- })
503
-
504
- it('should call onRequestShowCalendar when input receives up arrow event', async () => {
505
- const onRequestShowCalendar = vi.fn()
506
-
507
- render(
508
- <DateInput
509
- renderLabel="Choose date"
510
- renderWeekdayLabels={weekdayLabels}
511
- onRequestShowCalendar={onRequestShowCalendar}
512
- >
513
- {generateDays()}
514
- </DateInput>
515
- )
516
- const dateInput = screen.getByLabelText('Choose date')
517
-
518
- await userEvent.type(dateInput, '{arrowup}')
519
-
520
- await waitFor(() => {
521
- expect(onRequestShowCalendar).toHaveBeenCalledTimes(1)
522
- })
523
- })
524
-
525
- it('should not call onRequestShowCalendar when input receives up arrow event if calendar is already showing', async () => {
526
- const onRequestShowCalendar = vi.fn()
527
-
528
- render(
529
- <DateInput
530
- renderLabel="Choose date"
531
- renderWeekdayLabels={weekdayLabels}
532
- onRequestShowCalendar={onRequestShowCalendar}
533
- isShowingCalendar
534
- >
535
- {generateDays()}
536
- </DateInput>
537
- )
538
- const dateInput = screen.getByLabelText('Choose date')
539
-
540
- await userEvent.type(dateInput, '{arrowup}')
541
-
542
- await waitFor(() => {
543
- expect(onRequestShowCalendar).not.toHaveBeenCalled()
544
- })
545
- })
546
-
547
- it('should call onRequestShowCalendar when input receives onChange event', async () => {
548
- const onRequestShowCalendar = vi.fn()
549
-
550
- render(
551
- <DateInput
552
- renderLabel="Choose date"
553
- renderWeekdayLabels={weekdayLabels}
554
- onRequestShowCalendar={onRequestShowCalendar}
555
- >
556
- {generateDays()}
557
- </DateInput>
558
- )
559
- const dateInput = screen.getByLabelText('Choose date')
560
-
561
- fireEvent.change(dateInput, { target: { value: 'January 5' } })
562
-
563
- await waitFor(() => {
564
- expect(onRequestShowCalendar).toHaveBeenCalledTimes(1)
565
- })
566
- })
567
-
568
- it('should not call onRequestShowCalendar when disabled', async () => {
569
- const onRequestShowCalendar = vi.fn()
570
-
571
- render(
572
- <DateInput
573
- renderLabel="Choose date"
574
- renderWeekdayLabels={weekdayLabels}
575
- onRequestShowCalendar={onRequestShowCalendar}
576
- interaction="disabled"
577
- >
578
- {generateDays()}
579
- </DateInput>
580
- )
581
- const dateInput = screen.getByLabelText('Choose date')
582
-
583
- fireEvent.click(dateInput)
584
-
585
- await userEvent.type(
586
- dateInput,
587
- '{arrowup}{arrowdown}{space}January 5',
588
- { skipClick: true }
589
- )
590
-
591
- await waitFor(() => {
592
- expect(onRequestShowCalendar).not.toHaveBeenCalled()
593
- })
594
- })
595
- })
596
-
597
- describe('onRequestHideCalendar and onRequestValidateDate', () => {
598
- it('should call onRequestHideCalendar and onRequestValidateDate input receives onBlur event', async () => {
599
- const onRequestHideCalendar = vi.fn()
600
- const onRequestValidateDate = vi.fn()
601
-
602
- render(
603
- <DateInput
604
- renderLabel="Choose date"
605
- renderWeekdayLabels={weekdayLabels}
606
- onRequestHideCalendar={onRequestHideCalendar}
607
- onRequestValidateDate={onRequestValidateDate}
608
- isShowingCalendar
609
- >
610
- {generateDays()}
611
- </DateInput>
612
- )
613
- const dateInput = screen.getByLabelText('Choose date')
614
-
615
- fireEvent.blur(dateInput)
616
-
617
- await waitFor(() => {
618
- expect(onRequestHideCalendar).toHaveBeenCalledTimes(1)
619
- expect(onRequestValidateDate).toHaveBeenCalledTimes(1)
620
- })
621
- })
622
-
623
- it('should call onRequestHideCalendar and onRequestValidateDate when input receives esc event', async () => {
624
- const onRequestHideCalendar = vi.fn()
625
- const onRequestValidateDate = vi.fn()
626
-
627
- render(
628
- <DateInput
629
- renderLabel="Choose date"
630
- renderWeekdayLabels={weekdayLabels}
631
- onRequestHideCalendar={onRequestHideCalendar}
632
- onRequestValidateDate={onRequestValidateDate}
633
- isShowingCalendar
634
- >
635
- {generateDays()}
636
- </DateInput>
637
- )
638
- const dateInput = screen.getByLabelText('Choose date')
639
-
640
- await userEvent.type(dateInput, '{esc}')
641
-
642
- await waitFor(() => {
643
- expect(onRequestHideCalendar).toHaveBeenCalledTimes(1)
644
- expect(onRequestValidateDate).toHaveBeenCalledTimes(1)
645
- })
646
- })
647
-
648
- it('should call onRequestHideCalendar and onRequestValidateDate when input receives enter event', async () => {
649
- const onRequestHideCalendar = vi.fn()
650
- const onRequestValidateDate = vi.fn()
651
-
652
- const days = generateDays()
653
- days[4] = (
654
- <Calendar.Day key="4" label="4" date="2019-09-28" isSelected>
655
- {4}
656
- </Calendar.Day>
657
- )
658
-
659
- render(
660
- <DateInput
661
- renderLabel="Choose date"
662
- renderWeekdayLabels={weekdayLabels}
663
- onRequestHideCalendar={onRequestHideCalendar}
664
- onRequestValidateDate={onRequestValidateDate}
665
- isShowingCalendar
666
- >
667
- {days}
668
- </DateInput>
669
- )
670
- const dateInput = screen.getByLabelText('Choose date')
671
-
672
- await userEvent.type(dateInput, '{enter}')
673
-
674
- await waitFor(() => {
675
- expect(onRequestHideCalendar).toHaveBeenCalledTimes(1)
676
- expect(onRequestValidateDate).toHaveBeenCalledTimes(1)
677
- })
678
- })
679
- })
680
-
681
- it('should render calendar navigation label', () => {
682
- const label = 'January 2019'
683
-
684
- render(
685
- <DateInput
686
- renderLabel="Choose date"
687
- renderWeekdayLabels={weekdayLabels}
688
- renderNavigationLabel={<div data-testId="label-id">{label}</div>}
689
- isShowingCalendar
690
- >
691
- {generateDays()}
692
- </DateInput>
693
- )
694
- const navigationLabel = screen.getByTestId('label-id')
695
-
696
- expect(navigationLabel).toBeInTheDocument()
697
- expect(navigationLabel).toHaveTextContent(label)
698
- })
699
-
700
- it('should render calendar weekday labels', async () => {
701
- render(
702
- <DateInput
703
- renderLabel="Choose date"
704
- renderWeekdayLabels={weekdayLabels}
705
- isShowingCalendar
706
- >
707
- {generateDays()}
708
- </DateInput>
709
- )
710
- const calendar = await screen.findByRole('listbox')
711
- const headers = calendar.querySelectorAll('th')
712
-
713
- expect(headers.length).toEqual(7)
714
-
715
- weekdayLabels.forEach((label) => {
716
- expect(calendar).toHaveTextContent(label)
717
- })
718
- })
719
-
720
- it('should render all focusable elements in calendar with tabIndex="-1"', async () => {
721
- render(
722
- <DateInput
723
- renderLabel="Choose date"
724
- renderWeekdayLabels={weekdayLabels}
725
- renderNextMonthButton={
726
- <button data-testId="button-next">next</button>
727
- }
728
- renderPrevMonthButton={
729
- <button data-testId="button-prev">prev</button>
730
- }
731
- isShowingCalendar
732
- >
733
- {generateDays()}
734
- </DateInput>
735
- )
736
- const calendar = await screen.findByRole('listbox')
737
- const calendarDays = calendar.querySelectorAll('button')
738
- const nextMonthButton = screen.getByTestId('button-next')
739
- const prevMonthButton = screen.getByTestId('button-prev')
740
-
741
- expect(nextMonthButton).toHaveAttribute('tabIndex', '-1')
742
- expect(prevMonthButton).toHaveAttribute('tabIndex', '-1')
743
- expect(calendarDays).toHaveLength(42)
744
-
745
- calendarDays.forEach((day) => {
746
- expect(day).toHaveAttribute('tabIndex', '-1')
747
- })
748
- })
749
-
750
- it('should render days with the correct role', async () => {
751
- const days = generateDays()
752
- days[5] = (
753
- <Calendar.Day key="5" label="5" date="2019-09-28" id="5" isOutsideMonth>
754
- outside
755
- </Calendar.Day>
756
- )
757
-
758
- render(
759
- <DateInput
760
- renderLabel="Choose date"
761
- renderWeekdayLabels={weekdayLabels}
762
- isShowingCalendar
763
- >
764
- {days}
765
- </DateInput>
766
- )
767
- const calendar = await screen.findByRole('listbox')
768
- const calendarDays = calendar.querySelectorAll('button')
769
-
770
- calendarDays.forEach((day) => {
771
- if (day.textContent!.includes('outside')) {
772
- expect(day).toHaveAttribute('role', 'presentation')
773
- } else {
774
- expect(day).toHaveAttribute('role', 'option')
775
- }
776
- })
777
- })
778
-
779
- it('should assign aria-selected to the selected date and link it to the input', async () => {
780
- const days = generateDays()
781
- days[7] = (
782
- <Calendar.Day
783
- key="7"
784
- label="7"
785
- date="2019-09-28"
786
- id="selected-day-id"
787
- isSelected
788
- >
789
- selected
790
- </Calendar.Day>
791
- )
792
-
793
- render(
794
- <DateInput
795
- renderLabel="Choose date"
796
- renderWeekdayLabels={weekdayLabels}
797
- isShowingCalendar
798
- >
799
- {days}
800
- </DateInput>
801
- )
802
- const calendar = await screen.findByRole('listbox')
803
- const calendarDays = calendar.querySelectorAll('button')
804
- let selectedDayID = ''
805
-
806
- calendarDays.forEach((day) => {
807
- if (day.textContent!.includes('selected')) {
808
- selectedDayID = day.id
809
- expect(day).toHaveAttribute('aria-selected', 'true')
810
- } else {
811
- expect(day).toHaveAttribute('aria-selected', 'false')
812
- }
813
- })
814
-
815
- const dateInput = screen.getByLabelText('Choose date')
816
- expect(dateInput).toHaveAttribute('aria-activedescendant', selectedDayID)
817
- })
818
- })
819
-
820
- describe('with generated examples', () => {
821
- const generatedComponents = generateA11yTests(DateInput, Examples)
822
-
823
- it.each(generatedComponents)(
824
- 'should be accessible with example: $description',
825
- async ({ content }) => {
826
- const { container } = render(content)
827
- const axeCheck = await runAxeCheck(container)
828
- expect(axeCheck).toBe(true)
829
- }
830
- )
831
- })
832
-
833
- describe('with minimal config', () => {
834
- it('should render 44 buttons (a calendar) when clicked', async () => {
835
- const onChange = vi.fn()
836
- render(
837
- <DateInput
838
- renderLabel="Choose date"
839
- assistiveText="Type a date or use arrow keys to navigate date picker."
840
- width="20rem"
841
- isInline
842
- value={'2023-11-23'}
843
- onChange={onChange}
844
- currentDate="2023-12-23"
845
- disabledDates={['2023-12-22', '2023-12-12', '2023-12-11']}
846
- disabledDateErrorMessage="disabled date"
847
- invalidDateErrorMessage="invalid date"
848
- ></DateInput>
849
- )
850
- const dateInput = screen.getByLabelText('Choose date')
851
-
852
- fireEvent.click(dateInput)
853
-
854
- const calendarTable = document.querySelector('table')
855
- const calendarDays = calendarTable!.querySelectorAll('button')
856
- const calendarWrapper = document.querySelector(
857
- '[id^="Selectable_"][id$="-list"]'
858
- )
859
- const calendarButtons = calendarWrapper!.querySelectorAll('button')
860
-
861
- await waitFor(() => {
862
- expect(calendarButtons).toHaveLength(44)
863
- expect(calendarDays).toHaveLength(42)
864
- })
865
- })
866
- })
867
- })