-
Notifications
You must be signed in to change notification settings - Fork 191
/
swatch-group.test.ts
361 lines (305 loc) · 12.5 KB
/
swatch-group.test.ts
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
/*
Copyright 2022 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import { elementUpdated, expect, fixture, nextFrame } from '@open-wc/testing';
import { sendKeys } from '@web/test-runner-commands';
import '../sp-swatch.js';
import { Swatch, SwatchGroup } from '../';
import { Default } from '../stories/swatch-group.stories.js';
import { spy } from 'sinon';
import { html } from '@spectrum-web-components/base';
import { testForLitDevWarnings } from '../../../test/testing-helpers.js';
describe('Swatch Group', () => {
let el: SwatchGroup;
beforeEach(async () => {
el = await fixture<SwatchGroup>(Default(Default.args));
await elementUpdated(el);
});
testForLitDevWarnings(
async () => await fixture<SwatchGroup>(Default(Default.args))
);
it('loads default swatch accessibly', async () => {
await expect(el).to.be.accessible();
});
it('forwards `border` to children', async () => {
el.border = 'light';
await elementUpdated(el);
([...el.children] as Swatch[]).forEach((child) => {
expect(child.border).to.equal('light');
});
});
it('forwards `rounding` to children', async () => {
el.rounding = 'full';
await elementUpdated(el);
([...el.children] as Swatch[]).forEach((child) => {
expect(child.rounding).to.equal('full');
});
});
it('forwards `size` to children', async () => {
el.size = 'xs';
await elementUpdated(el);
([...el.children] as Swatch[]).forEach((child) => {
expect(child.size).to.equal('xs');
});
});
it('forwards `shape` to children', async () => {
el.shape = 'rectangle';
await elementUpdated(el);
([...el.children] as Swatch[]).forEach((child) => {
expect(child.shape).to.equal('rectangle');
});
});
it('unsets forwarding', async () => {
el.border = 'light';
el.rounding = 'full';
el.size = 'xs';
el.shape = 'rectangle';
await elementUpdated(el);
([...el.children] as Swatch[]).forEach((child) => {
expect(child.border).to.not.be.undefined;
expect(child.rounding).to.not.be.undefined;
expect(child.size).to.not.equal('m');
expect(child.shape).to.not.be.undefined;
});
el.border = undefined;
el.rounding = undefined;
el.removeAttribute('size');
el.shape = undefined;
await elementUpdated(el);
([...el.children] as Swatch[]).forEach((child) => {
expect(child.border).to.equal(undefined);
expect(child.rounding).to.equal(undefined);
expect(child.size).to.equal('m');
expect(child.shape).to.equal(undefined);
});
});
it('does not dispatch `change` events without `selects` attribute', async () => {
const selectedChild = el.querySelector(
':scope > sp-swatch:nth-child(4)'
) as Swatch;
const changeSpy = spy();
el.addEventListener('change', () => changeSpy());
expect(el.selected).to.deep.equal([]);
selectedChild.click();
expect(changeSpy.called).to.be.false;
expect(el.selected).to.deep.equal([]);
});
it('dispatches `change` events as [selects="single"]', async () => {
el.selects = 'single';
const selectedChild = el.querySelector(
':scope > sp-swatch:nth-child(4)'
) as Swatch;
const changeSpy = spy();
el.addEventListener('change', () => changeSpy());
expect(el.selected).to.deep.equal([]);
expect(selectedChild.selected).to.be.false;
selectedChild.click();
expect(changeSpy.calledOnce).to.be.true;
expect(el.selected).to.deep.equal([selectedChild.value]);
expect(selectedChild.selected).to.be.true;
selectedChild.click();
expect(changeSpy.calledOnce).to.be.true;
expect(el.selected).to.deep.equal([selectedChild.value]);
expect(selectedChild.selected).to.be.true;
});
it('can have `change` events prevented', async () => {
el.selects = 'single';
const selectedChild = el.querySelector(
':scope > sp-swatch:nth-child(4)'
) as Swatch;
el.addEventListener('change', (event: Event) => event.preventDefault());
expect(el.selected).to.deep.equal([]);
expect(selectedChild.selected).to.be.false;
selectedChild.click();
expect(el.selected).to.deep.equal([]);
expect(selectedChild.selected).to.be.false;
});
it('dispatches `change` events as [selects="multiple"]', async () => {
el.selects = 'multiple';
const selectedChild0 = el.querySelector(
':scope > sp-swatch:nth-child(1)'
) as Swatch;
const selectedChild1 = el.querySelector(
':scope > sp-swatch:nth-child(4)'
) as Swatch;
const selectedChild2 = el.querySelector(
':scope > sp-swatch:nth-child(6)'
) as Swatch;
await elementUpdated(selectedChild0);
const changeSpy = spy();
el.addEventListener('change', () => changeSpy());
expect(el.selected).to.deep.equal([]);
selectedChild0.click();
selectedChild1.click();
selectedChild2.click();
expect(changeSpy.callCount).to.equal(3);
expect(el.selected).to.deep.equal([
selectedChild0.value,
selectedChild1.value,
selectedChild2.value,
]);
});
it('filters `selected` when a selected Swatch is removed from the DOM', async () => {
el.selects = 'multiple';
const selectedChild0 = el.querySelector(
':scope > sp-swatch:nth-child(1)'
) as Swatch;
const selectedChild1 = el.querySelector(
':scope > sp-swatch:nth-child(4)'
) as Swatch;
const selectedChild2 = el.querySelector(
':scope > sp-swatch:nth-child(6)'
) as Swatch;
await elementUpdated(selectedChild0);
expect(el.selected).to.deep.equal([]);
selectedChild0.click();
selectedChild1.click();
selectedChild2.click();
expect(el.selected).to.deep.equal([
selectedChild0.value,
selectedChild1.value,
selectedChild2.value,
]);
selectedChild0.remove();
await elementUpdated(el);
expect(el.selected).to.deep.equal([
selectedChild1.value,
selectedChild2.value,
]);
selectedChild2.remove();
await elementUpdated(el);
expect(el.selected).to.deep.equal([selectedChild1.value]);
selectedChild1.remove();
await elementUpdated(el);
expect(el.selected).to.deep.equal([]);
});
it('maintains a single tab stop', async () => {
const inputBefore = document.createElement('input');
const inputAfter = document.createElement('input');
el.insertAdjacentElement('beforebegin', inputBefore);
el.insertAdjacentElement('afterend', inputAfter);
inputBefore.focus();
expect(document.activeElement === el.children[0]).to.be.false;
await sendKeys({
press: 'Tab',
});
expect(document.activeElement === el.children[0]).to.be.true;
await sendKeys({
press: 'Tab',
});
expect(document.activeElement === el.children[0]).to.be.false;
await sendKeys({
press: 'Shift+Tab',
});
expect(document.activeElement === el.children[0]).to.be.true;
});
it('makes the first selected child the single tab stop', async () => {
const selectedChild = el.querySelector(
':scope > sp-swatch:nth-child(4)'
) as Swatch;
expect(selectedChild.selected).to.be.false;
const inputBefore = document.createElement('input');
const inputAfter = document.createElement('input');
el.insertAdjacentElement('beforebegin', inputBefore);
el.insertAdjacentElement('afterend', inputAfter);
inputBefore.focus();
el.selects = 'single';
el.selected = [selectedChild.value];
await elementUpdated(el);
await nextFrame();
expect(selectedChild.selected).to.be.true;
expect(document.activeElement === selectedChild).to.be.false;
await sendKeys({
press: 'Tab',
});
expect(document.activeElement === selectedChild).to.be.true;
await sendKeys({
press: 'Tab',
});
expect(document.activeElement === selectedChild).to.be.false;
await sendKeys({
press: 'Shift+Tab',
});
expect(document.activeElement === selectedChild).to.be.true;
});
it('focus()es to the first Swatch', async () => {
el.focus();
expect(document.activeElement === el.children[0]).to.be.true;
});
it('focus()es to the first selected Swatch', async () => {
const selectedChild = el.querySelector(
':scope > sp-swatch:nth-child(4)'
) as Swatch;
expect(selectedChild.selected).to.be.false;
el.selects = 'single';
el.selected = [selectedChild.value];
await elementUpdated(el);
await nextFrame();
expect(selectedChild.selected).to.be.true;
el.focus();
expect(document.activeElement === selectedChild).to.be.true;
});
});
describe('Swatch Group - DOM selected', () => {
it('accepts selection from DOM', async () => {
const el = await fixture<SwatchGroup>(html`
<sp-swatch-group selects="multiple">
<sp-swatch value="color-0" color="red"></sp-swatch>
<sp-swatch value="color-1" color="green" selected></sp-swatch>
<sp-swatch value="color-2" color="blue"></sp-swatch>
<sp-swatch value="color-3" color="yellow" selected></sp-swatch>
</sp-swatch-group>
`);
await elementUpdated(el);
expect(el.selected).to.deep.equal(['color-1', 'color-3']);
});
it('merges `selected` and selection from DOM', async () => {
const el = await fixture<SwatchGroup>(html`
<sp-swatch-group selects="multiple" .selected=${['color-1']}>
<sp-swatch value="color-0" color="red"></sp-swatch>
<sp-swatch value="color-1" color="green"></sp-swatch>
<sp-swatch value="color-2" color="blue"></sp-swatch>
<sp-swatch value="color-3" color="yellow" selected></sp-swatch>
</sp-swatch-group>
`);
await elementUpdated(el);
expect(el.selected).to.deep.equal(['color-1', 'color-3']);
});
it('lazily accepts selection from DOM', async () => {
const el = await fixture<SwatchGroup>(html`
<sp-swatch-group selects="multiple">
<sp-swatch value="color-0" color="red"></sp-swatch>
<sp-swatch value="color-1" color="green"></sp-swatch>
<sp-swatch value="color-2" color="blue"></sp-swatch>
<sp-swatch value="color-3" color="yellow" selected></sp-swatch>
</sp-swatch-group>
`);
await elementUpdated(el);
const color1 = el.querySelector('[value="color-1"]') as Swatch;
expect(el.selected).to.deep.equal(['color-3']);
color1.selected = true;
await elementUpdated(el);
expect(el.selected).to.deep.equal(['color-3', 'color-1']);
});
it('clears previously selected children when updating `selected`', async () => {
const el = await fixture<SwatchGroup>(html`
<sp-swatch-group selects="single" .selected=${['color-1']}>
<sp-swatch value="color-0" color="red"></sp-swatch>
<sp-swatch value="color-1" color="green"></sp-swatch>
<sp-swatch value="color-2" color="blue"></sp-swatch>
</sp-swatch-group>
`);
await elementUpdated(el);
expect(el.selected).to.deep.equal(['color-1']);
el.selected = ['color-2'];
await elementUpdated(el);
expect(el.selected).to.deep.equal(['color-2']);
});
});