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

48 wiersze
1.6 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';
2021-06-25 23:02:34 +00:00
import type SlInput from './input';
describe('<sl-input>', () => {
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;
});
it('should be valid by default', async () => {
2021-08-10 21:11:07 +00:00
const el = await fixture<SlInput>(html` <sl-input></sl-input> `);
2021-06-25 23:02:34 +00:00
expect(el.invalid).to.be.false;
});
it('should be invalid when required and empty', async () => {
2021-08-10 21:11:07 +00:00
const el = await fixture<SlInput>(html` <sl-input required></sl-input> `);
2021-06-25 23:02:34 +00:00
expect(el.invalid).to.be.true;
});
2022-03-01 15:24:57 +00:00
it('should be invalid when required and after removing disabled', async () => {
2021-08-10 21:11:07 +00:00
const el = await fixture<SlInput>(html` <sl-input disabled required></sl-input> `);
2021-06-25 23:02:34 +00:00
el.disabled = false;
await el.updateComplete;
expect(el.invalid).to.be.true;
});
2022-03-01 15:24:57 +00:00
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());
form.addEventListener('submit', submitHandler);
input.focus();
await sendKeys({ press: 'Enter' });
await waitUntil(() => submitHandler.calledOnce);
expect(submitHandler).to.have.been.calledOnce;
});
2021-06-25 23:02:34 +00:00
});