-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPopup.modules.test.js
115 lines (91 loc) · 3.2 KB
/
Popup.modules.test.js
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
/* eslint-disable max-len */
import user from '@/.jest/user';
import Popup, {
ComponentConnector,
ManageTabIndex,
UseButtonRole,
UseHiddenAttribute,
} from '.';
// Set up our document body
document.body.innerHTML = `
<a aria-controls="dropdown" href="#dropdown" class="link">Open</a>
<span>Break up DOM</span>
<div class="wrapper" id="dropdown">
<ul>
<li><a class="first-child" href="example.com"></a></li>
<li><a href="example.com"></a></li>
<li><a href="example.com"></a></li>
<li><a class="last-child" href="example.com"></a></li>
</ul>
</div>
`;
const controller = document.querySelector('.link');
const target = document.querySelector('.wrapper');
let popup;
beforeEach(() => {
popup = new Popup(
controller,
{
modules: [
ComponentConnector,
ManageTabIndex,
UseButtonRole,
UseHiddenAttribute,
],
}
);
popup.init();
});
const targetFirstChild = document.querySelector('.first-child');
test('ComponentConnector: Connect disconnected controller-target pair', async () => {
expect(controller.getAttribute('aria-owns')).toEqual(target.id);
popup.expanded = true;
expect(popup.expanded).toBe(true);
controller.focus();
await user.keyboard('{Tab}');
expect(document.activeElement).toEqual(targetFirstChild);
await user.keyboard('{Shift>}{Tab}{/Shift}');
expect(document.activeElement).toEqual(controller);
popup.destroy();
expect(controller.getAttribute('aria-owns')).toBeNull();
});
test('ManageTabIndex: Manage target element tabindex', () => {
// Initial state.
popup.interactiveChildElements.forEach((link) => expect(link.getAttribute('tabindex')).toEqual('-1'));
popup.expanded = true;
expect(popup.expanded).toBe(true);
popup.interactiveChildElements.forEach((link) => expect(link.getAttribute('tabindex')).toBeNull());
popup.expanded = false;
expect(popup.expanded).toBe(false);
popup.interactiveChildElements.forEach((link) => expect(link.getAttribute('tabindex')).toEqual('-1'));
popup.destroy();
popup.interactiveChildElements.forEach((link) => expect(link.getAttribute('tabindex')).toBeNull());
});
test('UseButtonRole: Treats non-button controller as a button', async () => {
expect(controller.getAttribute('role')).toBe('button');
expect(controller.getAttribute('tabindex')).not.toBe('0');
// Verify initial state.
expect(popup.expanded).toBe(false);
// Enter activates the Popup.
await user.keyboard('{Enter}');
expect(popup.expanded).toBe(true);
// Spacebar activates the Popup.
await user.keyboard('{ }');
expect(popup.expanded).toBe(false);
// Module cleanup.
popup.destroy();
expect(controller.getAttribute('role')).toBeNull();
expect(controller.getAttribute('tabindex')).toBeNull();
});
test('UseHiddenAttribute: Uses hidden attribute when target not expanded', () => {
expect(popup.expanded).toBe(false);
expect(target.getAttribute('hidden')).toBe('');
popup.expanded = true;
expect(popup.expanded).toBe(true);
expect(target.getAttribute('hidden')).toBeNull();
popup.expanded = false;
expect(popup.expanded).toBe(false);
expect(target.getAttribute('hidden')).toBe('');
popup.destroy();
expect(target.getAttribute('hidden')).toBeNull();
});