import { expect, fixture, html, waitUntil } from '@open-wc/testing';
import { sendKeys } from '@web/test-runner-commands';
import sinon from 'sinon';
// eslint-disable-next-line no-restricted-imports
import { serialize } from '../../utilities/form';
import type SlInput from './input';
describe('', () => {
it('should pass accessibility tests', async () => {
const el = await fixture(html` `);
await expect(el).to.be.accessible();
});
it('should be disabled with the disabled attribute', async () => {
const el = await fixture(html` `);
const input = el.shadowRoot!.querySelector('[part="input"]')!;
expect(input.disabled).to.be.true;
});
describe('value methods', () => {
it('should set the value as a date when using valueAsDate', async () => {
const el = await fixture(html` `);
const today = new Date();
el.valueAsDate = today;
await el.updateComplete;
expect(el.value).to.equal(today.toISOString().split('T')[0]);
});
it('should set the value as a number when using valueAsNumber', async () => {
const el = await fixture(html` `);
const num = 12345;
el.valueAsNumber = num;
await el.updateComplete;
expect(el.value).to.equal(num.toString());
});
});
it('should focus the input when clicking on the label', async () => {
const el = await fixture(html` `);
const label = el.shadowRoot!.querySelector('[part="form-control-label"]')!;
const submitHandler = sinon.spy();
el.addEventListener('sl-focus', submitHandler);
(label as HTMLLabelElement).click();
await waitUntil(() => submitHandler.calledOnce);
expect(submitHandler).to.have.been.calledOnce;
});
describe('when using constraint validation', () => {
it('should be valid by default', async () => {
const el = await fixture(html` `);
expect(el.invalid).to.be.false;
});
it('should be invalid when required and empty', async () => {
const el = await fixture(html` `);
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(html` `);
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(html` `);
el.disabled = false;
await el.updateComplete;
expect(el.invalid).to.be.true;
});
});
describe('when serializing', () => {
it('should serialize its name and value with FormData', async () => {
const form = await fixture(html` `);
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(html` `);
const json = serialize(form);
expect(json.a).to.equal('1');
});
});
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(html` `);
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;
});
});
});