-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex-test.js
392 lines (340 loc) · 13.6 KB
/
index-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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import {
render,
click,
resetOnerror,
settled,
triggerKeyEvent,
} from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
class MockEventTarget extends EventTarget {}
module('Integration | Component | hds/side-nav/index', function (hooks) {
setupRenderingTest(hooks);
hooks.beforeEach(function () {
// Mock window.matchMedia to control media query events
let mockMedia = new MockEventTarget();
mockMedia.matches = true;
this.__matchMedia = window.matchMedia;
this.mockMedia = () => {
window.matchMedia = () => mockMedia;
};
this.changeBrowserSize = async (isDesktop) => {
mockMedia.matches = isDesktop;
mockMedia.dispatchEvent(
new MediaQueryListEvent('change', {
matches: isDesktop,
})
);
await settled();
};
});
hooks.afterEach(function () {
resetOnerror();
window.matchMedia = this.__matchMedia;
});
test('it should render the component with a CSS class that matches the component name', async function (assert) {
await render(
hbs`<Hds::SideNav id="test-side-nav" @hasA11yRefocus={{false}} />`
);
assert.dom('#test-side-nav').hasClass('hds-side-nav');
});
// CONTENT
test('it renders content passed to the named blocks', async function (assert) {
await render(hbs`
<Hds::SideNav @hasA11yRefocus={{false}}>
<:header>
<span id="test-side-nav-header" />
</:header>
<:body>
<span id="test-side-nav-body" />
</:body>
<:footer>
<span id="test-side-nav-footer" />
</:footer>
</Hds::SideNav>
`);
assert.dom('#test-side-nav-header').exists();
assert.dom('#test-side-nav-body').exists();
assert.dom('#test-side-nav-footer').exists();
});
// A11Y
test('it renders the `a11y-refocus` elements by default with a default skip link href value of "#hds-main', async function (assert) {
await render(hbs`<Hds::SideNav />`);
assert.dom('#ember-a11y-refocus-nav-message').exists();
assert
.dom('#ember-a11y-refocus-skip-link')
.exists()
.hasAttribute('href', '#hds-main');
});
test('it renders the `a11y-refocus` elements with the right properties provided as arguments', async function (assert) {
await render(hbs`
<Hds::SideNav
@a11yRefocusSkipTo="test-skip-to"
@a11yRefocusSkipText="test-skip-text"
@a11yRefocusNavigationText="test-navigation-text"
/>
`);
assert
.dom('#ember-a11y-refocus-nav-message')
.hasText('test-navigation-text');
assert.dom('#ember-a11y-refocus-skip-link').hasText('test-skip-text');
assert
.dom('#ember-a11y-refocus-skip-link')
.hasAttribute('href', '#test-skip-to');
});
test('it does not render the `a11y-refocus` elements if `hasA11yRefocus` is false', async function (assert) {
await render(hbs`<Hds::SideNav @hasA11yRefocus={{false}} />`);
assert.dom('#ember-a11y-refocus-nav-message').doesNotExist();
assert.dom('#ember-a11y-refocus-skip-link').doesNotExist();
});
// RESPONSIVENESS
test('it is "desktop" by default', async function (assert) {
await render(hbs`<Hds::SideNav id="test-side-nav" />`);
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-desktop');
});
test('it is "responsive" by default', async function (assert) {
await render(hbs`<Hds::SideNav id="test-side-nav" />`);
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-responsive');
});
test('it is not "responsive" if `isResponsive` is false', async function (assert) {
await render(
hbs`<Hds::SideNav id="test-side-nav" @isResponsive={{false}} />`
);
assert
.dom('#test-side-nav')
.doesNotHaveClass('hds-side-nav--is-responsive');
});
// MOBILE
test('it is "mobile" on narrow viewports', async function (assert) {
await render(hbs`
<style>:root {--hds-app-desktop-breakpoint: 10088px}</style>
<Hds::SideNav id="test-side-nav" />
`);
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-mobile');
});
test('it is minimized/collapsed on narrow viewports by default', async function (assert) {
await render(hbs`
<style>:root {--hds-app-desktop-breakpoint: 10088px}</style>
<Hds::SideNav id="test-side-nav" />
`);
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-minimized');
});
test('it is not minimized/collapsed on narrow viewports if `isResponsive` is false', async function (assert) {
await render(hbs`
<style>:root {--hds-app-desktop-breakpoint: 10088px}</style>
<Hds::SideNav id="test-side-nav" @isResponsive={{false}} />
`);
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-not-minimized');
});
test('it shows a toggle button on narrow viewports by default', async function (assert) {
await render(hbs`
<style>:root {--hds-app-desktop-breakpoint: 10088px}</style>
<Hds::SideNav id="test-side-nav" />
`);
assert.dom('.hds-side-nav__toggle-button').exists();
});
test('it does not show a toggle button on narrow viewports if `isResponsive` is false', async function (assert) {
await render(hbs`
<style>:root {--hds-app-desktop-breakpoint: 10088px}</style>
<Hds::SideNav id="test-side-nav" @isResponsive={{false}} />
`);
assert.dom('.hds-side-nav__toggle-button').doesNotExist();
});
test('it expands/collapses when the toggle button is pressed on narrow viewports', async function (assert) {
await render(hbs`
<style>:root {--hds-app-desktop-breakpoint: 10088px}</style>
<Hds::SideNav id="test-side-nav" />
`);
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-minimized');
await click('.hds-side-nav__toggle-button');
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-not-minimized');
await click('.hds-side-nav__toggle-button');
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-minimized');
});
test('it collapses when the ESC key is pressed on narrow viewports', async function (assert) {
await render(hbs`
<style>:root {--hds-app-desktop-breakpoint: 10088px}</style>
<Hds::SideNav id="test-side-nav">
<:header as |H|>
<span id="test-side-nav-header" data-test-minimized={{H.isMinimized}} />
</:header>
<:body as |B|>
<span id="test-side-nav-body" data-test-minimized={{B.isMinimized}} />
<span class="hds-side-nav-hide-when-minimized" />
</:body>
<:footer as |F|>
<span id="test-side-nav-footer" data-test-minimized={{F.isMinimized}} />
</:footer>
</Hds::SideNav>
`);
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-minimized');
await click('.hds-side-nav__toggle-button');
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-not-minimized');
await triggerKeyEvent('#test-side-nav', 'keydown', 'Escape');
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-minimized');
assert.dom('.hds-side-nav-hide-when-minimized').hasAttribute('inert');
});
// COLLAPSIBLE
test('if the @ariaLabel argument is not provided the default aria-labelledby value is set on the toggle button', async function (assert) {
await render(
hbs`<Hds::SideNav @isCollapsible={{true}} id="test-side-nav" />`
);
assert
.dom('.hds-side-nav__toggle-button')
.hasAttribute('aria-labelledby', 'hds-side-nav-header');
assert.dom('.hds-side-nav__toggle-button').hasNoAttribute('aria-label');
});
test('if the @ariaLabel argument is provided the label value is set on the toggle button', async function (assert) {
await render(
hbs`<Hds::SideNav @isCollapsible={{true}} id="test-side-nav" @ariaLabel="Test"/>`
);
assert
.dom('.hds-side-nav__toggle-button')
.hasNoAttribute('aria-labelledby');
assert
.dom('.hds-side-nav__toggle-button')
.hasAttribute('aria-label', 'Test');
});
test('it responds to different events to toggle between "non-minimized" (by default) and "mimimized" states', async function (assert) {
await render(
hbs`<Hds::SideNav @isCollapsible={{true}} id="test-side-nav" />`
);
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-not-minimized');
await click('.hds-side-nav__toggle-button');
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-minimized');
await click('.hds-side-nav__toggle-button');
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-not-minimized');
});
test('the "non-minimized" and "minimized" states have impact on its internal properties', async function (assert) {
await render(hbs`
<Hds::SideNav @isCollapsible={{true}} id="test-side-nav">
<:header as |H|>
<span id="test-side-nav-header" data-test-minimized={{H.isMinimized}} />
</:header>
<:body as |B|>
<span id="test-side-nav-body" data-test-minimized={{B.isMinimized}} />
<span class="hds-side-nav-hide-when-minimized" />
</:body>
<:footer as |F|>
<span id="test-side-nav-footer" data-test-minimized={{F.isMinimized}} />
</:footer>
</Hds::SideNav>
`);
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-not-minimized');
assert
.dom('.hds-side-nav__toggle-button')
.hasAttribute('aria-expanded', 'true');
assert
.dom('.hds-side-nav__toggle-button .hds-icon')
.hasClass('hds-icon-chevrons-left');
assert
.dom('#test-side-nav-header')
.doesNotHaveAttribute('data-test-minimized');
assert
.dom('#test-side-nav-body')
.doesNotHaveAttribute('data-test-minimized');
assert
.dom('#test-side-nav-footer')
.doesNotHaveAttribute('data-test-minimized');
assert
.dom('.hds-side-nav-hide-when-minimized')
.doesNotHaveAttribute('inert');
assert.dom('#test-side-nav-body').doesNotHaveAttribute('inert');
await click('.hds-side-nav__toggle-button');
assert.dom('#test-side-nav').hasClass('hds-side-nav--is-minimized');
assert
.dom('.hds-side-nav__toggle-button')
.hasAttribute('aria-expanded', 'false');
assert
.dom('.hds-side-nav__toggle-button .hds-icon')
.hasClass('hds-icon-chevrons-right');
assert.dom('#test-side-nav-header').hasAttribute('data-test-minimized');
assert.dom('#test-side-nav-body').hasAttribute('data-test-minimized');
assert.dom('#test-side-nav-footer').hasAttribute('data-test-minimized');
assert.dom('.hds-side-nav-hide-when-minimized').hasAttribute('inert');
assert.dom('#test-side-nav-body').doesNotHaveAttribute('inert');
});
test('when the viewport changes from desktop to mobile, it automatically collapses and becomes inert', async function (assert) {
this.mockMedia();
let calls = [];
this.setProperties({
onDesktopViewportChange: (...args) => calls.push(args),
});
await render(hbs`
<Hds::SideNav @isCollapsible={{true}} @onDesktopViewportChange={{this.onDesktopViewportChange}}>
<:header as |H|>
<span id="test-side-nav-header" data-test-minimized={{H.isMinimized}} />
</:header>
<:body as |B|>
<span id="test-side-nav-body" data-test-minimized={{B.isMinimized}} />
<span class="hds-side-nav-hide-when-minimized" />
</:body>
<:footer as |F|>
<span id="test-side-nav-footer" data-test-minimized={{F.isMinimized}} />
</:footer>
</Hds::SideNav>
`);
assert.strictEqual(calls.length, 1, 'called with initial viewport');
await this.changeBrowserSize(false);
assert.deepEqual(
calls[1],
[false],
'resizing to mobile triggers a false event'
);
assert.dom('.hds-side-nav-hide-when-minimized').hasAttribute('inert');
});
test('when collapsed and the viewport changes from mobile to desktop, it automatically expands and is no longer inert', async function (assert) {
this.mockMedia();
let calls = [];
this.setProperties({
onDesktopViewportChange: (...args) => calls.push(args),
});
await render(hbs`
<Hds::SideNav @isCollapsible={{true}} @onDesktopViewportChange={{this.onDesktopViewportChange}}>
<:header as |H|>
<span id="test-side-nav-header" data-test-minimized={{H.isMinimized}} />
</:header>
<:body as |B|>
<span id="test-side-nav-body" data-test-minimized={{B.isMinimized}} />
<span class="hds-side-nav-hide-when-minimized" />
</:body>
<:footer as |F|>
<span id="test-side-nav-footer" data-test-minimized={{F.isMinimized}} />
</:footer>
</Hds::SideNav>
`);
await click('.hds-side-nav__toggle-button');
assert.dom('.hds-side-nav-hide-when-minimized').hasAttribute('inert');
await this.changeBrowserSize(false);
assert.deepEqual(
calls[1],
[false],
'resizing to mobile triggers a false event'
);
assert.dom('.hds-side-nav-hide-when-minimized').hasAttribute('inert');
await this.changeBrowserSize(true);
assert.deepEqual(
calls[2],
[true],
'resizing to desktop triggers a true event'
);
assert
.dom('.hds-side-nav-hide-when-minimized')
.doesNotHaveAttribute('inert');
});
// CALLBACKS
test('it should call `onToggleMinimizedStatus` function if provided', async function (assert) {
let toggled = false;
this.set('onToggleMinimizedStatus', () => (toggled = true));
await render(
hbs`<Hds::SideNav @isCollapsible={{true}} @onToggleMinimizedStatus={{this.onToggleMinimizedStatus}} />`
);
await click('.hds-side-nav__toggle-button');
assert.ok(toggled);
});
});