|
1 | 1 | import React from 'react';
|
2 | 2 | import { Factory } from 'rosie';
|
3 |
| -import userEvent from '@testing-library/user-event'; |
4 | 3 | import { act, waitFor } from '@testing-library/react';
|
5 | 4 | import {
|
6 | 5 | fireEvent, initializeTestStore, render, screen,
|
@@ -32,6 +31,10 @@ describe('Unit Button', () => {
|
32 | 31 | onClick: () => {},
|
33 | 32 | unitIndex: courseMetadata.id,
|
34 | 33 | };
|
| 34 | + |
| 35 | + global.requestAnimationFrame = jest.fn((cb) => { |
| 36 | + setImmediate(cb); |
| 37 | + }); |
35 | 38 | });
|
36 | 39 |
|
37 | 40 | it('hides title by default', () => {
|
@@ -99,53 +102,61 @@ describe('Unit Button', () => {
|
99 | 102 | expect(onClick).toHaveBeenCalledTimes(1);
|
100 | 103 | });
|
101 | 104 |
|
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(); |
109 | 107 |
|
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"]'); |
113 | 116 |
|
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' }); |
117 | 118 |
|
118 | 119 | await act(async () => {
|
119 |
| - await userEvent.keyboard(' '); |
| 120 | + jest.runAllTimers(); |
120 | 121 | });
|
121 | 122 |
|
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 | + }); |
125 | 126 |
|
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 | + }); |
132 | 129 |
|
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 | + ); |
135 | 139 |
|
136 |
| - const { container } = render(<Component />, { wrapWithRouter: true }); |
137 | 140 | const unitButton = container.querySelector('[role="tabpanel"]');
|
138 | 141 |
|
139 |
| - fireEvent.keyDown(unitButton, { key: 'Enter' }); |
140 |
| - |
141 | 142 | await act(async () => {
|
142 |
| - jest.runAllTimers(); |
| 143 | + fireEvent.keyDown(unitButton, { key: 'Enter' }); |
143 | 144 | });
|
144 | 145 |
|
145 | 146 | await waitFor(() => {
|
| 147 | + expect(requestAnimationFrame).toHaveBeenCalledTimes(2); |
| 148 | + expect(onClick).toHaveBeenCalledTimes(1); |
146 | 149 | expect(document.activeElement).toBe(document.getElementById('bookmark-button'));
|
147 | 150 | });
|
148 | 151 |
|
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 | + }); |
150 | 161 | });
|
151 | 162 | });
|
0 commit comments