-
Notifications
You must be signed in to change notification settings - Fork 76
/
segmented-control.e2e.ts
477 lines (406 loc) · 17.3 KB
/
segmented-control.e2e.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import { E2EElement, E2EPage, newE2EPage } from "@stencil/core/testing";
import { html } from "../../../support/formatting";
import {
defaults,
disabled,
focusable,
formAssociated,
hidden,
labelable,
reflects,
renders,
} from "../../tests/commonTests";
describe("calcite-segmented-control", () => {
describe("defaults", () => {
defaults("calcite-segmented-control", [
{
propertyName: "appearance",
defaultValue: "solid",
},
{
propertyName: "layout",
defaultValue: "horizontal",
},
{
propertyName: "scale",
defaultValue: "m",
},
{
propertyName: "width",
defaultValue: "auto",
},
{
propertyName: "status",
defaultValue: "idle",
},
{
propertyName: "validationIcon",
defaultValue: undefined,
},
{
propertyName: "validationMessage",
defaultValue: undefined,
},
]);
});
describe("reflects", () => {
reflects("calcite-segmented-control", [
{
propertyName: "scale",
value: "m",
},
{
propertyName: "layout",
value: "horizontal",
},
{
propertyName: "appearance",
value: "solid",
},
{
propertyName: "width",
value: "auto",
},
{
propertyName: "status",
value: "invalid",
},
{
propertyName: "validationIcon",
value: true,
},
]);
});
describe("renders", () => {
renders("calcite-segmented-control", { display: "flex" });
});
describe("honors hidden attribute", () => {
hidden("calcite-segmented-control");
});
describe("labelable", () => {
labelable(
html`<calcite-segmented-control>
<calcite-segmented-control-item value="1"></calcite-segmented-control-item>
<calcite-segmented-control-item value="2"></calcite-segmented-control-item>
<calcite-segmented-control-item value="3"></calcite-segmented-control-item>
</calcite-segmented-control>`,
{ focusTargetSelector: "calcite-segmented-control-item" },
);
});
describe("disabled", () => {
disabled(
html`<calcite-segmented-control>
<calcite-segmented-control-item value="1"></calcite-segmented-control-item>
<calcite-segmented-control-item value="2"></calcite-segmented-control-item>
<calcite-segmented-control-item value="3"></calcite-segmented-control-item>
</calcite-segmented-control>`,
{ focusTarget: "child" },
);
});
it("sets value from selected item", async () => {
const page = await newE2EPage();
await page.setContent(html`
<calcite-segmented-control>
<calcite-segmented-control-item id="child-1" value="1" checked>one</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-2" value="2">two</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-3" value="3">three</calcite-segmented-control-item>
</calcite-segmented-control>
`);
const element = await page.find("calcite-segmented-control");
const value = await element.getProperty("value");
expect(value).toBe("1");
});
it("does not require an item to be checked", async () => {
const page = await newE2EPage();
await page.setContent(
`<calcite-segmented-control>
<calcite-segmented-control-item value="1"></calcite-segmented-control-item>
<calcite-segmented-control-item value="2"></calcite-segmented-control-item>
<calcite-segmented-control-item value="3"></calcite-segmented-control-item>
</calcite-segmented-control>`,
);
const element = await page.find("calcite-segmented-control");
const selected = await element.getProperty("selectedItem");
expect(selected).not.toBeDefined();
});
it("when multiple items are checked, last one wins", async () => {
const page = await newE2EPage();
await page.setContent(
`<calcite-segmented-control>
<calcite-segmented-control-item value="1" checked>one</calcite-segmented-control-item>
<calcite-segmented-control-item value="2" checked>two</calcite-segmented-control-item>
<calcite-segmented-control-item value="3" checked>three</calcite-segmented-control-item>
</calcite-segmented-control>`,
);
const element = await page.find("calcite-segmented-control");
const selected = await element.getProperty("selectedItem");
expect(selected).toBeDefined();
const selectedItems = await element.findAll("calcite-segmented-control-item[checked]");
expect(selectedItems).toHaveLength(1);
const selectedValue = await selectedItems[0].getProperty("value");
expect(selectedValue).toBe("3");
});
async function getSelectedItemValue(page: E2EPage): Promise<string> {
return page.$eval(
"calcite-segmented-control",
(segmentedControl: HTMLCalciteSegmentedControlElement) => segmentedControl.selectedItem.value,
);
}
it("allows items to be selected", async () => {
const page = await newE2EPage();
await page.setContent(
`<calcite-segmented-control>
<calcite-segmented-control-item value="1">one</calcite-segmented-control-item>
<calcite-segmented-control-item value="2">two</calcite-segmented-control-item>
<calcite-segmented-control-item value="3">three</calcite-segmented-control-item>
</calcite-segmented-control>`,
);
const element = await page.find("calcite-segmented-control");
const eventSpy = await element.spyOnEvent("calciteSegmentedControlChange");
expect(eventSpy).not.toHaveReceivedEvent();
const [first, second, third] = await page.findAll("calcite-segmented-control-item");
await first.click();
expect(eventSpy).toHaveReceivedEventTimes(1);
expect(await getSelectedItemValue(page)).toBe("1");
// does not emit from programmatic changes
third.setProperty("checked", true);
await page.waitForChanges();
expect(eventSpy).toHaveReceivedEventTimes(1);
expect(await getSelectedItemValue(page)).toBe("3");
await second.click();
expect(eventSpy).toHaveReceivedEventTimes(2);
expect(await getSelectedItemValue(page)).toBe("2");
});
it("updates selection when cleared with undefined", async () => {
const page = await newE2EPage();
await page.setContent(
`<calcite-segmented-control>
<calcite-segmented-control-item value="1" checked>one</calcite-segmented-control-item>
<calcite-segmented-control-item value="2">two</calcite-segmented-control-item>
</calcite-segmented-control>`,
);
await page.waitForChanges();
expect(await getSelectedItemValue(page)).toBe("1");
const [first, second] = await page.findAll("calcite-segmented-control-item");
first.setProperty("checked", undefined);
second.setProperty("checked", true);
await page.waitForChanges();
expect(await getSelectedItemValue(page)).toBe("2");
first.setProperty("checked", true);
second.setProperty("checked", undefined);
await page.waitForChanges();
expect(await getSelectedItemValue(page)).toBe("1");
});
it("does not emit extraneous events (edge case from #3210)", async () => {
const page = await newE2EPage();
await page.setContent(
`<calcite-segmented-control>
<calcite-segmented-control-item value="1">one</calcite-segmented-control-item>
<calcite-segmented-control-item value="2">two</calcite-segmented-control-item>
</calcite-segmented-control>`,
);
const timesCalled = await page.evaluate(async () => {
let calls = 0;
const segmentedControl = document.querySelector("calcite-segmented-control");
const waitForFrame = async () => await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
document.addEventListener("calciteSegmentedControlChange", () => calls++);
let [first, second] = Array.from(document.querySelectorAll("calcite-segmented-control-item"));
first.checked = true;
await waitForFrame();
second.click();
await waitForFrame();
segmentedControl.remove();
await waitForFrame();
document.body.innerHTML = `
<calcite-segmented-control>
<calcite-segmented-control-item value="1">one</calcite-segmented-control-item>
<calcite-segmented-control-item value="2">two</calcite-segmented-control-item>
</calcite-segmented-control>
`;
[first, second] = Array.from(document.querySelectorAll("calcite-segmented-control-item"));
second.checked = true;
await waitForFrame();
first.click();
await waitForFrame();
return calls;
});
expect(timesCalled).toBe(2);
});
describe("keyboard navigation", () => {
async function assertArrowSelection(page: E2EPage): Promise<void> {
const element = await page.find("calcite-segmented-control");
const spy = await element.spyOnEvent("calciteSegmentedControlChange");
await tabIntoFirstElement();
await cycleThroughItemsAndAssertValue("left-right");
expect(spy).toHaveReceivedEventTimes(6);
await tabIntoFirstElement();
await cycleThroughItemsAndAssertValue("up-down");
expect(spy).toHaveReceivedEventTimes(12);
async function tabIntoFirstElement(): Promise<void> {
const firstElement = await element.find("calcite-segmented-control-item[checked]");
await firstElement.click();
await page.keyboard.down("Shift");
await page.keyboard.press("Tab");
await page.keyboard.up("Shift");
await page.keyboard.press("Tab");
}
async function cycleThroughItemsAndAssertValue(keys: "left-right" | "up-down"): Promise<void> {
const [moveBeforeArrowKey, moveAfterArrowKey] =
keys === "left-right" ? (["ArrowLeft", "ArrowRight"] as const) : (["ArrowUp", "ArrowDown"] as const);
await element.press(moveAfterArrowKey);
await page.waitForChanges();
let selected = await element.find("calcite-segmented-control-item[checked]");
let value = await selected.getProperty("value");
expect(value).toBe("2");
await element.press(moveAfterArrowKey);
selected = await element.find("calcite-segmented-control-item[checked]");
value = await selected.getProperty("value");
expect(value).toBe("3");
await element.press(moveAfterArrowKey);
selected = await element.find("calcite-segmented-control-item[checked]");
value = await selected.getProperty("value");
expect(value).toBe("1");
await element.press(moveBeforeArrowKey);
selected = await element.find("calcite-segmented-control-item[checked]");
value = await selected.getProperty("value");
expect(value).toBe("3");
await element.press(moveBeforeArrowKey);
selected = await element.find("calcite-segmented-control-item[checked]");
value = await selected.getProperty("value");
expect(value).toBe("2");
await element.press(moveBeforeArrowKey);
selected = await element.find("calcite-segmented-control-item[checked]");
value = await selected.getProperty("value");
expect(value).toBe("1");
}
}
it("selects item with left-right/up-down arrow keys", async () => {
const page = await newE2EPage();
await page.setContent(
html`<calcite-segmented-control>
<calcite-segmented-control-item value="1" checked>one</calcite-segmented-control-item>
<calcite-segmented-control-item value="2">two</calcite-segmented-control-item>
<calcite-segmented-control-item value="3">three</calcite-segmented-control-item>
</calcite-segmented-control>`,
);
await assertArrowSelection(page);
});
it("selects item with left-right/up-down arrow keys after adding items programmatically", async () => {
const page = await newE2EPage();
await page.setContent(html`<calcite-segmented-control></calcite-segmented-control>`);
await page.$eval("calcite-segmented-control", (segmentedControl: HTMLCalciteSegmentedControlElement) => {
segmentedControl.innerHTML = `
<calcite-segmented-control-item value="1" checked>one</calcite-segmented-control-item>
<calcite-segmented-control-item value="2">two</calcite-segmented-control-item>
<calcite-segmented-control-item value="3">three</calcite-segmented-control-item>`;
});
await assertArrowSelection(page);
});
});
describe("WAI-ARIA Roles, States, and Properties", () => {
it(`has a role of 'radiogroup'`, async () => {
const page = await newE2EPage();
await page.setContent("<calcite-segmented-control></calcite-segmented-control>");
const element = await page.find("calcite-segmented-control");
const role = element.getAttribute("role");
expect(role).toEqualText("radiogroup");
});
});
it("renders requested props", async () => {
const page = await newE2EPage();
await page.setContent(
"<calcite-segmented-control scale='l' layout='vertical' appearance='outline' width='full'></calcite-segmented-control>",
);
const element = await page.find("calcite-segmented-control");
expect(element).toEqualAttribute("scale", "l");
expect(element).toEqualAttribute("layout", "vertical");
expect(element).toEqualAttribute("appearance", "outline");
expect(element).toEqualAttribute("width", "full");
});
it("inheritable props: `appearance`, `layout`, and `scale` modified on the parent get passed to items", async () => {
async function inheritsProps(segmentedControlItems: E2EElement[]): Promise<void> {
for (const item of segmentedControlItems) {
expect(await item.getProperty("appearance")).toBe("outline");
expect(await item.getProperty("layout")).toBe("vertical");
expect(await item.getProperty("scale")).toBe("l");
}
}
const page = await newE2EPage();
await page.setContent(html`
<calcite-segmented-control appearance="outline" layout="vertical" scale="l">
<calcite-segmented-control-item id="child-1" value="1">one</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-2" value="2">two</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-3" value="3">three</calcite-segmented-control-item>
</calcite-segmented-control>
`);
await page.waitForChanges();
const segmentedControl = await page.find("calcite-segmented-control");
let segmentedControlItems = await page.findAll("calcite-segmented-control-item");
expect(segmentedControlItems).toHaveLength(3);
await inheritsProps(segmentedControlItems);
segmentedControl.innerHTML = html`
<calcite-segmented-control-item id="child-4" value="4">one</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-5" value="5">two</calcite-segmented-control-item>
`;
await page.waitForChanges();
segmentedControlItems = await page.findAll("calcite-segmented-control-item");
expect(segmentedControlItems).toHaveLength(2);
await inheritsProps(segmentedControlItems);
});
describe("setFocus()", () => {
describe("focuses the first item if there is no selection", () => {
focusable(
html`
<calcite-segmented-control>
<calcite-segmented-control-item id="child-1" value="1">one</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-2" value="2">two</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-3" value="3">three</calcite-segmented-control-item>
</calcite-segmented-control>
`,
{
focusTargetSelector: "#child-1",
},
);
});
describe("focuses the selected item", () => {
focusable(
html`
<calcite-segmented-control>
<calcite-segmented-control-item id="child-1" value="1">one</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-2" value="2">two</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-3" value="3" checked>three</calcite-segmented-control-item>
</calcite-segmented-control>
`,
{
focusTargetSelector: "#child-3",
},
);
});
});
describe("is form-associated", () => {
describe("unselected value", () => {
formAssociated(
html`
<calcite-segmented-control>
<calcite-segmented-control-item id="child-1" value="1">one</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-2" value="2">two</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-3" value="3">three</calcite-segmented-control-item>
</calcite-segmented-control>
`,
{ testValue: 2, validation: true, changeValueKeys: ["Space"] },
);
});
describe("selected-value", () => {
formAssociated(
html`
<calcite-segmented-control>
<calcite-segmented-control-item id="child-1" value="1">one</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-2" value="2" checked>two</calcite-segmented-control-item>
<calcite-segmented-control-item id="child-3" value="3">three</calcite-segmented-control-item>
</calcite-segmented-control>
`,
{ testValue: 2 },
);
});
});
});