-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDialog.modules.test.js
More file actions
160 lines (135 loc) · 4.43 KB
/
Copy pathDialog.modules.test.js
File metadata and controls
160 lines (135 loc) · 4.43 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
/* eslint-disable max-len */
import user from '@/.jest/user';
import Dialog, {
ManageTabIndex,
UseButtonRole,
UseHiddenAttribute,
UseLegacyDialog,
} from '.';
// Set up our document body
document.body.innerHTML = `
<main>
<article>
<h1>The Article Title</h1>
<a href="#" class="outside-link">Link</a>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur
sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.</p>
<a aria-controls="dialog" class="link" href="#dialog">Open dialog</a>
</article>
</main>
<footer class="site-footer">Site footer</footer>
<div class="wrapper" id="dialog" tabindex="0">
<button>Close</button>
<ul>
<li><a href="example.com"></a></li>
<li><a href="example.com"></a></li>
<li><a href="example.com"></a></li>
<li><a class="last-item" href="example.com"></a></li>
</ul>
</div>
`;
const controller = document.querySelector('[aria-controls="dialog"]');
const target = document.getElementById('dialog');
const content = document.querySelector('main');
const footer = document.querySelector('footer');
let dialog;
test('ManageTabIndex: Manage target element tabindex', () => {
dialog = new Dialog(
controller,
{
modules: [
ManageTabIndex,
],
}
);
dialog.interactiveChildElements.forEach((link) => {
expect(link.getAttribute('tabindex')).toEqual('-1');
});
dialog.expanded = true;
expect(dialog.expanded).toBe(true);
dialog.interactiveChildElements.forEach((link) => {
expect(link.getAttribute('tabindex')).toBeNull();
});
dialog.expanded = false;
expect(dialog.expanded).toBe(false);
dialog.interactiveChildElements.forEach((link) => {
expect(link.getAttribute('tabindex')).toEqual('-1');
});
dialog.destroy();
dialog.interactiveChildElements.forEach((link) => {
expect(link.getAttribute('tabindex')).toBeNull();
});
});
test('UseButtonRole: Treats non-button controller as a button', async () => {
dialog = new Dialog(
controller,
{
modules: [
UseButtonRole,
],
}
);
expect(controller.getAttribute('role')).toBe('button');
expect(controller.getAttribute('tabindex')).not.toBe('0');
// Verify Dialog state.
expect(dialog.expanded).toBe(false);
// Enter activates the Dialog.
await user.keyboard('{Enter}');
expect(dialog.expanded).toBe(true);
// Spacebar activates the Dialog.
await user.keyboard('{ }');
expect(dialog.expanded).toBe(true);
// Module cleanup.
dialog.destroy();
expect(controller.getAttribute('role')).toBeNull();
expect(controller.getAttribute('tabindex')).toBeNull();
});
test('UseHiddenAttribute: Uses hidden attribute when target not expanded', () => {
dialog = new Dialog(
controller,
{
modules: [
UseHiddenAttribute,
],
}
);
expect(dialog.expanded).toBe(false);
expect(target.getAttribute('hidden')).toBe('');
dialog.expanded = true;
expect(dialog.expanded).toBe(true);
expect(target.getAttribute('hidden')).toBeNull();
dialog.expanded = false;
expect(dialog.expanded).toBe(false);
expect(target.getAttribute('hidden')).toBe('');
dialog.destroy();
expect(target.getAttribute('hidden')).toBeNull();
});
test('UseLegacyDialog: Uses aria-hidden on external content', () => {
dialog = new Dialog(
controller,
{
modules: [
UseLegacyDialog,
],
}
);
expect(target.getAttribute('aria-hidden')).toBe('true');
expect(footer.getAttribute('aria-hidden')).toBeNull();
expect(content.getAttribute('aria-hidden')).toBeNull();
dialog.expanded = true;
expect(target.getAttribute('aria-hidden')).toBe('false');
expect(footer.getAttribute('aria-hidden')).toEqual('true');
expect(content.getAttribute('aria-hidden')).toEqual('true');
dialog.expanded = false;
expect(target.getAttribute('aria-hidden')).toBe('true');
expect(footer.getAttribute('aria-hidden')).toBeNull();
expect(content.getAttribute('aria-hidden')).toBeNull();
dialog.destroy();
expect(footer.getAttribute('aria-hidden')).toBeNull();
expect(content.getAttribute('aria-hidden')).toBeNull();
});