|
1 | 1 | import React from 'react';
|
2 | 2 | import { Factory } from 'rosie';
|
| 3 | +import { act, waitFor } from '@testing-library/react'; |
3 | 4 | import {
|
4 | 5 | fireEvent, initializeTestStore, render, screen,
|
5 | 6 | } from '../../../../setupTest';
|
@@ -28,17 +29,35 @@ describe('Unit Button', () => {
|
28 | 29 | mockData = {
|
29 | 30 | unitId: unit.id,
|
30 | 31 | onClick: () => {},
|
| 32 | + unitIndex: courseMetadata.id, |
31 | 33 | };
|
| 34 | + |
| 35 | + global.requestAnimationFrame = jest.fn((cb) => { |
| 36 | + setImmediate(cb); |
| 37 | + }); |
32 | 38 | });
|
33 | 39 |
|
34 | 40 | it('hides title by default', () => {
|
35 | 41 | render(<UnitButton {...mockData} />, { wrapWithRouter: true });
|
36 |
| - expect(screen.getByRole('link')).not.toHaveTextContent(unit.display_name); |
| 42 | + expect(screen.getByRole('tabpanel')).not.toHaveTextContent(unit.display_name); |
37 | 43 | });
|
38 | 44 |
|
39 | 45 | it('shows title', () => {
|
40 | 46 | render(<UnitButton {...mockData} showTitle />, { wrapWithRouter: true });
|
41 |
| - expect(screen.getByRole('link')).toHaveTextContent(unit.display_name); |
| 47 | + expect(screen.getByRole('tabpanel')).toHaveTextContent(unit.display_name); |
| 48 | + }); |
| 49 | + |
| 50 | + it('check button attributes', () => { |
| 51 | + render(<UnitButton {...mockData} showTitle />, { wrapWithRouter: true }); |
| 52 | + expect(screen.getByRole('tabpanel')).toHaveAttribute('id', `${unit.display_name}-${courseMetadata.id}`); |
| 53 | + expect(screen.getByRole('tabpanel')).toHaveAttribute('aria-controls', unit.display_name); |
| 54 | + expect(screen.getByRole('tabpanel')).toHaveAttribute('aria-labelledby', unit.display_name); |
| 55 | + expect(screen.getByRole('tabpanel')).toHaveAttribute('tabindex', '-1'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('button with isActive prop has tabindex 0', () => { |
| 59 | + render(<UnitButton {...mockData} isActive />, { wrapWithRouter: true }); |
| 60 | + expect(screen.getByRole('tabpanel')).toHaveAttribute('tabindex', '0'); |
42 | 61 | });
|
43 | 62 |
|
44 | 63 | it('does not show completion for non-completed unit', () => {
|
@@ -79,7 +98,65 @@ describe('Unit Button', () => {
|
79 | 98 | it('handles the click', () => {
|
80 | 99 | const onClick = jest.fn();
|
81 | 100 | render(<UnitButton {...mockData} onClick={onClick} />, { wrapWithRouter: true });
|
82 |
| - fireEvent.click(screen.getByRole('link')); |
| 101 | + fireEvent.click(screen.getByRole('tabpanel')); |
83 | 102 | expect(onClick).toHaveBeenCalledTimes(1);
|
84 | 103 | });
|
| 104 | + |
| 105 | + it('focuses the bookmark button after key press', async () => { |
| 106 | + jest.useFakeTimers(); |
| 107 | + |
| 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"]'); |
| 116 | + |
| 117 | + fireEvent.keyDown(unitButton, { key: 'Enter' }); |
| 118 | + |
| 119 | + await act(async () => { |
| 120 | + jest.runAllTimers(); |
| 121 | + }); |
| 122 | + |
| 123 | + await waitFor(() => { |
| 124 | + expect(document.activeElement).toBe(document.getElementById('bookmark-button')); |
| 125 | + }); |
| 126 | + |
| 127 | + jest.useRealTimers(); |
| 128 | + }); |
| 129 | + |
| 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 | + ); |
| 139 | + |
| 140 | + const unitButton = container.querySelector('[role="tabpanel"]'); |
| 141 | + |
| 142 | + await act(async () => { |
| 143 | + fireEvent.keyDown(unitButton, { key: 'Enter' }); |
| 144 | + }); |
| 145 | + |
| 146 | + await waitFor(() => { |
| 147 | + expect(requestAnimationFrame).toHaveBeenCalledTimes(2); |
| 148 | + expect(onClick).toHaveBeenCalledTimes(1); |
| 149 | + expect(document.activeElement).toBe(document.getElementById('bookmark-button')); |
| 150 | + }); |
| 151 | + |
| 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 | + }); |
| 161 | + }); |
85 | 162 | });
|
0 commit comments