forked from patternfly/patternfly-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpf-tabs.spec.ts
More file actions
356 lines (292 loc) · 12.9 KB
/
pf-tabs.spec.ts
File metadata and controls
356 lines (292 loc) · 12.9 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import { expect, html, nextFrame } from '@open-wc/testing';
import { createFixture } from '@patternfly/pfe-tools/test/create-fixture.js';
import { a11ySnapshot } from '@patternfly/pfe-tools/test/a11y-snapshot.js';
import { setViewport, sendKeys } from '@web/test-runner-commands';
import { allUpdates } from '@patternfly/pfe-tools/test/utils.js';
import { PfTabs } from '../pf-tabs.js';
import { PfTab } from '../pf-tab.js';
import { PfTabPanel } from '../pf-tab-panel.js';
import '@patternfly/pfe-tools/test/stub-logger.js';
function press(key: string) {
return async function() {
await sendKeys({ press: key });
};
}
describe('<pf-tabs>', function() {
it('instantiates imperatively', function() {
expect(document.createElement('pf-tabs')).to.be.an.instanceof(PfTabs);
expect(document.createElement('pf-tab')).to.be.an.instanceof(PfTab);
expect(document.createElement('pf-tab-panel')).to.be.an.instanceof(PfTabPanel);
});
it('should upgrade', async function() {
const el = await createFixture<PfTabs>(html`<pf-tabs></pf-tabs>`);
expect(el, 'pf-tabs should be an instance of PfeTabs')
.to.be.an.instanceOf(customElements.get('pf-tabs'))
.and
.to.be.an.instanceOf(PfTabs);
});
describe('with three valid tab pairs', function() {
let element: PfTabs;
const updateComplete = () => allUpdates(element);
beforeEach(async function() {
element = await createFixture<PfTabs>(html`
<pf-tabs>
<pf-tab slot="tab">tab-1</pf-tab>
<pf-tab-panel>panel-1</pf-tab-panel>
<pf-tab slot="tab">tab-2</pf-tab>
<pf-tab-panel>panel-2</pf-tab-panel>
<pf-tab slot="tab">tab-3</pf-tab>
<pf-tab-panel>panel-3</pf-tab-panel>
</pf-tabs>
`);
});
beforeEach(updateComplete);
it('should apply aria attributes on initialization', function() {
const tabs = element.querySelectorAll('pf-tab');
const panels = element.querySelectorAll('pf-tab-panel');
tabs.forEach(function(tab: Element, index: number) {
const tabId = tab.getAttribute('id');
const tabControls = tab.getAttribute('aria-controls');
panels.forEach(function(panel: Element, pindex: number) {
if (index === pindex) {
expect(panel.getAttribute('aria-labelledby')).to.equal(tabId);
expect(panel.id).to.equal(tabControls);
}
});
});
});
describe('pressing Tab', function() {
beforeEach(press('Tab'));
beforeEach(updateComplete);
beforeEach(nextFrame);
it('should activate the first focusable tab', function() {
expect(element.querySelector('pf-tab')).to.have.attribute('active');
});
it('should activate the first tab panel', function() {
expect(element.querySelector('pf-tab')).to.not.have.attribute('hidden');
});
});
describe('setting the `vertical` attribute', function() {
beforeEach(function() {
element.setAttribute('vertical', '');
});
beforeEach(updateComplete);
it('should have vertical styles', function() {
// WARNING: asserting on shadow root content;
const tabs = element.shadowRoot!.querySelector('[part="tabs"]')!;
const tabsVerticalStyles = getComputedStyle(tabs).flexDirection;
expect(tabsVerticalStyles).to.be.equal('column');
});
});
describe('setting the second tab\'s `active` attribute', function() {
beforeEach(function() {
const [, tab] = element.querySelectorAll('pf-tab');
tab!.active = true;
});
beforeEach(updateComplete);
beforeEach(nextFrame);
it('should activate the second tab', function() {
const [, tab] = element.querySelectorAll('pf-tab');
expect(tab).to.have.attribute('active');
});
it('should activate its panel', function() {
const [, tab] = element.querySelectorAll('pf-tab');
expect(tab).to.not.have.attribute('hidden');
});
it('should deactivate previously active tab', function() {
expect(element.querySelector('pf-tab')).to.not.have.attribute('active');
});
it('should hide previously active panel', function() {
expect(element.querySelector('pf-tab-panel')).to.have.attribute('hidden');
});
});
describe('setting `activeIndex` to 2', function() {
beforeEach(function() {
element.activeIndex = 2;
});
beforeEach(updateComplete);
beforeEach(nextFrame);
it('should activate the third tab', function() {
const [,, tab] = element.querySelectorAll('pf-tab');
expect(tab).to.have.attribute('active');
});
it('should activate the third panel', async function() {
const snapshot = await a11ySnapshot();
expect(snapshot.children?.find(x => x.role === 'tabpanel')?.name).to.equal('tab-3');
});
describe('then setting the first tab\'s `disabled` attribute', function() {
beforeEach(async function() {
element.querySelector('pf-tab')!.disabled = true;
});
beforeEach(updateComplete);
it('should disable the button', async function() {
const snapshot = await a11ySnapshot();
const disabledTab = snapshot.children?.find(x => x.role === 'tab' && x.disabled);
expect(disabledTab).to.be.ok;
});
describe('and clicking the disabled tab', function() {
beforeEach(async function() {
element.querySelector('pf-tab')!.click();
});
beforeEach(updateComplete);
it('should not activate', function() {
const [first] = element.querySelectorAll('pf-tab');
expect(first).to.not.have.attribute('active');
});
it('should present the third panel to the ax tree', async function() {
const snapshot = await a11ySnapshot();
expect(snapshot.children?.find(x => x.role === 'tabpanel')?.name).to.equal('tab-3');
});
});
describe('then setting `activeIndex` to 0 (i.e. the disabled tab\'s index)', function() {
beforeEach(function() {
element.activeIndex = 0;
});
beforeEach(updateComplete);
it('should not activate the first tab', function() {
expect(element.querySelector('pf-tab')).to.not.have.attribute('active');
});
it('should present the third panel to the ax tree', async function() {
const snapshot = await a11ySnapshot();
expect(snapshot.children?.find(x => x.role === 'tabpanel')?.name).to.equal('tab-3');
});
});
});
});
describe('when viewed in a small viewport', function() {
beforeEach(async function() {
// this is a comically narrow viewport, but our tabs have quite short
// labels, so viewport must be itsy-pitsy in order to cause overflow
await setViewport({ width: 100, height: 640 });
});
beforeEach(nextFrame);
beforeEach(nextFrame);
beforeEach(nextFrame);
beforeEach(updateComplete);
it('should have visible scroll buttons if overflowed', function() {
// Note: overflow buttons are not included in the accessibility tree otherwise we'd test
// for buttons there. tabindex="-1" is used on the buttons to prevent focus and was a
// decision made to keep logical keyboard navigation order flow between tabs and panels
// as the next overflow button exists in the DOM between the tabs container and the open panel
// and would disrupt the expected flow. For keyboard users they are able to scroll using the
// left and right arrows keys and do not need direct access to the overflow buttons but still
// exist as visual cues for which direction is overflowed
const previousTab = element.shadowRoot!.querySelector('#previousTab')!;
const nextTab = element.shadowRoot!.querySelector('#nextTab')!;
expect(previousTab).to.not.be.null;
expect(nextTab).to.not.be.null;
const prevDisplayStyle = getComputedStyle(previousTab).display;
const nextDisplayStyle = getComputedStyle(nextTab).display;
expect(prevDisplayStyle ).to.not.equal('none');
expect(nextDisplayStyle).to.not.equal('none');
});
});
describe(`when navigated by keyboard`, function() {
describe('when focused on the first tab', function() {
beforeEach(async function() {
element.querySelector('pf-tab')!.focus();
});
describe('pressing ArrowRight', function() {
beforeEach(async function() {
await sendKeys({ down: 'ArrowRight' });
});
beforeEach(updateComplete);
it('should activate the next tab', function() {
const [first, second, third] = element.querySelectorAll('pf-tab');
expect(first).to.not.have.attribute('active');
expect(second).to.have.attribute('active');
expect(third).to.not.have.attribute('active');
});
it('should specify the selected tab to assistive technology', async function() {
expect(await a11ySnapshot()).to.axContainQuery({ role: 'tabpanel', name: 'tab-2' });
});
});
describe('pressing ArrowLeft', function() {
beforeEach(async function() {
await sendKeys({ down: 'ArrowLeft' });
});
it('should activate the last tab', function() {
const [first, second, third] = element.querySelectorAll('pf-tab');
expect(first).to.not.have.attribute('active');
expect(second).to.not.have.attribute('active');
expect(third).to.have.attribute('active');
});
it('should specify the selected tab to assistive technology', async function() {
expect(await a11ySnapshot()).to.axContainQuery({ role: 'tabpanel', name: 'tab-3' });
});
describe('then pressing ArrowRight', function() {
beforeEach(async function() {
await sendKeys({ down: 'ArrowRight' });
});
beforeEach(updateComplete);
it('should activate the first tab', function() {
const [first, second, third] = element.querySelectorAll('pf-tab');
expect(first).to.have.attribute('active');
expect(second).to.not.have.attribute('active');
expect(third).to.not.have.attribute('active');
});
it('should specify the selected tab to assistive technology', async function() {
expect(await a11ySnapshot()).to.axContainQuery({ role: 'tabpanel', name: 'tab-1' });
});
});
});
});
});
describe('setting the `manual` attribute', function() {
beforeEach(function() {
element.setAttribute('manual', '');
});
beforeEach(updateComplete);
describe('pressing Tab', function() {
beforeEach(press('Tab'));
describe('pressing ArrowRight key', function() {
beforeEach(press('ArrowRight'));
beforeEach(updateComplete);
it('should not activate second tab', function() {
const [first, second, third] = element.querySelectorAll('pf-tab');
expect(first).to.have.attribute('active');
expect(second).to.not.have.attribute('active');
expect(third).to.not.have.attribute('active');
});
it('should focus the second tab', function() {
const [, second] = element.querySelectorAll('pf-tab');
expect(document.activeElement).to.equal(second);
});
describe('pressing enter key', function() {
beforeEach(press('Enter'));
beforeEach(updateComplete);
it('should activate second tab', function() {
const [first, second, third] = element.querySelectorAll('pf-tab');
expect(first).to.not.have.attribute('active');
expect(second).to.have.attribute('active');
expect(third).to.not.have.attribute('active');
expect(document.activeElement).to.equal(second);
});
});
});
});
});
});
describe('when no active tab is given and the first tab is disabled', function() {
let element: PfTabs;
beforeEach(async function() {
element = await createFixture<PfTabs>(html`
<pf-tabs>
<pf-tab id="tab1" slot="tab" disabled></pf-tab>
<pf-tab id="tab2" slot="tab"></pf-tab>
<pf-tab id="tab3" slot="tab"></pf-tab>
<pf-tab id="tab4" slot="tab" disabled></pf-tab>
<pf-tab id="tab5" slot="tab" aria-disabled="true"></pf-tab>
<pf-tab-panel></pf-tab-panel>
<pf-tab-panel></pf-tab-panel>
<pf-tab-panel></pf-tab-panel>
<pf-tab-panel></pf-tab-panel>
<pf-tab-panel></pf-tab-panel>
</pf-tabs>
`);
});
it('should activate the next focusable tab', function() {
expect(element.activeTab).to.have.id('tab2');
});
});
});