2021-08-10 12:49:18 +00:00
|
|
|
import { expect, fixture, html, oneEvent } from '@open-wc/testing';
|
2021-08-10 21:11:07 +00:00
|
|
|
import { sendKeys } from '@web/test-runner-commands';
|
2021-08-10 12:49:18 +00:00
|
|
|
|
|
|
|
import '../../../dist/shoelace.js';
|
2021-08-10 15:21:56 +00:00
|
|
|
import type SlCheckbox from './checkbox';
|
2021-08-10 12:49:18 +00:00
|
|
|
|
|
|
|
describe('<sl-checkbox>', () => {
|
|
|
|
it('should be disabled with the disabled attribute', async () => {
|
2021-08-10 21:11:07 +00:00
|
|
|
const el = await fixture<SlCheckbox>(html` <sl-checkbox disabled></sl-checkbox> `);
|
2021-08-10 12:49:18 +00:00
|
|
|
const checkbox = el.shadowRoot?.querySelector('input');
|
|
|
|
|
|
|
|
expect(checkbox.disabled).to.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be valid by default', async () => {
|
2021-08-10 21:11:07 +00:00
|
|
|
const el = await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `);
|
2021-08-10 12:49:18 +00:00
|
|
|
|
|
|
|
expect(el.invalid).to.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fire sl-change when clicked', async () => {
|
2021-08-10 21:11:07 +00:00
|
|
|
const el = await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `);
|
2021-08-10 12:49:18 +00:00
|
|
|
setTimeout(() => el.shadowRoot?.querySelector('input').click());
|
2021-08-10 21:11:07 +00:00
|
|
|
const event = await oneEvent(el, 'sl-change');
|
2021-08-10 12:49:18 +00:00
|
|
|
expect(event.target).to.equal(el);
|
|
|
|
expect(el.checked).to.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fire sl-change when toggled via keyboard', async () => {
|
2021-08-10 21:11:07 +00:00
|
|
|
const el = await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `);
|
2021-08-10 12:49:18 +00:00
|
|
|
const input = el.shadowRoot?.querySelector('input');
|
|
|
|
input.focus();
|
|
|
|
setTimeout(() => sendKeys({ press: ' ' }));
|
2021-08-10 21:11:07 +00:00
|
|
|
const event = await oneEvent(el, 'sl-change');
|
2021-08-10 12:49:18 +00:00
|
|
|
expect(event.target).to.equal(el);
|
|
|
|
expect(el.checked).to.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not fire sl-change when checked is set by javascript', async () => {
|
2021-08-10 21:11:07 +00:00
|
|
|
const el = await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `);
|
|
|
|
el.addEventListener('sl-change', () => expect.fail('event fired'));
|
2021-08-10 12:49:18 +00:00
|
|
|
el.checked = true;
|
|
|
|
await el.updateComplete;
|
|
|
|
el.checked = false;
|
|
|
|
await el.updateComplete;
|
|
|
|
});
|
|
|
|
});
|