Skip to content

Commit 34e2ba2

Browse files
committed
Calendar keyboard navigation tests
1 parent d908350 commit 34e2ba2

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/roles/calendar.test.tsx

+50
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { Calendar } from './calendar';
33
import { prettyDOM, render } from '@testing-library/react';
4+
import user from '@testing-library/user-event';
45
import '@testing-library/jest-dom';
56
import { CalendarSpectrum } from './Calendar-spectrum';
67
import { screenTest } from '../screen';
@@ -71,6 +72,55 @@ describe('Calendar', () => {
7172
expect(dispatch).toHaveBeenCalledTimes(1);
7273
});
7374
});
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+
});
74124
}
75125
);
76126
});

0 commit comments

Comments
 (0)