mirror of
https://github.com/shoelace-style/webawesome.git
synced 2026-01-12 12:09:26 +00:00
168 lines
5.6 KiB
TypeScript
168 lines
5.6 KiB
TypeScript
import { expect, fixture, html, oneEvent, waitUntil } from '@open-wc/testing';
|
|
import { sendKeys } from '@web/test-runner-commands';
|
|
import sinon from 'sinon';
|
|
import type SlSwitch from './switch';
|
|
|
|
describe('<sl-switch>', () => {
|
|
it('should pass accessibility tests', async () => {
|
|
const el = await fixture<SlSwitch>(html` <sl-switch>Switch</sl-switch> `);
|
|
await expect(el).to.be.accessible();
|
|
});
|
|
|
|
it('default properties', async () => {
|
|
const el = await fixture<SlSwitch>(html` <sl-switch></sl-switch> `);
|
|
|
|
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.defaultChecked).to.be.false;
|
|
});
|
|
|
|
it('should have title if title attribute is set', async () => {
|
|
const el = await fixture<SlSwitch>(html` <sl-switch title="Test"></sl-switch> `);
|
|
const input = el.shadowRoot!.querySelector('input')!;
|
|
|
|
expect(input.title).to.equal('Test');
|
|
});
|
|
|
|
it('should be disabled with the disabled attribute', async () => {
|
|
const el = await fixture<SlSwitch>(html` <sl-switch disabled></sl-switch> `);
|
|
const input = el.shadowRoot!.querySelector<HTMLInputElement>('input')!;
|
|
|
|
expect(input.disabled).to.be.true;
|
|
});
|
|
|
|
it('should be valid by default', async () => {
|
|
const el = await fixture<SlSwitch>(html` <sl-switch></sl-switch> `);
|
|
|
|
expect(el.invalid).to.be.false;
|
|
});
|
|
|
|
it('should emit sl-change and sl-input when clicked', async () => {
|
|
const el = await fixture<SlSwitch>(html` <sl-switch></sl-switch> `);
|
|
const changeHandler = sinon.spy();
|
|
const inputHandler = sinon.spy();
|
|
|
|
el.addEventListener('sl-change', changeHandler);
|
|
el.addEventListener('sl-input', inputHandler);
|
|
el.click();
|
|
await el.updateComplete;
|
|
|
|
expect(changeHandler).to.have.been.calledOnce;
|
|
expect(inputHandler).to.have.been.calledOnce;
|
|
expect(el.checked).to.be.true;
|
|
});
|
|
|
|
it('should emit sl-change when toggled with spacebar', async () => {
|
|
const el = await fixture<SlSwitch>(html` <sl-switch></sl-switch> `);
|
|
const changeHandler = sinon.spy();
|
|
const inputHandler = sinon.spy();
|
|
|
|
el.addEventListener('sl-change', changeHandler);
|
|
el.addEventListener('sl-input', inputHandler);
|
|
el.focus();
|
|
await sendKeys({ press: ' ' });
|
|
|
|
expect(changeHandler).to.have.been.calledOnce;
|
|
expect(inputHandler).to.have.been.calledOnce;
|
|
expect(el.checked).to.be.true;
|
|
});
|
|
|
|
it('should emit sl-change and sl-input when toggled with the right arrow', async () => {
|
|
const el = await fixture<SlSwitch>(html` <sl-switch></sl-switch> `);
|
|
const changeHandler = sinon.spy();
|
|
const inputHandler = sinon.spy();
|
|
|
|
el.addEventListener('sl-change', changeHandler);
|
|
el.addEventListener('sl-input', inputHandler);
|
|
el.focus();
|
|
await sendKeys({ press: 'ArrowRight' });
|
|
await el.updateComplete;
|
|
|
|
expect(changeHandler).to.have.been.calledOnce;
|
|
expect(inputHandler).to.have.been.calledOnce;
|
|
expect(el.checked).to.be.true;
|
|
});
|
|
|
|
it('should emit sl-change and sl-input when toggled with the left arrow', async () => {
|
|
const el = await fixture<SlSwitch>(html` <sl-switch checked></sl-switch> `);
|
|
const changeHandler = sinon.spy();
|
|
const inputHandler = sinon.spy();
|
|
|
|
el.addEventListener('sl-change', changeHandler);
|
|
el.addEventListener('sl-input', inputHandler);
|
|
el.focus();
|
|
await sendKeys({ press: 'ArrowLeft' });
|
|
await el.updateComplete;
|
|
|
|
expect(changeHandler).to.have.been.calledOnce;
|
|
expect(inputHandler).to.have.been.calledOnce;
|
|
expect(el.checked).to.be.false;
|
|
});
|
|
|
|
it('should not emit sl-change or sl-input when checked is set by javascript', async () => {
|
|
const el = await fixture<SlSwitch>(html` <sl-switch></sl-switch> `);
|
|
el.addEventListener('sl-change', () => expect.fail('sl-change incorrectly emitted'));
|
|
el.addEventListener('sl-input', () => expect.fail('sl-change incorrectly emitted'));
|
|
el.checked = true;
|
|
await el.updateComplete;
|
|
el.checked = false;
|
|
await el.updateComplete;
|
|
});
|
|
|
|
it('should submit "on" when no value is provided', async () => {
|
|
const form = await fixture<HTMLFormElement>(html`
|
|
<form>
|
|
<sl-switch name="a" checked></sl-switch>
|
|
<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');
|
|
});
|
|
|
|
describe('when resetting a form', () => {
|
|
it('should reset the element to its initial value', async () => {
|
|
const form = await fixture<HTMLFormElement>(html`
|
|
<form>
|
|
<sl-switch name="a" value="1" checked></sl-switch>
|
|
<sl-button type="reset">Reset</sl-button>
|
|
</form>
|
|
`);
|
|
const button = form.querySelector('sl-button')!;
|
|
const switchEl = form.querySelector('sl-switch')!;
|
|
switchEl.checked = false;
|
|
|
|
await switchEl.updateComplete;
|
|
setTimeout(() => button.click());
|
|
|
|
await oneEvent(form, 'reset');
|
|
await switchEl.updateComplete;
|
|
|
|
expect(switchEl.checked).to.true;
|
|
|
|
switchEl.defaultChecked = false;
|
|
|
|
setTimeout(() => button.click());
|
|
await oneEvent(form, 'reset');
|
|
await switchEl.updateComplete;
|
|
|
|
expect(switchEl.checked).to.false;
|
|
});
|
|
});
|
|
});
|