-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDropdown.spec.tsx
More file actions
337 lines (277 loc) · 11.7 KB
/
Copy pathDropdown.spec.tsx
File metadata and controls
337 lines (277 loc) · 11.7 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import React from 'react';
import renderer from 'react-test-renderer';
import * as focusUtils from '../reactUtils/focusUtils';
import * as keyboardUtils from '../reactUtils/keyboardUtils';
import TestContainer from '../../test/TestContainer';
import Dropdown, { DropdownItem, DropdownList, callOrRefocus } from './Dropdown';
describe('Dropdown', () => {
it('matches snapshot', () => {
const component = renderer.create(<TestContainer>
<Dropdown toggle={<button>show more</button>}>
<DropdownList>
<DropdownItem onClick={() => null} message='i18n:highlighting:dropdown:delete' />
<DropdownItem onClick={() => null} href='/wooo' message='i18n:highlighting:dropdown:edit' />
</DropdownList>
</Dropdown>
</TestContainer>);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
it('matches snapshot for tab hidden (closed)', () => {
const component = renderer.create(<TestContainer>
<Dropdown transparentTab={false} toggle={<button>show more</button>}>
<DropdownList>
<DropdownItem onClick={() => null} message='i18n:highlighting:dropdown:delete' />
<DropdownItem onClick={() => null} href='/wooo' message='i18n:highlighting:dropdown:edit' />
</DropdownList>
</Dropdown>
</TestContainer>);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
it('matches snapshot for tab hidden (open)', () => {
const component = renderer.create(<TestContainer>
<Dropdown transparentTab={false} toggle={<button>show more</button>}>
<DropdownList>
<DropdownItem onClick={() => null} message='i18n:highlighting:dropdown:delete' />
<DropdownItem onClick={() => null} href='/wooo' message='i18n:highlighting:dropdown:edit' />
</DropdownList>
</Dropdown>
</TestContainer>);
renderer.act(() => {
component.root.findByType('button').props.onClick();
});
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
it('tab hidden closes on focus lost', () => {
const useFocusLost = jest.spyOn(focusUtils, 'useFocusLost');
const component = renderer.create(<TestContainer>
<Dropdown transparentTab={false} toggle={<button>show more</button>}>
<DropdownList>
<DropdownItem onClick={() => null} message='i18n:highlighting:dropdown:delete' />
<DropdownItem onClick={() => null} href='/wooo' message='i18n:highlighting:dropdown:edit' />
</DropdownList>
</Dropdown>
</TestContainer>);
renderer.act(() => {
component.root.findByType('button').props.onClick();
});
expect(() => component.root.findByType(DropdownList)).not.toThrow();
renderer.act(() => {
useFocusLost.mock.calls[0][2]();
});
expect(() => component.root.findByType(DropdownList)).toThrow();
});
it('callOrRefocus refocuses when container.match(:hover)', () => {
const cb = jest.fn();
const matches = jest.fn().mockReturnValue(true);
const focus = jest.fn();
// @ts-expect-error not HTMLElements
callOrRefocus(cb, {matches}, {focus});
expect(cb).not.toHaveBeenCalled();
expect(focus).toHaveBeenCalled();
});
it('tab hidden closes on Esc', () => {
const useOnEscSpy = jest.spyOn(keyboardUtils, 'useOnEsc');
const component = renderer.create(<TestContainer>
<Dropdown transparentTab={false} toggle={<button>show more</button>}>
<DropdownList>
<DropdownItem onClick={() => null} message='i18n:highlighting:dropdown:delete' />
<DropdownItem onClick={() => null} href='/wooo' message='i18n:highlighting:dropdown:edit' />
</DropdownList>
</Dropdown>
</TestContainer>);
renderer.act(() => {
component.root.findByType('button').props.onClick();
});
expect(() => component.root.findByType(DropdownList)).not.toThrow();
renderer.act(() => {
useOnEscSpy.mock.calls[0][1]();
});
expect(() => component.root.findByType(DropdownList)).toThrow();
useOnEscSpy.mockClear();
});
it('tab hidden focus after Esc', () => {
const useOnEscSpy = jest.spyOn(keyboardUtils, 'useOnEsc');
const focus = jest.fn();
const focus2 = jest.fn();
const addEventListener = jest.fn();
const removeEventListener = jest.fn();
const createNodeMock = () => ({focus, addEventListener, removeEventListener});
const component = renderer.create(<TestContainer>
<Dropdown transparentTab={false} toggle={<button>show more</button>}>
<DropdownList>
<DropdownItem onClick={() => null} message='i18n:highlighting:dropdown:delete' />
<DropdownItem onClick={() => null} href='/wooo' message='i18n:highlighting:dropdown:edit' />
</DropdownList>
</Dropdown>
</TestContainer>, {createNodeMock});
renderer.act(() => {
component.root.findByType('button').props.onClick();
});
expect(() => component.root.findByType(DropdownList)).not.toThrow();
renderer.act(() => {
const buttons = component.root.findAllByType('button');
buttons[1].props.onMouseEnter({target: {focus: focus2}});
});
renderer.act(() => {
useOnEscSpy.mock.calls[0][1]();
});
expect(() => component.root.findByType(DropdownList)).toThrow();
expect(focus).toHaveBeenCalled();
expect(focus2).toHaveBeenCalled();
useOnEscSpy.mockClear();
});
it(`items have onClick function even if it wasn't passed as prop`, () => {
const mockEv = { preventDefault: jest.fn() };
const component = renderer.create(<TestContainer>
<Dropdown toggle={<button>show more</button>}>
<DropdownList>
<DropdownItem message='i18n:highlighting:dropdown:delete' />
<DropdownItem onClick={() => null} message='i18n:highlighting:dropdown:delete' />
</DropdownList>
</Dropdown>
</TestContainer>);
renderer.act(() => {
const items = component.root.findAll((i) => i.props.onClick && i.type === 'button');
items.forEach((i) => i.props.onClick(mockEv));
expect(items.length).toBe(2);
});
expect(mockEv.preventDefault).toHaveBeenCalledTimes(2);
});
it('TabTransparentDropdown handles focus in', () => {
const component = renderer.create(<TestContainer>
<Dropdown toggle={<button>show more</button>}>
<DropdownList>
<DropdownItem onClick={() => null} message='i18n:highlighting:dropdown:delete' />
</DropdownList>
</Dropdown>
</TestContainer>);
// Find the dropdown focus wrapper div
const focusWrapper = component.root.findAll(
(el) => el.props.className && el.props.className.includes('dropdown-focus-wrapper')
)[0];
// Initially, focus-within should not be present
expect(focusWrapper.props.className).not.toContain('focus-within');
// Simulate focus in
renderer.act(() => {
focusWrapper.props.onFocus();
});
// After focus in, focus-within should be present
const updatedFocusWrapper = component.root.findAll(
(el) => el.props.className && el.props.className.includes('dropdown-focus-wrapper')
)[0];
expect(updatedFocusWrapper.props.className).toContain('focus-within');
});
it('TabTransparentDropdown handles focus out when focus moves outside', () => {
const component = renderer.create(<TestContainer>
<Dropdown toggle={<button>show more</button>}>
<DropdownList>
<DropdownItem onClick={() => null} message='i18n:highlighting:dropdown:delete' />
</DropdownList>
</Dropdown>
</TestContainer>);
// Find the dropdown focus wrapper div
const focusWrapper = component.root.findAll(
(el) => el.props.className && el.props.className.includes('dropdown-focus-wrapper')
)[0];
// Simulate focus in first
renderer.act(() => {
focusWrapper.props.onFocus();
});
// Verify focus-within is present
let updatedFocusWrapper = component.root.findAll(
(el) => el.props.className && el.props.className.includes('dropdown-focus-wrapper')
)[0];
expect(updatedFocusWrapper.props.className).toContain('focus-within');
// Simulate focus out to an element outside the dropdown (relatedTarget is outside)
renderer.act(() => {
const outsideElement = document?.createElement('div');
focusWrapper.props.onBlur({
currentTarget: {
contains: jest.fn().mockReturnValue(false),
},
relatedTarget: outsideElement,
});
});
// After focus out, focus-within should be removed
updatedFocusWrapper = component.root.findAll(
(el) => el.props.className && el.props.className.includes('dropdown-focus-wrapper')
)[0];
expect(updatedFocusWrapper.props.className).not.toContain('focus-within');
});
it('TabTransparentDropdown handles focus out when relatedTarget is null', () => {
const component = renderer.create(<TestContainer>
<Dropdown toggle={<button>show more</button>}>
<DropdownList>
<DropdownItem onClick={() => null} message='i18n:highlighting:dropdown:delete' />
</DropdownList>
</Dropdown>
</TestContainer>);
// Find the dropdown focus wrapper div
const focusWrapper = component.root.findAll(
(el) => el.props.className && el.props.className.includes('dropdown-focus-wrapper')
)[0];
// Simulate focus in first
renderer.act(() => {
focusWrapper.props.onFocus();
});
// Verify focus-within is present
let updatedFocusWrapper = component.root.findAll(
(el) => el.props.className && el.props.className.includes('dropdown-focus-wrapper')
)[0];
expect(updatedFocusWrapper.props.className).toContain('focus-within');
// Simulate focus out with null relatedTarget (focus moved to browser UI or another app)
renderer.act(() => {
focusWrapper.props.onBlur({
currentTarget: {
contains: jest.fn(),
},
relatedTarget: null,
});
});
// After focus out with null relatedTarget, focus-within should be removed
updatedFocusWrapper = component.root.findAll(
(el) => el.props.className && el.props.className.includes('dropdown-focus-wrapper')
)[0];
expect(updatedFocusWrapper.props.className).not.toContain('focus-within');
});
it('TabTransparentDropdown maintains focus when moving within the dropdown', () => {
const component = renderer.create(<TestContainer>
<Dropdown toggle={<button>show more</button>}>
<DropdownList>
<DropdownItem onClick={() => null} message='i18n:highlighting:dropdown:delete' />
</DropdownList>
</Dropdown>
</TestContainer>);
// Find the dropdown focus wrapper div
const focusWrapper = component.root.findAll(
(el) => el.props.className && el.props.className.includes('dropdown-focus-wrapper')
)[0];
// Simulate focus in first
renderer.act(() => {
focusWrapper.props.onFocus();
});
// Verify focus-within is present
let updatedFocusWrapper = component.root.findAll(
(el) => el.props.className && el.props.className.includes('dropdown-focus-wrapper')
)[0];
expect(updatedFocusWrapper.props.className).toContain('focus-within');
// Simulate focus moving to another element within the dropdown (relatedTarget is inside)
renderer.act(() => {
const insideElement = document?.createElement('div');
focusWrapper.props.onBlur({
currentTarget: {
contains: jest.fn().mockReturnValue(true),
},
relatedTarget: insideElement,
});
});
// Focus-within should still be present because focus is still within the dropdown
updatedFocusWrapper = component.root.findAll(
(el) => el.props.className && el.props.className.includes('dropdown-focus-wrapper')
)[0];
expect(updatedFocusWrapper.props.className).toContain('focus-within');
});
});