shoelace/src/components/textarea/textarea.test.ts

181 wiersze
6.5 KiB
TypeScript
Czysty Zwykły widok Historia

import { expect, fixture, html, oneEvent, waitUntil } from '@open-wc/testing';
2022-12-07 22:40:46 +00:00
import { sendKeys } from '@web/test-runner-commands';
2022-03-08 22:34:17 +00:00
import sinon from 'sinon';
import { serialize } from '../../utilities/form';
2021-06-25 23:02:34 +00:00
import type SlTextarea from './textarea';
describe('<sl-textarea>', () => {
2022-03-08 22:34:17 +00:00
it('should pass accessibility tests', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea label="Name"></sl-textarea> `);
await expect(el).to.be.accessible();
});
it('default properties', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea></sl-textarea> `);
expect(el.size).to.equal('medium');
expect(el.name).to.equal('');
expect(el.value).to.equal('');
expect(el.defaultValue).to.equal('');
expect(el.title).to.equal('');
expect(el.filled).to.be.false;
expect(el.label).to.equal('');
expect(el.helpText).to.equal('');
expect(el.placeholder).to.equal('');
expect(el.rows).to.equal(4);
expect(el.resize).to.equal('vertical');
expect(el.disabled).to.be.false;
expect(el.readonly).to.be.false;
expect(el.minlength).to.be.undefined;
expect(el.maxlength).to.be.undefined;
expect(el.required).to.be.false;
expect(el.autocapitalize).to.be.undefined;
expect(el.autocorrect).to.be.undefined;
expect(el.autocomplete).to.be.undefined;
expect(el.autofocus).to.be.undefined;
expect(el.enterkeyhint).to.be.undefined;
expect(el.spellcheck).to.be.undefined;
expect(el.inputmode).to.be.undefined;
});
2022-11-30 20:43:36 +00:00
it('should have title if title attribute is set', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea title="Test"></sl-textarea> `);
const textarea = el.shadowRoot!.querySelector('textarea')!;
expect(textarea.title).to.equal('Test');
});
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<SlTextarea>(html` <sl-textarea disabled></sl-textarea> `);
2022-11-17 14:35:44 +00:00
const textarea = el.shadowRoot!.querySelector<HTMLTextAreaElement>('[part~="textarea"]')!;
2021-06-25 23:02:34 +00:00
expect(textarea.disabled).to.be.true;
});
2022-03-08 22:34:17 +00:00
it('should focus the textarea when clicking on the label', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea label="Name"></sl-textarea> `);
2022-11-17 14:35:44 +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-12-07 22:40:46 +00:00
describe('when the value changes', () => {
it('should emit sl-change and sl-input when the user types in the textarea', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea></sl-textarea> `);
const inputHandler = sinon.spy();
const changeHandler = sinon.spy();
el.addEventListener('sl-input', inputHandler);
el.addEventListener('sl-change', changeHandler);
el.focus();
await sendKeys({ type: 'abc' });
el.blur();
await el.updateComplete;
expect(changeHandler).to.have.been.calledOnce;
expect(inputHandler).to.have.been.calledThrice;
});
it('should not emit sl-change or sl-input when the value is set programmatically', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea></sl-textarea> `);
el.addEventListener('sl-change', () => expect.fail('sl-change should not be emitted'));
el.addEventListener('sl-input', () => expect.fail('sl-input should not be emitted'));
el.value = 'abc';
await el.updateComplete;
});
it('should not emit sl-change or sl-input when calling setRangeText()', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea value="hi there"></sl-textarea> `);
el.addEventListener('sl-change', () => expect.fail('sl-change should not be emitted'));
el.addEventListener('sl-input', () => expect.fail('sl-input should not be emitted'));
el.focus();
el.setSelectionRange(0, 2);
el.setRangeText('hello');
await el.updateComplete;
});
});
2022-03-11 16:27:36 +00:00
describe('when using constraint validation', () => {
it('should be valid by default', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea></sl-textarea> `);
2021-06-25 23:02:34 +00:00
2022-03-11 16:27:36 +00:00
expect(el.invalid).to.be.false;
});
2021-06-25 23:02:34 +00:00
2022-03-11 16:27:36 +00:00
it('should be invalid when required and empty', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea required></sl-textarea> `);
2021-06-25 23:02:34 +00:00
2022-03-11 16:27:36 +00:00
expect(el.invalid).to.be.true;
});
2021-06-25 23:02:34 +00:00
2022-03-11 16:27:36 +00:00
it('should be invalid when required and after removing disabled ', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea disabled required></sl-textarea> `);
2021-06-25 23:02:34 +00:00
2022-03-11 16:27:36 +00:00
el.disabled = false;
await el.updateComplete;
2021-06-25 23:02:34 +00:00
2022-03-11 16:27:36 +00:00
expect(el.invalid).to.be.true;
});
2022-03-08 22:34:17 +00:00
2022-03-11 16:27:36 +00:00
it('should be invalid when required and disabled is removed', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea disabled required></sl-textarea> `);
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-textarea name="a" value="1"></sl-textarea></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-textarea name="a" value="1"></sl-textarea></form> `);
const json = serialize(form);
expect(json.a).to.equal('1');
});
2022-03-08 22:34:17 +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-textarea name="a" value="test"></sl-textarea>
<sl-button type="reset">Reset</sl-button>
</form>
`);
const button = form.querySelector('sl-button')!;
const textarea = form.querySelector('sl-textarea')!;
textarea.value = '1234';
await textarea.updateComplete;
setTimeout(() => button.click());
await oneEvent(form, 'reset');
await textarea.updateComplete;
expect(textarea.value).to.equal('test');
textarea.defaultValue = '';
setTimeout(() => button.click());
await oneEvent(form, 'reset');
await textarea.updateComplete;
expect(textarea.value).to.equal('');
});
});
2021-06-25 23:02:34 +00:00
});