@spectrum-web-components/radio 0.9.13 → 0.9.15-devmode.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,81 +1,80 @@
1
- /*
2
- Copyright 2020 Adobe. All rights reserved.
3
- This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License. You may obtain a copy
5
- of the License at http://www.apache.org/licenses/LICENSE-2.0
6
-
7
- Unless required by applicable law or agreed to in writing, software distributed under
8
- the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- OF ANY KIND, either express or implied. See the License for the specific language
10
- governing permissions and limitations under the License.
11
- */
12
- import '../sp-radio-group.js';
13
- import '@spectrum-web-components/radio/sp-radio.js';
14
- import { elementUpdated, expect, fixture, html } from '@open-wc/testing';
15
- import { arrowDownEvent, arrowLeftEvent, arrowRightEvent, arrowUpEvent, endEvent, enterEvent, homeEvent, } from '../../../test/testing-helpers.js';
16
- import { a11ySnapshot, findAccessibilityNode, sendKeys, } from '@web/test-runner-commands';
17
- import { sendMouse } from '../../../test/plugins/browser.js';
18
- import { spy } from 'sinon';
19
- describe('Radio Group - focus control', () => {
20
- it('does not accept focus when empty', async () => {
21
- const el = await fixture(html `
1
+ import "@spectrum-web-components/radio/sp-radio-group.js";
2
+ import "@spectrum-web-components/radio/sp-radio.js";
3
+ import { elementUpdated, expect, fixture, html } from "@open-wc/testing";
4
+ import {
5
+ arrowDownEvent,
6
+ arrowLeftEvent,
7
+ arrowRightEvent,
8
+ arrowUpEvent,
9
+ endEvent,
10
+ enterEvent,
11
+ homeEvent
12
+ } from "../../../test/testing-helpers.js";
13
+ import {
14
+ a11ySnapshot,
15
+ findAccessibilityNode,
16
+ sendKeys
17
+ } from "@web/test-runner-commands";
18
+ import { sendMouse } from "../../../test/plugins/browser.js";
19
+ import { spy } from "sinon";
20
+ describe("Radio Group - focus control", () => {
21
+ it("does not accept focus when empty", async () => {
22
+ const el = await fixture(html`
22
23
  <sp-radio-group></sp-radio-group>
23
24
  `);
24
- await elementUpdated(el);
25
- expect(document.activeElement === el).to.be.false;
26
- el.focus();
27
- await elementUpdated(el);
28
- expect(document.activeElement === el).to.be.false;
29
- });
30
- it('focuses selected before first', async () => {
31
- const values = ['first', 'second', 'third'];
32
- const el = await fixture(html `
25
+ await elementUpdated(el);
26
+ expect(document.activeElement === el).to.be.false;
27
+ el.focus();
28
+ await elementUpdated(el);
29
+ expect(document.activeElement === el).to.be.false;
30
+ });
31
+ it("focuses selected before first", async () => {
32
+ const values = ["first", "second", "third"];
33
+ const el = await fixture(html`
33
34
  <sp-radio-group selected="second">
34
35
  <sp-radio value=${values[0]}>Option 1</sp-radio>
35
36
  <sp-radio value=${values[1]}>Option 2</sp-radio>
36
37
  <sp-radio value=${values[2]}>Option 3</sp-radio>
37
38
  </sp-radio-group>
38
39
  `);
39
- await elementUpdated(el);
40
- const selected = el.querySelector('[value="second"]');
41
- expect(document.activeElement === el).to.be.false;
42
- el.focus();
43
- await elementUpdated(el);
44
- expect(document.activeElement === selected).to.be.true;
45
- });
46
- it('focuses the child input not the root when [tabindex=-1]', async () => {
47
- const values = ['first', 'second', 'third'];
48
- const el = await fixture(html `
40
+ await elementUpdated(el);
41
+ const selected = el.querySelector('[value="second"]');
42
+ expect(document.activeElement === el).to.be.false;
43
+ el.focus();
44
+ await elementUpdated(el);
45
+ expect(document.activeElement === selected).to.be.true;
46
+ });
47
+ it("focuses the child input not the root when [tabindex=-1]", async () => {
48
+ const values = ["first", "second", "third"];
49
+ const el = await fixture(html`
49
50
  <sp-radio-group selected="second">
50
51
  <sp-radio value=${values[0]}>Option 1</sp-radio>
51
52
  <sp-radio value=${values[1]}>Option 2</sp-radio>
52
53
  <sp-radio value=${values[2]}>Option 3</sp-radio>
53
54
  </sp-radio-group>
54
55
  `);
55
- await elementUpdated(el);
56
- const first = el.querySelector('[value="first"]');
57
- const selected = el.querySelector('[value="second"]');
58
- expect(selected.tabIndex).to.equal(0);
59
- expect(first.tabIndex).to.equal(-1);
60
- const firstRect = first.getBoundingClientRect();
61
- await sendMouse({
62
- steps: [
63
- {
64
- type: 'move',
65
- position: [firstRect.x + 2, firstRect.y + 2],
66
- },
67
- {
68
- type: 'down',
69
- },
70
- ],
71
- });
72
- await elementUpdated(el);
73
- // Safari can have a situation where it thinks the root is focused, but really something inside of the
74
- // element is focused instead, this tests for both no focus on the root or focus inside of the element.
75
- expect(!first.matches(':focus') || first.matches(':focus-within'), 'root should not').to.be.true;
56
+ await elementUpdated(el);
57
+ const first = el.querySelector('[value="first"]');
58
+ const selected = el.querySelector('[value="second"]');
59
+ expect(selected.tabIndex).to.equal(0);
60
+ expect(first.tabIndex).to.equal(-1);
61
+ const firstRect = first.getBoundingClientRect();
62
+ await sendMouse({
63
+ steps: [
64
+ {
65
+ type: "move",
66
+ position: [firstRect.x + 2, firstRect.y + 2]
67
+ },
68
+ {
69
+ type: "down"
70
+ }
71
+ ]
76
72
  });
77
- it('does not select on focus', async () => {
78
- const el = await fixture(html `
73
+ await elementUpdated(el);
74
+ expect(!first.matches(":focus") || first.matches(":focus-within"), "root should not").to.be.true;
75
+ });
76
+ it("does not select on focus", async () => {
77
+ const el = await fixture(html`
79
78
  <sp-radio-group>
80
79
  <sp-radio value="1">Options 1</sp-radio>
81
80
  <sp-radio value="2">Options 2</sp-radio>
@@ -84,24 +83,24 @@ describe('Radio Group - focus control', () => {
84
83
  <sp-radio value="5">Options 5</sp-radio>
85
84
  </sp-radio-group>
86
85
  `);
87
- await elementUpdated(el);
88
- const radio1 = el.querySelector('sp-radio:nth-child(1)');
89
- const radio2 = el.querySelector('sp-radio:nth-child(2)');
90
- expect(el.selected).to.equal('');
91
- radio1.focus();
92
- await elementUpdated(el);
93
- expect(el.selected).to.equal('');
94
- el.selected = '1';
95
- await elementUpdated(el);
96
- expect(el.selected).to.equal('1');
97
- expect(radio1.checked).to.be.true;
98
- radio2.focus();
99
- await elementUpdated(el);
100
- expect(el.selected).to.equal('1');
101
- expect(radio1.checked).to.be.true;
102
- });
103
- it('loads accepts keyboard events while focused', async () => {
104
- const el = await fixture(html `
86
+ await elementUpdated(el);
87
+ const radio1 = el.querySelector("sp-radio:nth-child(1)");
88
+ const radio2 = el.querySelector("sp-radio:nth-child(2)");
89
+ expect(el.selected).to.equal("");
90
+ radio1.focus();
91
+ await elementUpdated(el);
92
+ expect(el.selected).to.equal("");
93
+ el.selected = "1";
94
+ await elementUpdated(el);
95
+ expect(el.selected).to.equal("1");
96
+ expect(radio1.checked).to.be.true;
97
+ radio2.focus();
98
+ await elementUpdated(el);
99
+ expect(el.selected).to.equal("1");
100
+ expect(radio1.checked).to.be.true;
101
+ });
102
+ it("loads accepts keyboard events while focused", async () => {
103
+ const el = await fixture(html`
105
104
  <sp-radio-group>
106
105
  <sp-radio>Options 1</sp-radio>
107
106
  <sp-radio>Options 2</sp-radio>
@@ -110,36 +109,36 @@ describe('Radio Group - focus control', () => {
110
109
  <sp-radio>Options 5</sp-radio>
111
110
  </sp-radio-group>
112
111
  `);
113
- await elementUpdated(el);
114
- const radio1 = el.querySelector('sp-radio:nth-child(1)');
115
- const radio2 = el.querySelector('sp-radio:nth-child(2)');
116
- const radio3 = el.querySelector('sp-radio:nth-child(3)');
117
- const radio4 = el.querySelector('sp-radio:nth-child(4)');
118
- const radio5 = el.querySelector('sp-radio:nth-child(5)');
119
- radio1.focus();
120
- await elementUpdated(el);
121
- el.dispatchEvent(arrowRightEvent());
122
- await elementUpdated(el);
123
- expect(document.activeElement === radio2).to.be.true;
124
- el.dispatchEvent(arrowDownEvent());
125
- await elementUpdated(el);
126
- expect(document.activeElement === radio3).to.be.true;
127
- el.dispatchEvent(endEvent());
128
- await elementUpdated(el);
129
- expect(document.activeElement === radio5).to.be.true;
130
- el.dispatchEvent(arrowLeftEvent());
131
- await elementUpdated(el);
132
- expect(document.activeElement === radio4).to.be.true;
133
- el.dispatchEvent(arrowUpEvent());
134
- await elementUpdated(el);
135
- expect(document.activeElement === radio3).to.be.true;
136
- el.dispatchEvent(homeEvent());
137
- await elementUpdated(el);
138
- expect(document.activeElement === radio1).to.be.true;
139
- radio1.blur();
140
- });
141
- it('accepts keyboard interactions where `checked` and `calculateFocusInIndex` might conflict', async () => {
142
- const el = await fixture(html `
112
+ await elementUpdated(el);
113
+ const radio1 = el.querySelector("sp-radio:nth-child(1)");
114
+ const radio2 = el.querySelector("sp-radio:nth-child(2)");
115
+ const radio3 = el.querySelector("sp-radio:nth-child(3)");
116
+ const radio4 = el.querySelector("sp-radio:nth-child(4)");
117
+ const radio5 = el.querySelector("sp-radio:nth-child(5)");
118
+ radio1.focus();
119
+ await elementUpdated(el);
120
+ el.dispatchEvent(arrowRightEvent());
121
+ await elementUpdated(el);
122
+ expect(document.activeElement === radio2).to.be.true;
123
+ el.dispatchEvent(arrowDownEvent());
124
+ await elementUpdated(el);
125
+ expect(document.activeElement === radio3).to.be.true;
126
+ el.dispatchEvent(endEvent());
127
+ await elementUpdated(el);
128
+ expect(document.activeElement === radio5).to.be.true;
129
+ el.dispatchEvent(arrowLeftEvent());
130
+ await elementUpdated(el);
131
+ expect(document.activeElement === radio4).to.be.true;
132
+ el.dispatchEvent(arrowUpEvent());
133
+ await elementUpdated(el);
134
+ expect(document.activeElement === radio3).to.be.true;
135
+ el.dispatchEvent(homeEvent());
136
+ await elementUpdated(el);
137
+ expect(document.activeElement === radio1).to.be.true;
138
+ radio1.blur();
139
+ });
140
+ it("accepts keyboard interactions where `checked` and `calculateFocusInIndex` might conflict", async () => {
141
+ const el = await fixture(html`
143
142
  <sp-radio-group>
144
143
  <sp-radio>Options 1</sp-radio>
145
144
  <sp-radio>Options 2</sp-radio>
@@ -148,22 +147,22 @@ describe('Radio Group - focus control', () => {
148
147
  <sp-radio>Options 5</sp-radio>
149
148
  </sp-radio-group>
150
149
  `);
151
- await elementUpdated(el);
152
- const radio1 = el.querySelector('sp-radio:nth-child(1)');
153
- const radio5 = el.querySelector('sp-radio:nth-child(5)');
154
- radio5.focus();
155
- await elementUpdated(el);
156
- expect(document.activeElement === radio5).to.be.true;
157
- expect(radio5.checked).to.be.true;
158
- await sendKeys({
159
- press: 'ArrowRight',
160
- });
161
- await elementUpdated(el);
162
- expect(document.activeElement === radio1).to.be.true;
163
- expect(radio1.checked).to.be.true;
150
+ await elementUpdated(el);
151
+ const radio1 = el.querySelector("sp-radio:nth-child(1)");
152
+ const radio5 = el.querySelector("sp-radio:nth-child(5)");
153
+ radio5.focus();
154
+ await elementUpdated(el);
155
+ expect(document.activeElement === radio5).to.be.true;
156
+ expect(radio5.checked).to.be.true;
157
+ await sendKeys({
158
+ press: "ArrowRight"
164
159
  });
165
- it('acknowledges `disabled` and accepts keyboard events while focused', async () => {
166
- const el = await fixture(html `
160
+ await elementUpdated(el);
161
+ expect(document.activeElement === radio1).to.be.true;
162
+ expect(radio1.checked).to.be.true;
163
+ });
164
+ it("acknowledges `disabled` and accepts keyboard events while focused", async () => {
165
+ const el = await fixture(html`
167
166
  <sp-radio-group>
168
167
  <sp-radio value="1" disabled>Option 1</sp-radio>
169
168
  <sp-radio value="2">Option 2</sp-radio>
@@ -172,52 +171,48 @@ describe('Radio Group - focus control', () => {
172
171
  <sp-radio value="5" disabled>Option 5</sp-radio>
173
172
  </sp-radio-group>
174
173
  `);
175
- await elementUpdated(el);
176
- const radio2 = el.querySelector('sp-radio:nth-child(2)');
177
- const radio4 = el.querySelector('sp-radio:nth-child(4)');
178
- radio2.focus();
179
- await elementUpdated(el);
180
- expect(document.activeElement === radio2, 'start 2').to.be.true;
181
- expect(el.selected).to.equal('');
182
- el.dispatchEvent(enterEvent());
183
- el.dispatchEvent(endEvent());
184
- await elementUpdated(el);
185
- expect(document.activeElement === radio4, 'first 4').to.be.true;
186
- expect(el.selected).to.equal('4');
187
- el.dispatchEvent(homeEvent());
188
- await elementUpdated(el);
189
- expect(document.activeElement === radio2, 'second 2').to.be.true;
190
- el.dispatchEvent(arrowUpEvent());
191
- await elementUpdated(el);
192
- expect(document.activeElement === radio4, 'third 4').to.be.true;
193
- el.dispatchEvent(arrowDownEvent());
194
- await elementUpdated(el);
195
- expect(document.activeElement === radio2, 'fourth 2').to.be.true;
196
- });
174
+ await elementUpdated(el);
175
+ const radio2 = el.querySelector("sp-radio:nth-child(2)");
176
+ const radio4 = el.querySelector("sp-radio:nth-child(4)");
177
+ radio2.focus();
178
+ await elementUpdated(el);
179
+ expect(document.activeElement === radio2, "start 2").to.be.true;
180
+ expect(el.selected).to.equal("");
181
+ el.dispatchEvent(enterEvent());
182
+ el.dispatchEvent(endEvent());
183
+ await elementUpdated(el);
184
+ expect(document.activeElement === radio4, "first 4").to.be.true;
185
+ expect(el.selected).to.equal("4");
186
+ el.dispatchEvent(homeEvent());
187
+ await elementUpdated(el);
188
+ expect(document.activeElement === radio2, "second 2").to.be.true;
189
+ el.dispatchEvent(arrowUpEvent());
190
+ await elementUpdated(el);
191
+ expect(document.activeElement === radio4, "third 4").to.be.true;
192
+ el.dispatchEvent(arrowDownEvent());
193
+ await elementUpdated(el);
194
+ expect(document.activeElement === radio2, "fourth 2").to.be.true;
195
+ });
197
196
  });
198
- describe('Group Accessibility', () => {
199
- it('created the expected accessibility tree', async () => {
200
- await fixture(html `
197
+ describe("Group Accessibility", () => {
198
+ it("created the expected accessibility tree", async () => {
199
+ await fixture(html`
201
200
  <sp-radio-group label="Testing Label" tabindex="0">
202
201
  <sp-radio value="first">Option 1</sp-radio>
203
202
  <sp-radio value="second" checked>Option 2</sp-radio>
204
203
  <sp-radio value="third">Option 3</sp-radio>
205
204
  </sp-radio-group>
206
205
  `);
207
- const snapshot = (await a11ySnapshot({}));
208
- expect(findAccessibilityNode(snapshot, (node // Firefox uses 'group' instead of 'radiogroup' here.
209
- ) => (node.role === 'radiogroup' || node.role === 'group') &&
210
- node.name === 'Testing Label'), 'Has a "radiogroup" with the supplied name').to.not.be.null;
211
- expect(findAccessibilityNode(snapshot, (node) => node.role === 'radio' &&
212
- node.checked &&
213
- node.name === 'Option 2'), 'Has a named and checked "radio" element').to.not.be.null;
214
- expect(findAccessibilityNode(snapshot, (node) => node.name === 'Option 2' && node.role.startsWith('text')), 'Does not have a text leaf named like the "radio" element').to.be.null;
215
- });
206
+ const snapshot = await a11ySnapshot({});
207
+ expect(findAccessibilityNode(snapshot, (node) => (node.role === "radiogroup" || node.role === "group") && node.name === "Testing Label"), 'Has a "radiogroup" with the supplied name').to.not.be.null;
208
+ expect(findAccessibilityNode(snapshot, (node) => node.role === "radio" && node.checked && node.name === "Option 2"), 'Has a named and checked "radio" element').to.not.be.null;
209
+ expect(findAccessibilityNode(snapshot, (node) => node.name === "Option 2" && node.role.startsWith("text")), 'Does not have a text leaf named like the "radio" element').to.be.null;
210
+ });
216
211
  });
217
- describe('Radio Group', () => {
218
- let testDiv;
219
- beforeEach(async () => {
220
- testDiv = await fixture(html `
212
+ describe("Radio Group", () => {
213
+ let testDiv;
214
+ beforeEach(async () => {
215
+ testDiv = await fixture(html`
221
216
  <div id="test-radio-group">
222
217
  <sp-radio-group id="test-default">
223
218
  <sp-radio value="first" checked>Option 1</sp-radio>
@@ -265,154 +260,154 @@ describe('Radio Group', () => {
265
260
  </sp-radio-group>
266
261
  </div>
267
262
  `);
268
- });
269
- it('loads', () => {
270
- const radioGroup = testDiv.querySelector('sp-radio-group#test-default');
271
- const radioChildren = radioGroup.querySelectorAll('sp-radio');
272
- expect(radioGroup).to.exist;
273
- expect(radioChildren.length).to.equal(3);
274
- });
275
- it('loads accessibly', async () => {
276
- await expect(testDiv).to.be.accessible();
277
- });
278
- it('validates selection', async () => {
279
- const radioGroup = testDiv.querySelector('sp-radio-group#test-none-selected');
280
- expect(radioGroup.selected).to.equal('');
281
- radioGroup.selected = 'missing';
282
- await elementUpdated(radioGroup);
283
- expect(radioGroup.selected).to.equal('');
284
- });
285
- it('can have selection prevented', async () => {
286
- const el = testDiv.querySelector('sp-radio-group#test-default');
287
- const secondRadio = el.querySelector('sp-radio[value=second]');
288
- const thirdRadio = el.querySelector('sp-radio[value=third]');
289
- await elementUpdated(el);
290
- expect(el.selected).to.equal('first');
291
- secondRadio.click();
292
- await elementUpdated(el);
293
- expect(el.selected).to.equal('second');
294
- el.addEventListener('change', (event) => event.preventDefault());
295
- thirdRadio.click();
296
- await elementUpdated(el);
297
- expect(el.selected).to.equal('second');
298
- });
299
- it('reflects checked radio with selected property', async () => {
300
- const radioGroup = testDiv.querySelector('sp-radio-group#test-default');
301
- const firstRadio = radioGroup.querySelector('sp-radio[value=first]');
302
- const secondRadio = radioGroup.querySelector('sp-radio[value=second]');
303
- const thirdRadio = radioGroup.querySelector('sp-radio[value=third]');
304
- expect(firstRadio.checked).to.be.true;
305
- expect(secondRadio.checked).to.be.false;
306
- expect(thirdRadio.checked).to.be.false;
307
- expect(radioGroup.selected).to.equal(firstRadio.value);
308
- secondRadio.click();
309
- await elementUpdated(radioGroup);
310
- expect(firstRadio.checked).to.be.false;
311
- expect(secondRadio.checked).to.be.true;
312
- expect(thirdRadio.checked).to.be.false;
313
- expect(radioGroup.selected).to.equal(secondRadio.value);
314
- thirdRadio.click();
315
- await elementUpdated(radioGroup);
316
- expect(firstRadio.checked).to.be.false;
317
- expect(secondRadio.checked).to.be.false;
318
- expect(thirdRadio.checked).to.be.true;
319
- expect(radioGroup.selected).to.equal(thirdRadio.value);
320
- });
321
- it('forces only one radio to be checked', () => {
322
- const radioGroup = testDiv.querySelector('sp-radio-group#test-multiple-checked');
323
- const checkedRadios = radioGroup.querySelectorAll('sp-radio[checked]');
324
- expect(radioGroup.selected).to.equal('first');
325
- expect(checkedRadios.length).to.equal(1);
326
- });
327
- it('respects clicking on disabled attribute causing nothing to happen', async () => {
328
- const radioGroup = testDiv.querySelector('sp-radio-group#test-disabled');
329
- const checkedRadio = radioGroup.querySelector('sp-radio[checked]');
330
- const disabledRadio = radioGroup.querySelector('sp-radio[disabled]');
331
- disabledRadio.click();
332
- await elementUpdated(radioGroup);
333
- expect(disabledRadio.checked).to.be.false;
334
- expect(checkedRadio.checked).to.be.true;
335
- expect(radioGroup.selected).to.equal(checkedRadio.value);
336
- });
337
- it('de-checks all but first checked radio if multiple checked', () => {
338
- const radioGroup = testDiv.querySelector('sp-radio-group#test-multiple-checked');
339
- const radio1 = radioGroup.querySelector('sp-radio[value=first]');
340
- const radio2 = radioGroup.querySelector('sp-radio[value=second]');
341
- expect(radioGroup.selected).to.equal('first');
342
- expect(radio1.checked).to.be.true;
343
- expect(radio2.checked).to.be.false;
344
- });
345
- it('ensures setting selection updates checked radio', async () => {
346
- const radioGroup = testDiv.querySelector('sp-radio-group#test-selected');
347
- const radio1 = radioGroup.querySelector('sp-radio[value=first]');
348
- const radio2 = radioGroup.querySelector('sp-radio[value=second]');
349
- const radio3 = radioGroup.querySelector('sp-radio[value=third]');
350
- expect(radioGroup.selected).to.equal('third');
351
- expect(radio1.checked).to.be.false;
352
- expect(radio2.checked).to.be.false;
353
- expect(radio3.checked, 'initial').to.be.true;
354
- radioGroup.selected = 'second';
355
- await elementUpdated(radioGroup);
356
- expect(radioGroup.selected).to.equal('second');
357
- expect(radio1.checked).to.be.false;
358
- expect(radio2.checked, 'second').to.be.true;
359
- expect(radio3.checked).to.be.false;
360
- radioGroup.selected = 'first';
361
- await elementUpdated(radioGroup);
362
- expect(radioGroup.selected).to.equal('first');
363
- expect(radio1.checked, 'third').to.be.true;
364
- expect(radio2.checked).to.be.false;
365
- expect(radio3.checked).to.be.false;
366
- });
367
- it('ensures setting selected and clicking on radio both work together', async () => {
368
- const radioGroup = testDiv.querySelector('sp-radio-group#test-selected-click');
369
- const radio1 = radioGroup.querySelector('sp-radio[value=first]');
370
- const radio2 = radioGroup.querySelector('sp-radio[value=second]');
371
- const radio3 = radioGroup.querySelector('sp-radio[value=third]');
372
- expect(radioGroup.selected).to.equal('third');
373
- radio2.click();
374
- await elementUpdated(radioGroup);
375
- expect(radioGroup.selected).to.equal('second');
376
- expect(radio1.checked).to.be.false;
377
- expect(radio2.checked).to.be.true;
378
- expect(radio3.checked).to.be.false;
379
- radioGroup.selected = 'first';
380
- await elementUpdated(radioGroup);
381
- expect(radioGroup.selected).to.equal('first');
382
- expect(radio1.checked, 'moved to checked').to.be.true;
383
- expect(radio2.checked).to.be.false;
384
- expect(radio3.checked).to.be.false;
385
- });
386
- it('prioritizes checked over selected on initialization when conflicting', () => {
387
- const radioGroup = testDiv.querySelector('sp-radio-group#test-checked-prioritized');
388
- const radio1 = radioGroup.querySelector('sp-radio[value=first]');
389
- const radio2 = radioGroup.querySelector('sp-radio[value=second]');
390
- expect(radioGroup.selected).to.equal('first');
391
- expect(radio1.checked).to.be.true;
392
- expect(radio2.checked).to.be.false;
393
- });
394
- it('handles integer values for radio buttons', () => {
395
- const radioGroup = testDiv.querySelector('sp-radio-group#test-integer-value');
396
- expect(radioGroup.selected).to.equal('5');
397
- });
398
- it('prevents `change` events from radio buttons', async () => {
399
- const changeSpy = spy();
400
- const onChange = (event) => {
401
- changeSpy(event.target.selected);
402
- };
403
- const el = await fixture(html `
263
+ });
264
+ it("loads", () => {
265
+ const radioGroup = testDiv.querySelector("sp-radio-group#test-default");
266
+ const radioChildren = radioGroup.querySelectorAll("sp-radio");
267
+ expect(radioGroup).to.exist;
268
+ expect(radioChildren.length).to.equal(3);
269
+ });
270
+ it("loads accessibly", async () => {
271
+ await expect(testDiv).to.be.accessible();
272
+ });
273
+ it("validates selection", async () => {
274
+ const radioGroup = testDiv.querySelector("sp-radio-group#test-none-selected");
275
+ expect(radioGroup.selected).to.equal("");
276
+ radioGroup.selected = "missing";
277
+ await elementUpdated(radioGroup);
278
+ expect(radioGroup.selected).to.equal("");
279
+ });
280
+ it("can have selection prevented", async () => {
281
+ const el = testDiv.querySelector("sp-radio-group#test-default");
282
+ const secondRadio = el.querySelector("sp-radio[value=second]");
283
+ const thirdRadio = el.querySelector("sp-radio[value=third]");
284
+ await elementUpdated(el);
285
+ expect(el.selected).to.equal("first");
286
+ secondRadio.click();
287
+ await elementUpdated(el);
288
+ expect(el.selected).to.equal("second");
289
+ el.addEventListener("change", (event) => event.preventDefault());
290
+ thirdRadio.click();
291
+ await elementUpdated(el);
292
+ expect(el.selected).to.equal("second");
293
+ });
294
+ it("reflects checked radio with selected property", async () => {
295
+ const radioGroup = testDiv.querySelector("sp-radio-group#test-default");
296
+ const firstRadio = radioGroup.querySelector("sp-radio[value=first]");
297
+ const secondRadio = radioGroup.querySelector("sp-radio[value=second]");
298
+ const thirdRadio = radioGroup.querySelector("sp-radio[value=third]");
299
+ expect(firstRadio.checked).to.be.true;
300
+ expect(secondRadio.checked).to.be.false;
301
+ expect(thirdRadio.checked).to.be.false;
302
+ expect(radioGroup.selected).to.equal(firstRadio.value);
303
+ secondRadio.click();
304
+ await elementUpdated(radioGroup);
305
+ expect(firstRadio.checked).to.be.false;
306
+ expect(secondRadio.checked).to.be.true;
307
+ expect(thirdRadio.checked).to.be.false;
308
+ expect(radioGroup.selected).to.equal(secondRadio.value);
309
+ thirdRadio.click();
310
+ await elementUpdated(radioGroup);
311
+ expect(firstRadio.checked).to.be.false;
312
+ expect(secondRadio.checked).to.be.false;
313
+ expect(thirdRadio.checked).to.be.true;
314
+ expect(radioGroup.selected).to.equal(thirdRadio.value);
315
+ });
316
+ it("forces only one radio to be checked", () => {
317
+ const radioGroup = testDiv.querySelector("sp-radio-group#test-multiple-checked");
318
+ const checkedRadios = radioGroup.querySelectorAll("sp-radio[checked]");
319
+ expect(radioGroup.selected).to.equal("first");
320
+ expect(checkedRadios.length).to.equal(1);
321
+ });
322
+ it("respects clicking on disabled attribute causing nothing to happen", async () => {
323
+ const radioGroup = testDiv.querySelector("sp-radio-group#test-disabled");
324
+ const checkedRadio = radioGroup.querySelector("sp-radio[checked]");
325
+ const disabledRadio = radioGroup.querySelector("sp-radio[disabled]");
326
+ disabledRadio.click();
327
+ await elementUpdated(radioGroup);
328
+ expect(disabledRadio.checked).to.be.false;
329
+ expect(checkedRadio.checked).to.be.true;
330
+ expect(radioGroup.selected).to.equal(checkedRadio.value);
331
+ });
332
+ it("de-checks all but first checked radio if multiple checked", () => {
333
+ const radioGroup = testDiv.querySelector("sp-radio-group#test-multiple-checked");
334
+ const radio1 = radioGroup.querySelector("sp-radio[value=first]");
335
+ const radio2 = radioGroup.querySelector("sp-radio[value=second]");
336
+ expect(radioGroup.selected).to.equal("first");
337
+ expect(radio1.checked).to.be.true;
338
+ expect(radio2.checked).to.be.false;
339
+ });
340
+ it("ensures setting selection updates checked radio", async () => {
341
+ const radioGroup = testDiv.querySelector("sp-radio-group#test-selected");
342
+ const radio1 = radioGroup.querySelector("sp-radio[value=first]");
343
+ const radio2 = radioGroup.querySelector("sp-radio[value=second]");
344
+ const radio3 = radioGroup.querySelector("sp-radio[value=third]");
345
+ expect(radioGroup.selected).to.equal("third");
346
+ expect(radio1.checked).to.be.false;
347
+ expect(radio2.checked).to.be.false;
348
+ expect(radio3.checked, "initial").to.be.true;
349
+ radioGroup.selected = "second";
350
+ await elementUpdated(radioGroup);
351
+ expect(radioGroup.selected).to.equal("second");
352
+ expect(radio1.checked).to.be.false;
353
+ expect(radio2.checked, "second").to.be.true;
354
+ expect(radio3.checked).to.be.false;
355
+ radioGroup.selected = "first";
356
+ await elementUpdated(radioGroup);
357
+ expect(radioGroup.selected).to.equal("first");
358
+ expect(radio1.checked, "third").to.be.true;
359
+ expect(radio2.checked).to.be.false;
360
+ expect(radio3.checked).to.be.false;
361
+ });
362
+ it("ensures setting selected and clicking on radio both work together", async () => {
363
+ const radioGroup = testDiv.querySelector("sp-radio-group#test-selected-click");
364
+ const radio1 = radioGroup.querySelector("sp-radio[value=first]");
365
+ const radio2 = radioGroup.querySelector("sp-radio[value=second]");
366
+ const radio3 = radioGroup.querySelector("sp-radio[value=third]");
367
+ expect(radioGroup.selected).to.equal("third");
368
+ radio2.click();
369
+ await elementUpdated(radioGroup);
370
+ expect(radioGroup.selected).to.equal("second");
371
+ expect(radio1.checked).to.be.false;
372
+ expect(radio2.checked).to.be.true;
373
+ expect(radio3.checked).to.be.false;
374
+ radioGroup.selected = "first";
375
+ await elementUpdated(radioGroup);
376
+ expect(radioGroup.selected).to.equal("first");
377
+ expect(radio1.checked, "moved to checked").to.be.true;
378
+ expect(radio2.checked).to.be.false;
379
+ expect(radio3.checked).to.be.false;
380
+ });
381
+ it("prioritizes checked over selected on initialization when conflicting", () => {
382
+ const radioGroup = testDiv.querySelector("sp-radio-group#test-checked-prioritized");
383
+ const radio1 = radioGroup.querySelector("sp-radio[value=first]");
384
+ const radio2 = radioGroup.querySelector("sp-radio[value=second]");
385
+ expect(radioGroup.selected).to.equal("first");
386
+ expect(radio1.checked).to.be.true;
387
+ expect(radio2.checked).to.be.false;
388
+ });
389
+ it("handles integer values for radio buttons", () => {
390
+ const radioGroup = testDiv.querySelector("sp-radio-group#test-integer-value");
391
+ expect(radioGroup.selected).to.equal("5");
392
+ });
393
+ it("prevents `change` events from radio buttons", async () => {
394
+ const changeSpy = spy();
395
+ const onChange = (event) => {
396
+ changeSpy(event.target.selected);
397
+ };
398
+ const el = await fixture(html`
404
399
  <sp-radio-group @change=${onChange}>
405
400
  <sp-radio value="bulbasaur">Bulbasaur</sp-radio>
406
401
  <sp-radio value="squirtle">Squirtle</sp-radio>
407
402
  <sp-radio value="charmander">Charmander</sp-radio>
408
403
  </sp-radio-group>
409
404
  `);
410
- const bulbasaur = el.querySelector('[value="bulbasaur"]');
411
- const charmander = el.querySelector('[value="charmander"]');
412
- bulbasaur.click();
413
- bulbasaur.click();
414
- charmander.click();
415
- expect(changeSpy.calledWith(undefined)).to.be.false;
416
- });
405
+ const bulbasaur = el.querySelector('[value="bulbasaur"]');
406
+ const charmander = el.querySelector('[value="charmander"]');
407
+ bulbasaur.click();
408
+ bulbasaur.click();
409
+ charmander.click();
410
+ expect(changeSpy.calledWith(void 0)).to.be.false;
411
+ });
417
412
  });
418
- //# sourceMappingURL=radio-group.test.js.map
413
+ //# sourceMappingURL=radio-group.test.js.map