shoelace/src/components/checkbox/checkbox.test.ts

246 wiersze
8.1 KiB
TypeScript
Czysty Zwykły widok Historia

2022-03-24 12:11:49 +00:00
import { aTimeout, expect, fixture, html, oneEvent, waitUntil } from '@open-wc/testing';
2021-08-10 21:11:07 +00:00
import { sendKeys } from '@web/test-runner-commands';
2022-03-24 12:11:49 +00:00
import sinon from 'sinon';
import type SlCheckbox from './checkbox';
describe('<sl-checkbox>', () => {
it('should pass accessibility tests', async () => {
const el = await fixture<SlCheckbox>(html` <sl-checkbox>Checkbox</sl-checkbox> `);
await expect(el).to.be.accessible();
});
it('default properties', async () => {
const el = await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `);
expect(el.name).to.equal('');
expect(el.value).to.be.undefined;
expect(el.title).to.equal('');
expect(el.disabled).to.be.false;
expect(el.required).to.be.false;
expect(el.checked).to.be.false;
expect(el.indeterminate).to.be.false;
expect(el.defaultChecked).to.be.false;
});
2022-11-30 20:43:43 +00:00
it('should have title if title attribute is set', async () => {
const el = await fixture<SlCheckbox>(html` <sl-checkbox title="Test"></sl-checkbox> `);
const input = el.shadowRoot!.querySelector('input')!;
expect(input.title).to.equal('Test');
});
it('should be disabled with the disabled attribute', async () => {
2021-08-10 21:11:07 +00:00
const el = await fixture<SlCheckbox>(html` <sl-checkbox disabled></sl-checkbox> `);
const checkbox = el.shadowRoot!.querySelector('input')!;
expect(checkbox.disabled).to.be.true;
});
2022-04-07 11:24:48 +00:00
it('should be disabled when disabled property is set', async () => {
const el = await fixture<SlCheckbox>(html`<sl-checkbox></sl-checkbox>`);
const checkbox = el.shadowRoot!.querySelector('input')!;
el.disabled = true;
await el.updateComplete;
expect(checkbox.disabled).to.be.true;
});
it('should be valid by default', async () => {
2021-08-10 21:11:07 +00:00
const el = await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `);
expect(el.invalid).to.be.false;
});
it('should fire sl-change when clicked', async () => {
2021-08-10 21:11:07 +00:00
const el = await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `);
setTimeout(() => el.shadowRoot!.querySelector('input')!.click());
2022-05-24 12:12:13 +00:00
const event = (await oneEvent(el, 'sl-change')) as CustomEvent;
expect(event.target).to.equal(el);
expect(el.checked).to.be.true;
});
it('should fire sl-change when toggled via keyboard', async () => {
2021-08-10 21:11:07 +00:00
const el = await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `);
const input = el.shadowRoot!.querySelector('input')!;
input.focus();
setTimeout(() => sendKeys({ press: ' ' }));
2022-05-24 12:12:13 +00:00
const event = (await oneEvent(el, 'sl-change')) as CustomEvent;
expect(event.target).to.equal(el);
expect(el.checked).to.be.true;
});
it('should not fire sl-change when checked is set by javascript', async () => {
2021-08-10 21:11:07 +00:00
const el = await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `);
el.addEventListener('sl-change', () => expect.fail('event fired'));
el.checked = true;
await el.updateComplete;
el.checked = false;
await el.updateComplete;
});
2022-03-24 12:11:49 +00:00
describe('when submitting a form', () => {
2022-05-11 14:48:06 +00:00
it('should submit the correct value when a value is provided', async () => {
2022-03-24 12:11:49 +00:00
const form = await fixture<HTMLFormElement>(html`
<form>
<sl-checkbox name="a" value="1" checked></sl-checkbox>
<sl-button type="submit">Submit</sl-button>
</form>
`);
const button = form.querySelector('sl-button')!;
const submitHandler = sinon.spy((event: SubmitEvent) => {
formData = new FormData(form);
event.preventDefault();
});
let formData: FormData;
form.addEventListener('submit', submitHandler);
button.click();
await waitUntil(() => submitHandler.calledOnce);
expect(formData!.get('a')).to.equal('1');
});
2022-05-11 14:48:06 +00:00
it('should submit "on" when no value is provided', async () => {
const form = await fixture<HTMLFormElement>(html`
<form>
<sl-checkbox name="a" checked></sl-checkbox>
<sl-button type="submit">Submit</sl-button>
</form>
`);
const button = form.querySelector('sl-button')!;
const submitHandler = sinon.spy((event: SubmitEvent) => {
formData = new FormData(form);
event.preventDefault();
});
let formData: FormData;
form.addEventListener('submit', submitHandler);
button.click();
await waitUntil(() => submitHandler.calledOnce);
expect(formData!.get('a')).to.equal('on');
});
2022-03-24 12:11:49 +00:00
it('should show a constraint validation error when setCustomValidity() is called', async () => {
const form = await fixture<HTMLFormElement>(html`
<form>
<sl-checkbox name="a" value="1" checked></sl-checkbox>
<sl-button type="submit">Submit</sl-button>
</form>
`);
const button = form.querySelector('sl-button')!;
const checkbox = form.querySelector('sl-checkbox')!;
const submitHandler = sinon.spy((event: SubmitEvent) => event.preventDefault());
// Submitting the form after setting custom validity should not trigger the handler
checkbox.setCustomValidity('Invalid selection');
form.addEventListener('submit', submitHandler);
button.click();
await aTimeout(100);
expect(submitHandler).to.not.have.been.called;
});
});
2022-04-07 11:24:48 +00:00
describe('when resetting a form', () => {
it('should reset the element to its initial value', async () => {
const form = await fixture<HTMLFormElement>(html`
<form>
<sl-checkbox name="a" value="1" checked></sl-checkbox>
<sl-button type="reset">Reset</sl-button>
</form>
`);
const button = form.querySelector('sl-button')!;
const checkbox = form.querySelector('sl-checkbox')!;
checkbox.checked = false;
await checkbox.updateComplete;
setTimeout(() => button.click());
await oneEvent(form, 'reset');
await checkbox.updateComplete;
expect(checkbox.checked).to.true;
checkbox.defaultChecked = false;
setTimeout(() => button.click());
await oneEvent(form, 'reset');
await checkbox.updateComplete;
expect(checkbox.checked).to.false;
});
});
2022-04-07 11:24:48 +00:00
describe('click', () => {
it('should click the inner input', async () => {
const el = await fixture<SlCheckbox>(html`<sl-checkbox></sl-checkbox>`);
const checkbox = el.shadowRoot!.querySelector('input')!;
const clickSpy = sinon.spy();
checkbox.addEventListener('click', clickSpy, { once: true });
el.click();
await el.updateComplete;
expect(clickSpy.called).to.equal(true);
expect(el.checked).to.equal(true);
});
});
describe('focus', () => {
it('should focus the inner input', async () => {
const el = await fixture<SlCheckbox>(html`<sl-checkbox></sl-checkbox>`);
const checkbox = el.shadowRoot!.querySelector('input')!;
const focusSpy = sinon.spy();
checkbox.addEventListener('focus', focusSpy, { once: true });
el.focus();
await el.updateComplete;
expect(focusSpy.called).to.equal(true);
expect(el.shadowRoot!.activeElement).to.equal(checkbox);
});
});
describe('blur', () => {
it('should blur the inner input', async () => {
const el = await fixture<SlCheckbox>(html`<sl-checkbox></sl-checkbox>`);
const checkbox = el.shadowRoot!.querySelector('input')!;
const blurSpy = sinon.spy();
checkbox.addEventListener('blur', blurSpy, { once: true });
el.focus();
await el.updateComplete;
el.blur();
await el.updateComplete;
expect(blurSpy.called).to.equal(true);
expect(el.shadowRoot!.activeElement).to.equal(null);
});
});
describe('indeterminate', () => {
it('should render indeterminate icon until checked', async () => {
const el = await fixture<SlCheckbox>(html`<sl-checkbox indeterminate></sl-checkbox>`);
2022-11-17 14:35:44 +00:00
let indeterminateIcon = el.shadowRoot!.querySelector('[part~="indeterminate-icon"]')!;
2022-04-07 11:24:48 +00:00
expect(indeterminateIcon).not.to.be.null;
el.click();
await el.updateComplete;
2022-11-17 14:35:44 +00:00
indeterminateIcon = el.shadowRoot!.querySelector('[part~="indeterminate-icon"]')!;
2022-04-07 11:24:48 +00:00
expect(indeterminateIcon).to.be.null;
});
});
});