|
1 | 1 | import * as React from 'react';
|
2 | 2 | import { Calendar } from './calendar';
|
3 | 3 | import { prettyDOM, render } from '@testing-library/react';
|
| 4 | +import user from '@testing-library/user-event'; |
4 | 5 | import '@testing-library/jest-dom';
|
5 | 6 | import { CalendarSpectrum } from './Calendar-spectrum';
|
6 | 7 | import { screenTest } from '../screen';
|
@@ -71,6 +72,55 @@ describe('Calendar', () => {
|
71 | 72 | expect(dispatch).toHaveBeenCalledTimes(1);
|
72 | 73 | });
|
73 | 74 | });
|
| 75 | + |
| 76 | + describe('keyboard navigation', () => { |
| 77 | + beforeEach(async () => { |
| 78 | + await user.tab(); // Previous |
| 79 | + await user.tab(); // Next |
| 80 | + await user.tab(); // Cells in Grid |
| 81 | + }); |
| 82 | + |
| 83 | + it('focuses on selected day', () => { |
| 84 | + const selectedDay = screenTest(Calendar.dayGridCell().selected); |
| 85 | + expect(screenTest(Calendar.dayButton(), selectedDay)).toHaveFocus(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('can go left', async () => { |
| 89 | + await user.keyboard('{ArrowLeft}'); |
| 90 | + expect( |
| 91 | + screenTest(Calendar.dayButton(/February 2, 2020/)) |
| 92 | + ).toHaveFocus(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('can go right', async () => { |
| 96 | + await user.keyboard('{ArrowRight}'); |
| 97 | + expect( |
| 98 | + screenTest(Calendar.dayButton(/February 4, 2020/)) |
| 99 | + ).toHaveFocus(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('can select left with Enter key', async () => { |
| 103 | + await user.keyboard('{ArrowLeft}{Enter}'); |
| 104 | + const selectedDay = screenTest(Calendar.dayGridCell().selected); |
| 105 | + expect( |
| 106 | + screenTest(Calendar.dayButton(/February 2, 2020/), selectedDay) |
| 107 | + ).toHaveFocus(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('can select right with Enter key', async () => { |
| 111 | + await user.keyboard('{ArrowRight}{Enter}'); |
| 112 | + expect( |
| 113 | + screenTest(Calendar.dayButton(/February 4, 2020/)) |
| 114 | + ).toHaveFocus(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('focuses next week after pressing right arrow 7 times', async () => { |
| 118 | + await user.keyboard(new Array(7).fill('{ArrowRight}').join('')); |
| 119 | + expect( |
| 120 | + screenTest(Calendar.dayButton(/February 10, 2020/)) |
| 121 | + ).toHaveFocus(); |
| 122 | + }); |
| 123 | + }); |
74 | 124 | }
|
75 | 125 | );
|
76 | 126 | });
|
0 commit comments