-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathUnitButton.test.jsx
More file actions
167 lines (141 loc) · 5.4 KB
/
UnitButton.test.jsx
File metadata and controls
167 lines (141 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import React from 'react';
import { Factory } from 'rosie';
import {
act,
fireEvent,
initializeTestStore,
render,
screen,
waitFor,
} from '../../../../setupTest';
import UnitButton from './UnitButton';
import messages from './messages';
describe('Unit Button', () => {
let mockData;
const courseMetadata = Factory.build('courseMetadata');
const unitBlocks = [Factory.build(
'block',
{ type: 'problem' },
{ courseId: courseMetadata.id },
), Factory.build(
'block',
{ type: 'video', complete: true },
{ courseId: courseMetadata.id },
), Factory.build(
'block',
{ type: 'other', complete: true, bookmarked: true },
{ courseId: courseMetadata.id },
)];
const [unit, completedUnit, bookmarkedUnit] = unitBlocks;
beforeAll(async () => {
await initializeTestStore({ courseMetadata, unitBlocks });
mockData = {
unitId: unit.id,
onClick: () => {},
unitIdx: 0,
};
global.requestAnimationFrame = jest.fn((cb) => {
setImmediate(cb);
});
});
it('hides title by default', () => {
render(<UnitButton {...mockData} />, { wrapWithRouter: true });
expect(screen.getByRole('tabpanel')).not.toHaveTextContent(unit.display_name);
});
it('shows title', () => {
render(<UnitButton {...mockData} showTitle />, { wrapWithRouter: true });
expect(screen.getByRole('tabpanel')).toHaveTextContent(unit.display_name);
});
it('check button attributes', () => {
render(<UnitButton {...mockData} showTitle />, { wrapWithRouter: true });
expect(screen.getByRole('tabpanel')).toHaveAttribute('id', `${unit.display_name}-0`);
expect(screen.getByRole('tabpanel')).toHaveAttribute('aria-controls', unit.display_name);
expect(screen.getByRole('tabpanel')).toHaveAttribute('aria-labelledby', unit.display_name);
expect(screen.getByRole('tabpanel')).toHaveAttribute('tabindex', '-1');
});
it('button with isActive prop has tabindex 0', () => {
render(<UnitButton {...mockData} isActive />, { wrapWithRouter: true });
expect(screen.getByRole('tabpanel')).toHaveAttribute('tabindex', '0');
});
it('does not show completion for non-completed unit', () => {
const { container } = render(<UnitButton {...mockData} />);
container.querySelectorAll('svg').forEach(icon => {
expect(icon).not.toHaveClass('fa-check');
});
});
it('shows completion for completed unit', () => {
const { container } = render(<UnitButton {...mockData} unitId={completedUnit.id} />, { wrapWithRouter: true });
const buttonIcons = container.querySelectorAll('svg');
expect(buttonIcons).toHaveLength(2);
expect(buttonIcons[1]).toHaveClass('fa-check');
});
it('hides completion', () => {
const { container } = render(<UnitButton {...mockData} unitId={completedUnit.id} showCompletion={false} />);
container.querySelectorAll('svg').forEach(icon => {
expect(icon).not.toHaveClass('fa-check');
});
});
it('does not show bookmark', () => {
const { queryByTestId } = render(<UnitButton {...mockData} />);
expect(queryByTestId('bookmark-icon')).toBeNull();
});
it('shows bookmark', () => {
const { container } = render(<UnitButton {...mockData} unitId={bookmarkedUnit.id} />, { wrapWithRouter: true });
const buttonIcons = container.querySelectorAll('svg');
expect(buttonIcons).toHaveLength(3);
const bookmarkIcon = buttonIcons[2].closest('span');
expect(bookmarkIcon.getAttribute('data-testid')).toBe('bookmark-icon');
});
it('handles the click', () => {
const onClick = jest.fn();
render(<UnitButton {...mockData} onClick={onClick} />, { wrapWithRouter: true });
fireEvent.click(screen.getByRole('tabpanel'));
expect(onClick).toHaveBeenCalledTimes(1);
});
it('focuses the bookmark button after key press', async () => {
jest.useFakeTimers();
const { container } = render(
<>
<UnitButton {...mockData} />
<button id="bookmark-button" type="button">{messages.bookmark.defaultMessage}</button>
</>,
{ wrapWithRouter: true },
);
const unitButton = container.querySelector('[role="tabpanel"]');
fireEvent.keyDown(unitButton, { key: 'Enter' });
await act(async () => {
jest.runAllTimers();
});
await waitFor(() => {
expect(document.activeElement).toBe(document.getElementById('bookmark-button'));
});
jest.useRealTimers();
});
it('calls onClick and focuses bookmark button on Enter or Space key press', async () => {
const onClick = jest.fn();
const { container } = render(
<>
<UnitButton {...mockData} onClick={onClick} />
<button id="bookmark-button" type="button">{messages.bookmark.defaultMessage}</button>
</>,
{ wrapWithRouter: true },
);
const unitButton = container.querySelector('[role="tabpanel"]');
await act(async () => {
fireEvent.keyDown(unitButton, { key: 'Enter' });
});
await waitFor(() => {
expect(requestAnimationFrame).toHaveBeenCalledTimes(2);
expect(onClick).toHaveBeenCalledTimes(1);
expect(document.activeElement).toBe(document.getElementById('bookmark-button'));
});
await act(async () => {
fireEvent.keyDown(unitButton, { key: ' ' });
});
await waitFor(() => {
expect(requestAnimationFrame).toHaveBeenCalledTimes(4);
expect(onClick).toHaveBeenCalledTimes(2);
expect(document.activeElement).toBe(document.getElementById('bookmark-button'));
});
});
});