Skip to content

Commit 6719338

Browse files
feat: improved accessibility skipping to main content
1 parent 79ffe5a commit 6719338

File tree

1 file changed

+42
-31
lines changed

1 file changed

+42
-31
lines changed

src/courseware/course/sequence/sequence-navigation/UnitButton.test.jsx

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react';
22
import { Factory } from 'rosie';
3-
import userEvent from '@testing-library/user-event';
43
import { act, waitFor } from '@testing-library/react';
54
import {
65
fireEvent, initializeTestStore, render, screen,
@@ -32,6 +31,10 @@ describe('Unit Button', () => {
3231
onClick: () => {},
3332
unitIndex: courseMetadata.id,
3433
};
34+
35+
global.requestAnimationFrame = jest.fn((cb) => {
36+
setImmediate(cb);
37+
});
3538
});
3639

3740
it('hides title by default', () => {
@@ -99,53 +102,61 @@ describe('Unit Button', () => {
99102
expect(onClick).toHaveBeenCalledTimes(1);
100103
});
101104

102-
it('calls onClick when Enter key is pressed', async () => {
103-
const onClick = jest.fn();
104-
render(<UnitButton {...mockData} onClick={onClick} />, { wrapWithRouter: true });
105-
106-
await act(async () => {
107-
await userEvent.keyboard('{Enter}');
108-
});
105+
it('focuses the bookmark button after key press', async () => {
106+
jest.useFakeTimers();
109107

110-
expect(onClick).toHaveBeenCalledTimes(1);
111-
expect(onClick).toHaveBeenCalledWith(mockData.unitId);
112-
});
108+
const { container } = render(
109+
<>
110+
<UnitButton {...mockData} />
111+
<button id="bookmark-button" type="button">Bookmark</button>
112+
</>,
113+
{ wrapWithRouter: true },
114+
);
115+
const unitButton = container.querySelector('[role="tabpanel"]');
113116

114-
it('calls onClick when Space key is pressed', async () => {
115-
const onClick = jest.fn();
116-
render(<UnitButton {...mockData} onClick={onClick} />, { wrapWithRouter: true });
117+
fireEvent.keyDown(unitButton, { key: 'Enter' });
117118

118119
await act(async () => {
119-
await userEvent.keyboard(' ');
120+
jest.runAllTimers();
120121
});
121122

122-
expect(onClick).toHaveBeenCalledTimes(1);
123-
expect(onClick).toHaveBeenCalledWith(mockData.unitId);
124-
});
123+
await waitFor(() => {
124+
expect(document.activeElement).toBe(document.getElementById('bookmark-button'));
125+
});
125126

126-
const Component = () => (
127-
<>
128-
<UnitButton {...mockData} onClick={jest.fn()} />
129-
<button id="bookmark-button" type="button">Bookmark</button>
130-
</>
131-
);
127+
jest.useRealTimers();
128+
});
132129

133-
it('focuses the bookmark button after key press', async () => {
134-
jest.useFakeTimers();
130+
it('calls onClick and focuses bookmark button on Enter or Space key press', async () => {
131+
const onClick = jest.fn();
132+
const { container } = render(
133+
<>
134+
<UnitButton {...mockData} onClick={onClick} />
135+
<button id="bookmark-button" type="button">Bookmark</button>
136+
</>,
137+
{ wrapWithRouter: true },
138+
);
135139

136-
const { container } = render(<Component />, { wrapWithRouter: true });
137140
const unitButton = container.querySelector('[role="tabpanel"]');
138141

139-
fireEvent.keyDown(unitButton, { key: 'Enter' });
140-
141142
await act(async () => {
142-
jest.runAllTimers();
143+
fireEvent.keyDown(unitButton, { key: 'Enter' });
143144
});
144145

145146
await waitFor(() => {
147+
expect(requestAnimationFrame).toHaveBeenCalledTimes(2);
148+
expect(onClick).toHaveBeenCalledTimes(1);
146149
expect(document.activeElement).toBe(document.getElementById('bookmark-button'));
147150
});
148151

149-
jest.useRealTimers();
152+
await act(async () => {
153+
fireEvent.keyDown(unitButton, { key: ' ' });
154+
});
155+
156+
await waitFor(() => {
157+
expect(requestAnimationFrame).toHaveBeenCalledTimes(4);
158+
expect(onClick).toHaveBeenCalledTimes(2);
159+
expect(document.activeElement).toBe(document.getElementById('bookmark-button'));
160+
});
150161
});
151162
});

0 commit comments

Comments
 (0)