import './select-option/select-option.element.js'; import './select.element.js'; import { assert, fixture, html } from '@open-wc/testing'; import { userEvent } from '@testing-library/user-event'; import { USASelectElement } from './select.element.js'; describe('usa-select', () => { it('should be accessible', async () => { const el = await fixture(html` Hello World First Second Third `); return assert.isAccessible(el); }); it('should create local select options', async () => { const form = await fixture(html`
Hello World First Second Third
`); const nativeInputs = form.querySelector('usa-select')?.shadowRoot?.querySelectorAll('option'); assert.deepEqual( Array.from(nativeInputs ?? []).map((input) => input.value), ['first', 'second', 'third'], ); }); it('should remove select options when options are removed', async () => { const form = await fixture(html`
Hello World First Second Third
`); const options = form.querySelectorAll('usa-select-option'); options[1].remove(); const nativeInputs = form.querySelector('usa-select')?.shadowRoot?.querySelectorAll('option'); assert.deepEqual( Array.from(nativeInputs ?? []).map((input) => input.value), ['first', 'third'], ); }); it('should submit form with default values', async () => { const form = await fixture(html`
Hello World First Second Third
`); const value = new FormData(form); assert.equal(value.get('example'), 'second'); }); it('should update form value as select value changed', async () => { const form = await fixture(html`
Hello World First Second Third
`); const select = form.querySelector('usa-select'); const nativeSelect = select?.shadowRoot?.querySelector('select'); if (nativeSelect) { await userEvent.selectOptions(nativeSelect, 'third'); } const value = new FormData(form); assert.equal(value.get('example'), 'third'); }); it('should not submit when not valid', async () => { const form = await fixture(html`
Hello World -Select One - First Second Third
`); assert.equal(form.checkValidity(), false); }); it('should be disabled when disabled attribute is set', async () => { const select = await fixture(html` Hello World First Second Third `); const nativeSelect = select.shadowRoot?.querySelector('select'); assert.isTrue(nativeSelect?.disabled); }); });