import { expect, fixture, html, waitUntil } from '@open-wc/testing';
import { sendKeys } from '@web/test-runner-commands';
import sinon from 'sinon';
import type SlSelect from './select';
describe('', () => {
it('should pass accessibility tests', async () => {
const el = await fixture(html`
Option 1
Option 2
Option 3
`);
await expect(el).to.be.accessible();
});
it('should be disabled with the disabled attribute', async () => {
const el = await fixture(html`
Option 1
Option 2
Option 3
`);
expect(el.dropdown.disabled).to.be.true;
expect(el.control.tabIndex).to.equal(-1);
});
it('should focus the select when clicking on the label', async () => {
const el = await fixture(html`
Option 1
Option 2
Option 3
`);
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;
});
it('should emit sl-change when the value changes', async () => {
const el = await fixture(html`
Option 1
Option 2
Option 3
`);
const changeHandler = sinon.spy();
el.addEventListener('sl-change', changeHandler);
el.value = 'option-2';
await waitUntil(() => changeHandler.calledOnce);
expect(changeHandler).to.have.been.calledOnce;
});
it('should open the menu when any letter key is pressed with sl-select is on focus', async () => {
const el = await fixture(html`
Option 1
Option 2
Option 3
`);
const control = el.shadowRoot!.querySelector('.select__control')!;
control.focus();
await sendKeys({ press: 'r' });
await el.updateComplete;
expect(control.getAttribute('aria-expanded')).to.equal('true');
});
it('should not open the menu when ctrl + R is pressed with sl-select is on focus', async () => {
const el = await fixture(html`
Option 1
Option 2
Option 3
`);
const control = el.shadowRoot!.querySelector('.select__control')!;
control.focus();
await sendKeys({ down: 'Control' });
await sendKeys({ press: 'r' });
await sendKeys({ up: 'Control' });
await el.updateComplete;
expect(control.getAttribute('aria-expanded')).to.equal('false');
});
it('should focus on the custom control when constraint validation occurs', async () => {
const el = await fixture(html`
`);
const select = el.querySelector('sl-select')!;
el.requestSubmit();
expect(select.shadowRoot!.activeElement).to.equal(select.control);
});
});