shoelace/src/components/select/select.test.ts

57 wiersze
2.2 KiB
TypeScript
Czysty Zwykły widok Historia

import { expect, fixture, html, waitUntil, aTimeout } from '@open-wc/testing';
2021-06-15 13:36:45 +00:00
import sinon from 'sinon';
import '../../../dist/shoelace.js';
import type SlSelect from './select';
describe('<sl-select>', () => {
it('should emit sl-change when the value changes', async () => {
2021-08-10 21:11:07 +00:00
const el = await fixture<SlSelect>(html`
2021-06-15 13:36:45 +00:00
<sl-select>
<sl-menu-item value="option-1">Option 1</sl-menu-item>
<sl-menu-item value="option-2">Option 2</sl-menu-item>
<sl-menu-item value="option-3">Option 3</sl-menu-item>
</sl-select>
2021-08-10 21:11:07 +00:00
`);
2021-06-15 13:36:45 +00:00
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`
<sl-select>
<sl-menu-item value="option-1">Option 1</sl-menu-item>
<sl-menu-item value="option-2">Option 2</sl-menu-item>
<sl-menu-item value="option-3">Option 3</sl-menu-item>
</sl-select>
`)) as SlSelect;
2021-09-24 12:48:01 +00:00
const control = el.shadowRoot.querySelector('.select__control') as HTMLSelectElement;
control.focus();
const rKeyEvent = new KeyboardEvent('keydown', { key: 'r' });
2021-09-24 12:48:01 +00:00
control.dispatchEvent(rKeyEvent);
await aTimeout(100);
2021-09-24 12:48:01 +00:00
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`
<sl-select>
<sl-menu-item value="option-1">Option 1</sl-menu-item>
<sl-menu-item value="option-2">Option 2</sl-menu-item>
<sl-menu-item value="option-3">Option 3</sl-menu-item>
</sl-select>
`)) as SlSelect;
2021-09-24 12:48:01 +00:00
const control = el.shadowRoot.querySelector('.select__control') as HTMLSelectElement;
control.focus();
const rKeyEvent = new KeyboardEvent('keydown', { key: 'r', ctrlKey: true });
2021-09-24 12:48:01 +00:00
control.dispatchEvent(rKeyEvent);
await aTimeout(100);
2021-09-24 12:48:01 +00:00
expect(control.getAttribute('aria-expanded')).to.equal('false');
});
2021-06-15 13:36:45 +00:00
});