shoelace/src/components/input/input.test.ts

110 wiersze
4.1 KiB
TypeScript
Czysty Zwykły widok Historia

2022-03-01 15:24:57 +00:00
import { expect, fixture, html, waitUntil } from '@open-wc/testing';
import { sendKeys } from '@web/test-runner-commands';
import sinon from 'sinon';
2022-03-24 11:48:03 +00:00
// eslint-disable-next-line no-restricted-imports
2022-03-08 22:34:17 +00:00
import { serialize } from '../../utilities/form';
2021-06-25 23:02:34 +00:00
import type SlInput from './input';
describe('<sl-input>', () => {
2022-03-08 22:34:17 +00:00
it('should pass accessibility tests', async () => {
const el = await fixture<SlInput>(html` <sl-input label="Name"></sl-input> `);
await expect(el).to.be.accessible();
});
2021-06-25 23:02:34 +00:00
it('should be disabled with the disabled attribute', async () => {
2021-08-10 21:11:07 +00:00
const el = await fixture<SlInput>(html` <sl-input disabled></sl-input> `);
const input = el.shadowRoot!.querySelector<HTMLInputElement>('[part="input"]')!;
2021-06-25 23:02:34 +00:00
expect(input.disabled).to.be.true;
});
2022-03-11 16:27:36 +00:00
describe('value methods', () => {
it('should set the value as a date when using valueAsDate', async () => {
const el = await fixture<SlInput>(html` <sl-input type="date"></sl-input> `);
const today = new Date();
2022-03-02 17:19:59 +00:00
2022-03-11 16:27:36 +00:00
el.valueAsDate = today;
await el.updateComplete;
2022-03-02 17:19:59 +00:00
2022-03-11 16:27:36 +00:00
expect(el.value).to.equal(today.toISOString().split('T')[0]);
});
2022-03-02 17:19:59 +00:00
2022-03-11 16:27:36 +00:00
it('should set the value as a number when using valueAsNumber', async () => {
const el = await fixture<SlInput>(html` <sl-input type="number"></sl-input> `);
const num = 12345;
2022-03-02 17:19:59 +00:00
2022-03-11 16:27:36 +00:00
el.valueAsNumber = num;
await el.updateComplete;
2022-03-02 17:19:59 +00:00
2022-03-11 16:27:36 +00:00
expect(el.value).to.equal(num.toString());
});
2022-03-02 17:19:59 +00:00
});
2022-03-08 22:34:17 +00:00
it('should focus the input when clicking on the label', async () => {
const el = await fixture<SlInput>(html` <sl-input label="Name"></sl-input> `);
2022-03-18 21:33:23 +00:00
const label = el.shadowRoot!.querySelector('[part="form-control-label"]')!;
2022-03-08 22:34:17 +00:00
const submitHandler = sinon.spy();
el.addEventListener('sl-focus', submitHandler);
(label as HTMLLabelElement).click();
await waitUntil(() => submitHandler.calledOnce);
expect(submitHandler).to.have.been.calledOnce;
});
2022-03-11 16:27:36 +00:00
describe('when using constraint validation', () => {
it('should be valid by default', async () => {
const el = await fixture<SlInput>(html` <sl-input></sl-input> `);
expect(el.invalid).to.be.false;
});
it('should be invalid when required and empty', async () => {
const el = await fixture<SlInput>(html` <sl-input required></sl-input> `);
expect(el.reportValidity()).to.be.false;
expect(el.invalid).to.be.true;
});
it('should be invalid when the pattern does not match', async () => {
const el = await fixture<SlInput>(html` <sl-input pattern="^test" value="fail"></sl-input> `);
expect(el.invalid).to.be.true;
expect(el.reportValidity()).to.be.false;
});
it('should be invalid when required and disabled is removed', async () => {
const el = await fixture<SlInput>(html` <sl-input disabled required></sl-input> `);
el.disabled = false;
await el.updateComplete;
expect(el.invalid).to.be.true;
});
2022-03-08 22:34:17 +00:00
});
2022-03-11 16:27:36 +00:00
describe('when serializing', () => {
it('should serialize its name and value with FormData', async () => {
const form = await fixture<HTMLFormElement>(html` <form><sl-input name="a" value="1"></sl-input></form> `);
const formData = new FormData(form);
expect(formData.get('a')).to.equal('1');
});
it('should serialize its name and value with JSON', async () => {
const form = await fixture<HTMLFormElement>(html` <form><sl-input name="a" value="1"></sl-input></form> `);
const json = serialize(form);
expect(json.a).to.equal('1');
});
2022-03-08 22:34:17 +00:00
});
2022-03-11 16:27:36 +00:00
describe('when submitting a form', () => {
it('should submit the form when pressing enter in a form without a submit button', async () => {
const form = await fixture<HTMLFormElement>(html` <form><sl-input></sl-input></form> `);
const input = form.querySelector('sl-input')!;
const submitHandler = sinon.spy((event: SubmitEvent) => event.preventDefault());
2022-03-08 22:34:17 +00:00
2022-03-11 16:27:36 +00:00
form.addEventListener('submit', submitHandler);
input.focus();
await sendKeys({ press: 'Enter' });
await waitUntil(() => submitHandler.calledOnce);
2022-03-08 22:34:17 +00:00
2022-03-11 16:27:36 +00:00
expect(submitHandler).to.have.been.calledOnce;
});
2022-03-08 22:34:17 +00:00
});
2021-06-25 23:02:34 +00:00
});