2022-07-12 23:17:59 +03:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2019 Google LLC
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {html} from 'lit';
|
|
|
|
|
2022-10-03 21:57:50 +03:00
|
|
|
import {Environment} from '../testing/environment.js';
|
2023-05-17 06:27:58 +03:00
|
|
|
import {createFormTests} from '../testing/forms.js';
|
2023-01-05 02:00:49 +03:00
|
|
|
import {createTokenTests} from '../testing/tokens.js';
|
2022-10-03 21:57:50 +03:00
|
|
|
|
2022-08-24 21:00:50 +03:00
|
|
|
import {RadioHarness} from './harness.js';
|
|
|
|
import {MdRadio} from './radio.js';
|
2022-07-12 23:17:59 +03:00
|
|
|
|
|
|
|
const defaultRadio = html`<md-radio></md-radio>`;
|
|
|
|
|
|
|
|
const radioGroup = html`
|
2022-12-21 22:46:47 +03:00
|
|
|
<md-radio id="a1" name="a"></md-radio>
|
|
|
|
<md-radio id="a2" name="a"></md-radio>
|
|
|
|
<md-radio id="b1" name="b"></md-radio>
|
|
|
|
`;
|
2022-07-12 23:17:59 +03:00
|
|
|
|
|
|
|
const radioGroupDisabled = html`
|
2022-12-21 22:46:47 +03:00
|
|
|
<md-radio id="a1" name="a" disabled></md-radio>
|
|
|
|
<md-radio id="a2" name="a" disabled checked></md-radio>
|
|
|
|
`;
|
2022-07-12 23:17:59 +03:00
|
|
|
|
|
|
|
const radioGroupPreSelected = html`
|
2022-12-21 22:46:47 +03:00
|
|
|
<md-radio id="a1" name="a"></md-radio>
|
|
|
|
<md-radio id="a2" name="a" checked></md-radio>
|
|
|
|
<md-radio id="a3" name="a"></md-radio>
|
|
|
|
<md-radio id="b1" name="b"></md-radio>
|
|
|
|
`;
|
2022-07-12 23:17:59 +03:00
|
|
|
|
2023-01-05 02:00:49 +03:00
|
|
|
describe('<md-radio>', () => {
|
2022-10-03 21:57:50 +03:00
|
|
|
const env = new Environment();
|
|
|
|
|
2022-12-21 04:19:18 +03:00
|
|
|
// Note, this would be better in the harness, but waiting in the test setup
|
2023-05-30 20:33:46 +03:00
|
|
|
// can be flaky without access to the test `env`.
|
2022-12-21 04:19:18 +03:00
|
|
|
async function simulateKeyDown(element: HTMLElement, key: string) {
|
|
|
|
const event = new KeyboardEvent('keydown', {key, bubbles: true});
|
|
|
|
element.dispatchEvent(event);
|
2024-02-21 02:32:34 +03:00
|
|
|
// We can remove the delay when FF issue addressed:
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1804576
|
2022-12-21 04:19:18 +03:00
|
|
|
await env.waitForStability();
|
|
|
|
}
|
|
|
|
|
2022-12-21 22:46:47 +03:00
|
|
|
async function setupTest(template = defaultRadio) {
|
|
|
|
const root = env.render(template);
|
|
|
|
await env.waitForStability();
|
|
|
|
const radios = Array.from(root.querySelectorAll('md-radio'));
|
2023-10-25 21:58:21 +03:00
|
|
|
const harnesses = radios.map((radio) => new RadioHarness(radio));
|
2022-12-21 22:46:47 +03:00
|
|
|
return {harnesses, root};
|
|
|
|
}
|
2022-07-12 23:17:59 +03:00
|
|
|
|
2023-01-05 02:00:49 +03:00
|
|
|
describe('.styles', () => {
|
|
|
|
createTokenTests(MdRadio.styles);
|
|
|
|
});
|
|
|
|
|
2022-07-12 23:17:59 +03:00
|
|
|
describe('basic', () => {
|
|
|
|
it('initializes as an md-radio', async () => {
|
2022-12-21 22:46:47 +03:00
|
|
|
const {harnesses} = await setupTest();
|
|
|
|
expect(harnesses[0].element).toBeInstanceOf(MdRadio);
|
2022-07-12 23:17:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('clicking a radio should select it', async () => {
|
2022-12-21 22:46:47 +03:00
|
|
|
const {harnesses} = await setupTest(radioGroup);
|
|
|
|
const unselected = harnesses[1];
|
|
|
|
expect(unselected.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unselected checked')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
|
|
|
|
await unselected.clickWithMouse();
|
|
|
|
expect(unselected.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('after clicking checked')
|
|
|
|
.toBeTrue();
|
2022-07-12 23:17:59 +03:00
|
|
|
});
|
|
|
|
|
2023-08-25 02:45:22 +03:00
|
|
|
it('clicking a radio can be default prevented', async () => {
|
|
|
|
const {harnesses} = await setupTest(radioGroup);
|
|
|
|
const unselected = harnesses[1];
|
|
|
|
expect(unselected.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unselected checked')
|
|
|
|
.toBeFalse();
|
2023-08-25 02:45:22 +03:00
|
|
|
|
2023-10-25 21:58:21 +03:00
|
|
|
unselected.element.addEventListener('click', (event) => {
|
2023-08-25 02:45:22 +03:00
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
await unselected.clickWithMouse();
|
|
|
|
expect(unselected.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('after clicking checked')
|
|
|
|
.toBeFalse();
|
2023-08-25 02:45:22 +03:00
|
|
|
});
|
|
|
|
|
2023-10-25 21:58:21 +03:00
|
|
|
it('clicking a radio should unselect other radio which is already selected', async () => {
|
|
|
|
const {harnesses} = await setupTest(radioGroupPreSelected);
|
|
|
|
const [, a2, a3] = harnesses;
|
|
|
|
expect(a2.element.checked).withContext('already checked').toBeTrue();
|
2022-12-21 22:46:47 +03:00
|
|
|
|
2023-10-25 21:58:21 +03:00
|
|
|
await a3.clickWithMouse();
|
|
|
|
expect(a3.element.checked).withContext('new radio checked').toBeTrue();
|
|
|
|
expect(a2.element.checked)
|
|
|
|
.withContext('previous radio checked')
|
|
|
|
.toBeFalse();
|
|
|
|
});
|
2022-07-12 23:17:59 +03:00
|
|
|
|
|
|
|
it('disabled radio should not be selected when clicked', async () => {
|
2022-12-21 22:46:47 +03:00
|
|
|
const {harnesses} = await setupTest(radioGroupDisabled);
|
|
|
|
const [a1, a2] = harnesses;
|
2022-07-12 23:17:59 +03:00
|
|
|
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a1.element.checked).withContext('unchecked radio').toBeFalse();
|
|
|
|
expect(a2.element.checked).withContext('checked radio').toBeTrue();
|
2022-07-12 23:17:59 +03:00
|
|
|
|
2022-12-21 22:46:47 +03:00
|
|
|
await a1.clickWithMouse();
|
|
|
|
expect(a1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('still unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
|
|
|
|
await a2.clickWithMouse();
|
|
|
|
expect(a2.element.checked).withContext('still checked radio').toBeTrue();
|
2022-07-12 23:17:59 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('events', () => {
|
|
|
|
it('Should trigger change event when a radio is selected', async () => {
|
2022-12-21 22:46:47 +03:00
|
|
|
const {harnesses, root} = await setupTest(radioGroupPreSelected);
|
2022-07-12 23:17:59 +03:00
|
|
|
const changeHandler = jasmine.createSpy('changeHandler');
|
2022-10-03 21:57:50 +03:00
|
|
|
root.addEventListener('change', changeHandler);
|
2022-07-12 23:17:59 +03:00
|
|
|
|
2022-12-21 22:46:47 +03:00
|
|
|
const a3 = harnesses[2];
|
|
|
|
await a3.clickWithMouse();
|
2022-07-12 23:17:59 +03:00
|
|
|
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a3.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('clicked radio checked')
|
|
|
|
.toBeTrue();
|
2022-07-12 23:17:59 +03:00
|
|
|
expect(changeHandler).toHaveBeenCalledTimes(1);
|
|
|
|
expect(changeHandler).toHaveBeenCalledWith(jasmine.any(Event));
|
|
|
|
});
|
2023-09-15 03:25:19 +03:00
|
|
|
|
|
|
|
it('Should trigger input event when a radio is selected', async () => {
|
|
|
|
const {harnesses, root} = await setupTest(radioGroupPreSelected);
|
|
|
|
const inputHandler = jasmine.createSpy('inputHandler');
|
|
|
|
root.addEventListener('input', inputHandler);
|
|
|
|
|
|
|
|
const a3 = harnesses[2];
|
|
|
|
await a3.clickWithMouse();
|
|
|
|
|
|
|
|
expect(a3.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('clicked radio checked')
|
|
|
|
.toBeTrue();
|
2023-09-15 03:25:19 +03:00
|
|
|
expect(inputHandler).toHaveBeenCalledTimes(1);
|
|
|
|
expect(inputHandler).toHaveBeenCalledWith(jasmine.any(InputEvent));
|
|
|
|
});
|
2022-07-12 23:17:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('navigation', () => {
|
|
|
|
it('Using arrow right should select the next radio button', async () => {
|
2022-12-21 22:46:47 +03:00
|
|
|
const {harnesses} = await setupTest(radioGroupPreSelected);
|
|
|
|
const [, a2, a3] = harnesses;
|
|
|
|
expect(a2.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('default checked radio')
|
|
|
|
.toBeTrue();
|
2022-07-12 23:17:59 +03:00
|
|
|
|
2022-12-21 22:46:47 +03:00
|
|
|
await simulateKeyDown(a2.element, 'ArrowRight');
|
2022-07-12 23:17:59 +03:00
|
|
|
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a3.element.checked).withContext('next radio checked').toBeTrue();
|
|
|
|
expect(a2.element.checked).withContext('prev radio checked').toBeFalse();
|
2022-07-12 23:17:59 +03:00
|
|
|
});
|
|
|
|
|
2023-07-05 23:27:12 +03:00
|
|
|
it('dispatched a change event on user navigation', async () => {
|
2023-07-03 12:17:09 +03:00
|
|
|
const {harnesses, root} = await setupTest(radioGroupPreSelected);
|
|
|
|
const changeHandler = jasmine.createSpy('changeHandler');
|
|
|
|
root.addEventListener('change', changeHandler);
|
|
|
|
const [, a2] = harnesses;
|
|
|
|
expect(a2.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('default checked radio')
|
|
|
|
.toBeTrue();
|
2023-07-05 23:27:12 +03:00
|
|
|
|
2023-07-03 12:17:09 +03:00
|
|
|
await simulateKeyDown(a2.element, 'ArrowRight');
|
|
|
|
|
|
|
|
expect(changeHandler).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2023-10-25 21:58:21 +03:00
|
|
|
it('Using arrow right on the last radio should select the first radio in that group', async () => {
|
|
|
|
const {harnesses} = await setupTest(radioGroupPreSelected);
|
|
|
|
const [a1, a2, a3, b1] = harnesses;
|
|
|
|
|
|
|
|
expect(a2.element.checked).toBeTrue();
|
|
|
|
|
|
|
|
await simulateKeyDown(a2.element, 'ArrowRight');
|
|
|
|
await simulateKeyDown(a3.element, 'ArrowRight');
|
|
|
|
|
|
|
|
expect(a3.element.checked).withContext('last radio checked').toBeFalse();
|
|
|
|
expect(a1.element.checked).withContext('first radio checked').toBeTrue();
|
|
|
|
expect(b1.element.checked)
|
|
|
|
.withContext('unrelated radio checked')
|
|
|
|
.toBeFalse();
|
|
|
|
});
|
2022-07-12 23:17:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('manages selection groups', () => {
|
|
|
|
it('synchronously', async () => {
|
2022-12-21 22:46:47 +03:00
|
|
|
const {harnesses} = await setupTest(radioGroup);
|
|
|
|
const [a1, a2, b1] = harnesses;
|
|
|
|
|
|
|
|
expect(a1.element.checked).withContext('initially unchecked').toBeFalse();
|
|
|
|
expect(a2.element.checked).withContext('initially unchecked').toBeFalse();
|
|
|
|
expect(b1.element.checked).withContext('initially unchecked').toBeFalse();
|
|
|
|
|
|
|
|
// Should uncheck previously checked radio
|
|
|
|
a2.element.checked = true;
|
|
|
|
a1.element.checked = true;
|
|
|
|
|
|
|
|
expect(a1.element.checked).withContext('last radio checked').toBeTrue();
|
|
|
|
expect(a2.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unchecked by last radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(b1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unrelated radio unchecked')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
|
|
|
|
// Should re-check radio
|
|
|
|
a2.element.checked = true;
|
|
|
|
a1.element.checked = true;
|
|
|
|
a2.element.checked = true;
|
|
|
|
expect(a1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unchecked by second radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a2.element.checked).withContext('last radio checked').toBeTrue();
|
|
|
|
expect(b1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unrelated radio unchecked')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
|
|
|
|
// Should ignore unrelated radios
|
|
|
|
a1.element.checked = true;
|
|
|
|
expect(a1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('related checked radio')
|
|
|
|
.toBeTrue();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a2.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('related unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(b1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unrelated unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
|
|
|
|
b1.element.checked = true;
|
|
|
|
expect(a1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('related checked radio')
|
|
|
|
.toBeTrue();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a2.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('related unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(b1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unrelated checked radio')
|
|
|
|
.toBeTrue();
|
2022-12-21 22:46:47 +03:00
|
|
|
|
|
|
|
a1.element.checked = false;
|
|
|
|
b1.element.checked = false;
|
|
|
|
expect(a1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('related unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a2.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('related unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(b1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unrelated unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-07-12 23:17:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('after updates settle', async () => {
|
2022-12-21 22:46:47 +03:00
|
|
|
const {harnesses} = await setupTest(radioGroup);
|
|
|
|
const [a1, a2, b1] = harnesses;
|
2023-10-25 21:58:21 +03:00
|
|
|
const allUpdatesComplete = () =>
|
|
|
|
Promise.all(harnesses.map((harness) => harness.element.updateComplete));
|
2022-07-12 23:17:59 +03:00
|
|
|
|
|
|
|
await allUpdatesComplete();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a1.element.checked).withContext('initially unchecked').toBeFalse();
|
|
|
|
expect(a2.element.checked).withContext('initially unchecked').toBeFalse();
|
|
|
|
expect(b1.element.checked).withContext('initially unchecked').toBeFalse();
|
2022-07-12 23:17:59 +03:00
|
|
|
|
2022-12-21 22:46:47 +03:00
|
|
|
// Should uncheck previously checked radio
|
|
|
|
a2.element.checked = true;
|
|
|
|
a1.element.checked = true;
|
2022-07-12 23:17:59 +03:00
|
|
|
await allUpdatesComplete();
|
|
|
|
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a1.element.checked).withContext('last radio checked').toBeTrue();
|
|
|
|
expect(a2.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unchecked by last radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(b1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unrelated radio unchecked')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
|
|
|
|
// Should re-check radio
|
|
|
|
a2.element.checked = true;
|
|
|
|
a1.element.checked = true;
|
|
|
|
a2.element.checked = true;
|
2022-07-12 23:17:59 +03:00
|
|
|
await allUpdatesComplete();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unchecked by second radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a2.element.checked).withContext('last radio checked').toBeTrue();
|
|
|
|
expect(b1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unrelated radio unchecked')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
|
|
|
|
// Should ignore unrelated radios
|
|
|
|
a1.element.checked = true;
|
|
|
|
expect(a1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('related checked radio')
|
|
|
|
.toBeTrue();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a2.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('related unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(b1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unrelated unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
|
|
|
|
b1.element.checked = true;
|
2022-07-12 23:17:59 +03:00
|
|
|
await allUpdatesComplete();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('related checked radio')
|
|
|
|
.toBeTrue();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a2.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('related unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(b1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unrelated checked radio')
|
|
|
|
.toBeTrue();
|
2022-12-21 22:46:47 +03:00
|
|
|
|
|
|
|
a1.element.checked = false;
|
|
|
|
b1.element.checked = false;
|
2022-07-12 23:17:59 +03:00
|
|
|
await allUpdatesComplete();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('related unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(a2.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('related unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-12-21 22:46:47 +03:00
|
|
|
expect(b1.element.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('unrelated unchecked radio')
|
|
|
|
.toBeFalse();
|
2022-07-12 23:17:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('when checked before connected', async () => {
|
2022-10-03 21:57:50 +03:00
|
|
|
const root = env.render(html`<main></main>`);
|
|
|
|
const container = root.querySelector('main')!;
|
2022-07-12 23:17:59 +03:00
|
|
|
|
|
|
|
const r1 = document.createElement('md-radio');
|
2022-12-21 22:46:47 +03:00
|
|
|
r1.setAttribute('name', 'a');
|
2022-07-12 23:17:59 +03:00
|
|
|
const r2 = document.createElement('md-radio');
|
2022-12-21 22:46:47 +03:00
|
|
|
r2.setAttribute('name', 'a');
|
2022-07-12 23:17:59 +03:00
|
|
|
const r3 = document.createElement('md-radio');
|
2022-12-21 22:46:47 +03:00
|
|
|
r3.setAttribute('name', 'a');
|
2022-07-12 23:17:59 +03:00
|
|
|
|
|
|
|
// r1 and r2 should both be checked, because even though they have the
|
|
|
|
// same name, they aren't yet connected to a root. Groups are scoped to
|
|
|
|
// roots, and we can't know which root a radio belongs to until it is
|
|
|
|
// connected to one. This matches native <input type="radio"> behavior.
|
|
|
|
r1.checked = true;
|
|
|
|
r2.checked = true;
|
|
|
|
expect(r1.checked).toBeTrue();
|
|
|
|
expect(r2.checked).toBeTrue();
|
|
|
|
expect(r3.checked).toBeFalse();
|
|
|
|
|
|
|
|
// Connecting r1 shouldn't change anything, since it's the only one in the
|
|
|
|
// group.
|
|
|
|
container.appendChild(r1);
|
|
|
|
expect(r1.checked).toBeTrue();
|
|
|
|
expect(r2.checked).toBeTrue();
|
|
|
|
expect(r3.checked).toBeFalse();
|
|
|
|
|
2022-12-21 22:46:47 +03:00
|
|
|
// Appending r2 should uncheck r1, because when a new checked radio is
|
2022-07-12 23:17:59 +03:00
|
|
|
// connected, it wins (this matches native input behavior).
|
|
|
|
container.appendChild(r2);
|
|
|
|
expect(r1.checked).toBeFalse();
|
|
|
|
expect(r2.checked).toBeTrue();
|
|
|
|
expect(r3.checked).toBeFalse();
|
|
|
|
|
|
|
|
// Appending r3 shouldn't change anything, because it's not checked.
|
|
|
|
container.appendChild(r3);
|
|
|
|
expect(r1.checked).toBeFalse();
|
|
|
|
expect(r2.checked).toBeTrue();
|
|
|
|
expect(r3.checked).toBeFalse();
|
|
|
|
|
|
|
|
// Checking r3 should uncheck r2 because it's now in the same group.
|
|
|
|
r3.checked = true;
|
|
|
|
expect(r1.checked).toBeFalse();
|
|
|
|
expect(r2.checked).toBeFalse();
|
|
|
|
expect(r3.checked).toBeTrue();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('in a lit repeat', async () => {
|
|
|
|
const values = ['a1', 'a2'];
|
2022-12-21 22:46:47 +03:00
|
|
|
const repeated = values.map(
|
2023-10-25 21:58:21 +03:00
|
|
|
(value) => html`<md-radio value=${value} name="a"></md-radio>`,
|
|
|
|
);
|
2022-12-21 22:46:47 +03:00
|
|
|
const root = env.render(html`${repeated}`);
|
2022-10-03 21:57:50 +03:00
|
|
|
await env.waitForStability();
|
|
|
|
const [a1, a2] = root.querySelectorAll('md-radio');
|
2022-07-12 23:17:59 +03:00
|
|
|
|
|
|
|
expect(a1.checked).toBeFalse();
|
|
|
|
expect(a2.checked).toBeFalse();
|
|
|
|
expect(a1.value).toEqual(values[0]);
|
|
|
|
expect(a2.value).toEqual(values[1]);
|
|
|
|
|
|
|
|
a1.checked = true;
|
|
|
|
expect(a1.checked).toBeTrue();
|
|
|
|
expect(a2.checked).toBeFalse();
|
|
|
|
|
|
|
|
a2.checked = true;
|
|
|
|
expect(a1.checked).toBeFalse();
|
|
|
|
expect(a2.checked).toBeTrue();
|
|
|
|
|
|
|
|
a2.checked = false;
|
|
|
|
expect(a1.checked).toBeFalse();
|
|
|
|
expect(a2.checked).toBeFalse();
|
|
|
|
});
|
|
|
|
});
|
2022-12-21 04:19:18 +03:00
|
|
|
|
|
|
|
describe('label activation', () => {
|
|
|
|
async function setupLabelTest() {
|
|
|
|
const root = env.render(html`
|
|
|
|
<label> <md-radio name="a"></md-radio></label>
|
|
|
|
<label> <md-radio name="a"></md-radio></label>
|
2023-10-25 21:58:21 +03:00
|
|
|
`);
|
2022-12-21 04:19:18 +03:00
|
|
|
await env.waitForStability();
|
|
|
|
// [[label, radio]]
|
2023-10-25 21:58:21 +03:00
|
|
|
return Array.from(root.querySelectorAll('label')).map(
|
|
|
|
(el) => [el, el.firstElementChild as MdRadio] as const,
|
|
|
|
);
|
2022-12-21 04:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
it('toggles when label is clicked', async () => {
|
2023-10-25 21:58:21 +03:00
|
|
|
const [[label1, radio1], [label2, radio2]] = await setupLabelTest();
|
2022-12-21 04:19:18 +03:00
|
|
|
|
|
|
|
label1.click();
|
|
|
|
await env.waitForStability();
|
|
|
|
expect(radio1.checked).toBeTrue();
|
|
|
|
expect(radio2.checked).toBeFalse();
|
|
|
|
|
|
|
|
label2.click();
|
|
|
|
await env.waitForStability();
|
|
|
|
expect(radio1.checked).toBeFalse();
|
|
|
|
expect(radio2.checked).toBeTrue();
|
|
|
|
});
|
|
|
|
});
|
2023-05-17 06:27:58 +03:00
|
|
|
|
|
|
|
describe('forms', () => {
|
|
|
|
createFormTests({
|
2023-10-25 21:58:21 +03:00
|
|
|
queryControl: (root) => root.querySelector('md-radio'),
|
2023-05-17 06:27:58 +03:00
|
|
|
valueTests: [
|
|
|
|
{
|
|
|
|
name: 'unnamed',
|
|
|
|
render: () => html`
|
|
|
|
<md-radio value="One" checked></md-radio>
|
|
|
|
<md-radio value="Two"></md-radio>
|
|
|
|
`,
|
|
|
|
assertValue(formData) {
|
|
|
|
expect(formData)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('should not add anything to form without a name')
|
|
|
|
.toHaveSize(0);
|
|
|
|
},
|
2023-05-17 06:27:58 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'unchecked',
|
|
|
|
render: () => html`
|
|
|
|
<md-radio name="radio" value="One"></md-radio>
|
|
|
|
<md-radio name="radio" value="Two"></md-radio>
|
|
|
|
`,
|
|
|
|
assertValue(formData) {
|
|
|
|
expect(formData)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('should not add anything to form when unchecked')
|
|
|
|
.toHaveSize(0);
|
|
|
|
},
|
2023-05-17 06:27:58 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'checked first value',
|
|
|
|
render: () => html`
|
|
|
|
<md-radio name="radio" value="One" checked></md-radio>
|
|
|
|
<md-radio name="radio" value="Two"></md-radio>
|
|
|
|
`,
|
|
|
|
assertValue(formData) {
|
|
|
|
expect(formData.get('radio')).toBe('One');
|
2023-10-25 21:58:21 +03:00
|
|
|
},
|
2023-05-17 06:27:58 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'checked second value',
|
|
|
|
render: () => html`
|
|
|
|
<md-radio name="radio" value="One"></md-radio>
|
|
|
|
<md-radio name="radio" value="Two" checked></md-radio>
|
|
|
|
`,
|
|
|
|
assertValue(formData) {
|
|
|
|
expect(formData.get('radio')).toBe('Two');
|
2023-10-25 21:58:21 +03:00
|
|
|
},
|
2023-05-17 06:27:58 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'disabled',
|
|
|
|
render: () => html`
|
|
|
|
<md-radio name="radio" value="One" checked disabled></md-radio>
|
|
|
|
<md-radio name="radio" value="Two" disabled></md-radio>
|
|
|
|
`,
|
|
|
|
assertValue(formData) {
|
|
|
|
expect(formData)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('should not add anything to form when disabled')
|
|
|
|
.toHaveSize(0);
|
|
|
|
},
|
|
|
|
},
|
2023-05-17 06:27:58 +03:00
|
|
|
],
|
|
|
|
resetTests: [
|
|
|
|
{
|
|
|
|
name: 'reset to unchecked',
|
|
|
|
render: () => html`
|
|
|
|
<md-radio name="radio" value="One"></md-radio>
|
|
|
|
<md-radio name="radio" value="Two"></md-radio>
|
|
|
|
`,
|
|
|
|
change(radio) {
|
|
|
|
radio.checked = true;
|
|
|
|
},
|
|
|
|
assertReset(radio) {
|
|
|
|
expect(radio.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('radio.checked after reset')
|
|
|
|
.toBeFalse();
|
|
|
|
},
|
2023-05-17 06:27:58 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'reset to checked',
|
|
|
|
render: () => html`
|
|
|
|
<md-radio name="radio" value="One" checked></md-radio>
|
|
|
|
<md-radio name="radio" value="Two"></md-radio>
|
|
|
|
`,
|
|
|
|
change(radio) {
|
|
|
|
radio.checked = false;
|
|
|
|
},
|
|
|
|
assertReset(radio) {
|
|
|
|
expect(radio.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('radio.checked after reset')
|
|
|
|
.toBeTrue();
|
|
|
|
},
|
2023-05-17 06:27:58 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
restoreTests: [
|
|
|
|
{
|
|
|
|
name: 'restore unchecked',
|
|
|
|
render: () => html`
|
|
|
|
<md-radio name="radio" value="One"></md-radio>
|
|
|
|
<md-radio name="radio" value="Two"></md-radio>
|
|
|
|
`,
|
|
|
|
assertRestored(radio) {
|
|
|
|
expect(radio.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('radio.checked after restore')
|
|
|
|
.toBeFalse();
|
|
|
|
},
|
2023-05-17 06:27:58 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'restore checked',
|
|
|
|
render: () => html`
|
|
|
|
<md-radio name="radio" value="One" checked></md-radio>
|
|
|
|
<md-radio name="radio" value="Two"></md-radio>
|
|
|
|
`,
|
|
|
|
assertRestored(radio) {
|
|
|
|
expect(radio.checked)
|
2023-10-25 21:58:21 +03:00
|
|
|
.withContext('radio.checked after restore')
|
|
|
|
.toBeTrue();
|
|
|
|
},
|
2023-05-17 06:27:58 +03:00
|
|
|
},
|
2023-10-25 21:58:21 +03:00
|
|
|
],
|
2023-05-17 06:27:58 +03:00
|
|
|
});
|
|
|
|
});
|
2022-07-12 23:17:59 +03:00
|
|
|
});
|