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

278 wiersze
11 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 { serialize } from '../../utilities/form';
2023-01-13 20:43:55 +00:00
import sinon from 'sinon';
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;
2022-12-08 19:42:04 +00:00
expect(el.spellcheck).to.be.true;
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
2023-01-10 20:05:44 +00:00
expect(el.checkValidity()).to.be.true;
2022-03-11 16:27:36 +00:00
});
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
2023-01-10 20:05:44 +00:00
expect(el.checkValidity()).to.be.false;
2022-03-11 16:27:36 +00:00
});
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
2023-01-10 20:05:44 +00:00
expect(el.checkValidity()).to.be.false;
2022-03-11 16:27:36 +00:00
});
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;
2023-01-10 20:05:44 +00:00
expect(el.checkValidity()).to.be.false;
});
it('should receive the correct validation attributes ("states") when valid', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea required value="a"></sl-textarea> `);
expect(el.checkValidity()).to.be.true;
expect(el.hasAttribute('data-required')).to.be.true;
expect(el.hasAttribute('data-optional')).to.be.false;
expect(el.hasAttribute('data-invalid')).to.be.false;
expect(el.hasAttribute('data-valid')).to.be.true;
expect(el.hasAttribute('data-user-invalid')).to.be.false;
expect(el.hasAttribute('data-user-valid')).to.be.false;
el.focus();
await sendKeys({ press: 'b' });
await el.updateComplete;
expect(el.checkValidity()).to.be.true;
expect(el.hasAttribute('data-user-invalid')).to.be.false;
expect(el.hasAttribute('data-user-valid')).to.be.true;
});
it('should receive the correct validation attributes ("states") when invalid', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea required></sl-textarea> `);
expect(el.hasAttribute('data-required')).to.be.true;
expect(el.hasAttribute('data-optional')).to.be.false;
expect(el.hasAttribute('data-invalid')).to.be.true;
expect(el.hasAttribute('data-valid')).to.be.false;
expect(el.hasAttribute('data-user-invalid')).to.be.false;
expect(el.hasAttribute('data-user-valid')).to.be.false;
el.focus();
await sendKeys({ press: 'a' });
await sendKeys({ press: 'Backspace' });
await el.updateComplete;
expect(el.hasAttribute('data-user-invalid')).to.be.true;
expect(el.hasAttribute('data-user-valid')).to.be.false;
2022-03-11 16:27:36 +00:00
});
2022-03-08 22:34:17 +00:00
});
2023-01-13 17:34:33 +00:00
describe('when submitting a form', () => {
2022-03-11 16:27:36 +00:00
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');
});
2023-01-13 17:34:33 +00:00
it('should be invalid when setCustomValidity() is called with a non-empty value', async () => {
const textarea = await fixture<HTMLFormElement>(html` <sl-textarea></sl-textarea> `);
textarea.setCustomValidity('Invalid selection');
await textarea.updateComplete;
expect(textarea.checkValidity()).to.be.false;
expect(textarea.hasAttribute('data-invalid')).to.be.true;
expect(textarea.hasAttribute('data-valid')).to.be.false;
expect(textarea.hasAttribute('data-user-invalid')).to.be.false;
expect(textarea.hasAttribute('data-user-valid')).to.be.false;
textarea.focus();
await sendKeys({ type: 'test' });
await textarea.updateComplete;
expect(textarea.hasAttribute('data-user-invalid')).to.be.true;
expect(textarea.hasAttribute('data-user-valid')).to.be.false;
});
it('should be present in form data when using the form attribute and located outside of a <form>', async () => {
const el = await fixture<HTMLFormElement>(html`
<div>
<form id="f">
<sl-button type="submit">Submit</sl-button>
</form>
<sl-textarea form="f" name="a" value="1"></sl-textarea>
</div>
`);
const form = el.querySelector('form')!;
const formData = new FormData(form);
expect(formData.get('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('');
});
});
2022-12-08 19:42:04 +00:00
describe('when using spellcheck', () => {
it('should enable spellcheck when no attribute is present', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea></sl-textarea> `);
const textarea = el.shadowRoot!.querySelector<HTMLTextAreaElement>('textarea')!;
expect(textarea.getAttribute('spellcheck')).to.equal('true');
expect(textarea.spellcheck).to.be.true;
});
it('should enable spellcheck when set to "true"', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea spellcheck="true"></sl-textarea> `);
const textarea = el.shadowRoot!.querySelector<HTMLTextAreaElement>('textarea')!;
expect(textarea.getAttribute('spellcheck')).to.equal('true');
expect(textarea.spellcheck).to.be.true;
});
it('should disable spellcheck when set to "false"', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea spellcheck="false"></sl-textarea> `);
const textarea = el.shadowRoot!.querySelector<HTMLTextAreaElement>('textarea')!;
expect(textarea.getAttribute('spellcheck')).to.equal('false');
expect(textarea.spellcheck).to.be.false;
});
});
2021-06-25 23:02:34 +00:00
});