diff --git a/src/components/alert/alert.test.ts b/src/components/alert/alert.test.ts index 409ddc2f..47934440 100644 --- a/src/components/alert/alert.test.ts +++ b/src/components/alert/alert.test.ts @@ -6,21 +6,21 @@ import type SlAlert from './alert'; describe('', () => { it('should be visible with the open attribute', async () => { - const el = await fixture(html` I am an alert `); + const el = await fixture(html` I am an alert `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; expect(base.hidden).to.be.false; }); it('should not be visible without the open attribute', async () => { - const el = await fixture(html` I am an alert `); + const el = await fixture(html` I am an alert `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; expect(base.hidden).to.be.true; }); it('should emit sl-show and sl-after-show when calling show()', async () => { - const el = (await fixture(html` I am an alert `)) as SlAlert; + const el = await fixture(html` I am an alert `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const showHandler = sinon.spy(); const afterShowHandler = sinon.spy(); @@ -38,7 +38,7 @@ describe('', () => { }); it('should emit sl-hide and sl-after-hide when calling hide()', async () => { - const el = (await fixture(html` I am an alert `)) as SlAlert; + const el = await fixture(html` I am an alert `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const hideHandler = sinon.spy(); const afterHideHandler = sinon.spy(); @@ -56,7 +56,7 @@ describe('', () => { }); it('should emit sl-show and sl-after-show when setting open = true', async () => { - const el = (await fixture(html` I am an alert `)) as SlAlert; + const el = await fixture(html` I am an alert `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const showHandler = sinon.spy(); const afterShowHandler = sinon.spy(); @@ -74,7 +74,7 @@ describe('', () => { }); it('should emit sl-hide and sl-after-hide when setting open = false', async () => { - const el = (await fixture(html` I am an alert `)) as SlAlert; + const el = await fixture(html` I am an alert `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const hideHandler = sinon.spy(); const afterHideHandler = sinon.spy(); diff --git a/src/components/checkbox/checkbox.test.ts b/src/components/checkbox/checkbox.test.ts new file mode 100644 index 00000000..85d339ce --- /dev/null +++ b/src/components/checkbox/checkbox.test.ts @@ -0,0 +1,47 @@ +import { expect, fixture, html, oneEvent } from '@open-wc/testing'; +import { sendKeys } from '@web/test-runner-commands'; + +import '../../../dist/shoelace.js'; +import type SlCheckbox from './checkbox'; + +describe('', () => { + it('should be disabled with the disabled attribute', async () => { + const el = await fixture(html` `); + const checkbox = el.shadowRoot?.querySelector('input'); + + expect(checkbox.disabled).to.be.true; + }); + + it('should be valid by default', async () => { + const el = await fixture(html` `); + + expect(el.invalid).to.be.false; + }); + + it('should fire sl-change when clicked', async () => { + const el = await fixture(html` `); + setTimeout(() => el.shadowRoot?.querySelector('input').click()); + const event = await oneEvent(el, 'sl-change'); + expect(event.target).to.equal(el); + expect(el.checked).to.be.true; + }); + + it('should fire sl-change when toggled via keyboard', async () => { + const el = await fixture(html` `); + const input = el.shadowRoot?.querySelector('input'); + input.focus(); + setTimeout(() => sendKeys({ press: ' ' })); + const event = await oneEvent(el, 'sl-change'); + 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 () => { + const el = await fixture(html` `); + el.addEventListener('sl-change', () => expect.fail('event fired')); + el.checked = true; + await el.updateComplete; + el.checked = false; + await el.updateComplete; + }); +}); diff --git a/src/components/checkbox/checkbox.ts b/src/components/checkbox/checkbox.ts index 70694f6b..44e5ea71 100644 --- a/src/components/checkbox/checkbox.ts +++ b/src/components/checkbox/checkbox.ts @@ -90,6 +90,7 @@ export default class SlCheckbox extends LitElement { handleClick() { this.checked = !this.checked; this.indeterminate = false; + emit(this, 'sl-change'); } handleBlur() { @@ -121,7 +122,6 @@ export default class SlCheckbox extends LitElement { @watch('indeterminate', { waitUntilFirstUpdate: true }) handleStateChange() { this.invalid = !this.input.checkValidity(); - emit(this, 'sl-change'); } render() { diff --git a/src/components/color-picker/color-picker.test.ts b/src/components/color-picker/color-picker.test.ts index ef67aff5..0c6e13b7 100644 --- a/src/components/color-picker/color-picker.test.ts +++ b/src/components/color-picker/color-picker.test.ts @@ -6,7 +6,7 @@ import type SlColorPicker from './color-picker'; describe('', () => { it('should emit change and show correct color when the value changes', async () => { - const el = (await fixture(html` `)) as SlColorPicker; + const el = await fixture(html` `); const trigger = el.shadowRoot.querySelector('[part="trigger"]') as HTMLElement; const changeHandler = sinon.spy(); const color = 'rgb(255, 204, 0)'; @@ -21,21 +21,21 @@ describe('', () => { }); it('should render in a dropdown', async () => { - const el = (await fixture(html` `)) as SlColorPicker; + const el = await fixture(html` `); const dropdown = el.shadowRoot.querySelector('sl-dropdown'); expect(dropdown).to.exist; }); it('should not render in a dropdown when inline is enabled', async () => { - const el = (await fixture(html` `)) as SlColorPicker; + const el = await fixture(html` `); const dropdown = el.shadowRoot.querySelector('sl-dropdown'); expect(dropdown).to.not.exist; }); it('should show opacity slider when opacity is enabled', async () => { - const el = (await fixture(html` `)) as SlColorPicker; + const el = await fixture(html` `); const opacitySlider = el.shadowRoot.querySelector('[part*="opacity-slider"]') as HTMLElement; expect(opacitySlider).to.exist; diff --git a/src/components/details/details.test.ts b/src/components/details/details.test.ts index 125cc3f6..cb75deee 100644 --- a/src/components/details/details.test.ts +++ b/src/components/details/details.test.ts @@ -6,7 +6,7 @@ import type SlDetails from './details'; describe('', () => { it('should be visible with the open attribute', async () => { - const el = await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo @@ -19,7 +19,7 @@ describe('', () => { }); it('should not be visible without the open attribute', async () => { - const el = await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo @@ -32,13 +32,13 @@ describe('', () => { }); it('should emit sl-show and sl-after-show when calling show()', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. - `)) as SlDetails; + `); const body = el.shadowRoot?.querySelector('.details__body') as HTMLElement; const showHandler = sinon.spy(); const afterShowHandler = sinon.spy(); @@ -56,13 +56,13 @@ describe('', () => { }); it('should emit sl-hide and sl-after-hide when calling hide()', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. - `)) as SlDetails; + `); const body = el.shadowRoot?.querySelector('.details__body') as HTMLElement; const hideHandler = sinon.spy(); const afterHideHandler = sinon.spy(); @@ -80,13 +80,13 @@ describe('', () => { }); it('should emit sl-show and sl-after-show when setting open = true', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. - `)) as SlDetails; + `); const body = el.shadowRoot?.querySelector('.details__body') as HTMLElement; const showHandler = sinon.spy(); const afterShowHandler = sinon.spy(); @@ -104,13 +104,13 @@ describe('', () => { }); it('should emit sl-hide and sl-after-hide when setting open = false', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. - `)) as SlDetails; + `); const body = el.shadowRoot?.querySelector('.details__body') as HTMLElement; const hideHandler = sinon.spy(); const afterHideHandler = sinon.spy(); @@ -128,7 +128,7 @@ describe('', () => { }); it('should be the correct size after opening more than one instance', async () => { - const el = await fixture(html` + const el = await fixture(html`
diff --git a/src/components/dialog/dialog.test.ts b/src/components/dialog/dialog.test.ts index 9fa9e048..e5b9995b 100644 --- a/src/components/dialog/dialog.test.ts +++ b/src/components/dialog/dialog.test.ts @@ -6,7 +6,7 @@ import type SlDialog from './dialog'; describe('', () => { it('should be visible with the open attribute', async () => { - const el = await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; @@ -15,16 +15,18 @@ describe('', () => { }); it('should not be visible without the open attribute', async () => { - const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. `); + const el = await fixture( + html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. ` + ); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; expect(base.hidden).to.be.true; }); it('should emit sl-show and sl-after-show when calling show()', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. - `)) as SlDialog; + `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const showHandler = sinon.spy(); const afterShowHandler = sinon.spy(); @@ -42,9 +44,9 @@ describe('', () => { }); it('should emit sl-hide and sl-after-hide when calling hide()', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. - `)) as SlDialog; + `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const hideHandler = sinon.spy(); const afterHideHandler = sinon.spy(); @@ -62,9 +64,9 @@ describe('', () => { }); it('should emit sl-show and sl-after-show when setting open = true', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. - `)) as SlDialog; + `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const showHandler = sinon.spy(); const afterShowHandler = sinon.spy(); @@ -82,9 +84,9 @@ describe('', () => { }); it('should emit sl-hide and sl-after-hide when setting open = false', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. - `)) as SlDialog; + `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const hideHandler = sinon.spy(); const afterHideHandler = sinon.spy(); @@ -102,9 +104,9 @@ describe('', () => { }); it('should not close when sl-request-close is prevented', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. - `)) as SlDialog; + `); const overlay = el.shadowRoot?.querySelector('[part="overlay"]') as HTMLElement; el.addEventListener('sl-request-close', event => event.preventDefault()); @@ -114,7 +116,7 @@ describe('', () => { }); it('should allow initial focus to be set', async () => { - const el = (await fixture(html` `)) as SlDialog; + const el = await fixture(html` `); const input = el.querySelector('input'); const initialFocusHandler = sinon.spy(event => { event.preventDefault(); diff --git a/src/components/drawer/drawer.test.ts b/src/components/drawer/drawer.test.ts index 9ad4cc8d..7e964000 100644 --- a/src/components/drawer/drawer.test.ts +++ b/src/components/drawer/drawer.test.ts @@ -6,7 +6,7 @@ import type SlDrawer from './drawer'; describe('', () => { it('should be visible with the open attribute', async () => { - const el = await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; @@ -15,16 +15,18 @@ describe('', () => { }); it('should not be visible without the open attribute', async () => { - const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. `); + const el = await fixture( + html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. ` + ); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; expect(base.hidden).to.be.true; }); it('should emit sl-show and sl-after-show when calling show()', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. - `)) as SlDrawer; + `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const showHandler = sinon.spy(); const afterShowHandler = sinon.spy(); @@ -42,9 +44,9 @@ describe('', () => { }); it('should emit sl-hide and sl-after-hide when calling hide()', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. - `)) as SlDrawer; + `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const hideHandler = sinon.spy(); const afterHideHandler = sinon.spy(); @@ -62,9 +64,9 @@ describe('', () => { }); it('should emit sl-show and sl-after-show when setting open = true', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. - `)) as SlDrawer; + `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const showHandler = sinon.spy(); const afterShowHandler = sinon.spy(); @@ -82,9 +84,9 @@ describe('', () => { }); it('should emit sl-hide and sl-after-hide when setting open = false', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. - `)) as SlDrawer; + `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const hideHandler = sinon.spy(); const afterHideHandler = sinon.spy(); @@ -102,9 +104,9 @@ describe('', () => { }); it('should not close when sl-request-close is prevented', async () => { - const el = (await fixture(html` + const el = await fixture(html` Lorem ipsum dolor sit amet, consectetur adipiscing elit. - `)) as SlDrawer; + `); const overlay = el.shadowRoot?.querySelector('[part="overlay"]') as HTMLElement; el.addEventListener('sl-request-close', event => event.preventDefault()); @@ -114,7 +116,7 @@ describe('', () => { }); it('should allow initial focus to be set', async () => { - const el = (await fixture(html` `)) as SlDrawer; + const el = await fixture(html` `); const input = el.querySelector('input'); const initialFocusHandler = sinon.spy(event => { event.preventDefault(); diff --git a/src/components/dropdown/dropdown.test.ts b/src/components/dropdown/dropdown.test.ts index 032cabae..428d942d 100644 --- a/src/components/dropdown/dropdown.test.ts +++ b/src/components/dropdown/dropdown.test.ts @@ -6,7 +6,7 @@ import type SlDropdown from './dropdown'; describe('', () => { it('should be visible with the open attribute', async () => { - const el = await fixture(html` + const el = await fixture(html` Toggle @@ -22,7 +22,7 @@ describe('', () => { }); it('should not be visible without the open attribute', async () => { - const el = await fixture(html` + const el = await fixture(html` Toggle @@ -38,7 +38,7 @@ describe('', () => { }); it('should emit sl-show and sl-after-show when calling show()', async () => { - const el = (await fixture(html` + const el = await fixture(html` Toggle @@ -47,7 +47,7 @@ describe('', () => { Item 3 - `)) as SlDropdown; + `); const panel = el.shadowRoot?.querySelector('[part="panel"]') as HTMLElement; const showHandler = sinon.spy(); const afterShowHandler = sinon.spy(); @@ -65,7 +65,7 @@ describe('', () => { }); it('should emit sl-hide and sl-after-hide when calling hide()', async () => { - const el = (await fixture(html` + const el = await fixture(html` Toggle @@ -74,7 +74,7 @@ describe('', () => { Item 3 - `)) as SlDropdown; + `); const panel = el.shadowRoot?.querySelector('[part="panel"]') as HTMLElement; const hideHandler = sinon.spy(); const afterHideHandler = sinon.spy(); @@ -92,7 +92,7 @@ describe('', () => { }); it('should emit sl-show and sl-after-show when setting open = true', async () => { - const el = (await fixture(html` + const el = await fixture(html` Toggle @@ -101,7 +101,7 @@ describe('', () => { Item 3 - `)) as SlDropdown; + `); const panel = el.shadowRoot?.querySelector('[part="panel"]') as HTMLElement; const showHandler = sinon.spy(); const afterShowHandler = sinon.spy(); @@ -119,7 +119,7 @@ describe('', () => { }); it('should emit sl-hide and sl-after-hide when setting open = false', async () => { - const el = (await fixture(html` + const el = await fixture(html` Toggle @@ -128,7 +128,7 @@ describe('', () => { Item 3 - `)) as SlDropdown; + `); const panel = el.shadowRoot?.querySelector('[part="panel"]') as HTMLElement; const hideHandler = sinon.spy(); const afterHideHandler = sinon.spy(); diff --git a/src/components/include/include.test.ts b/src/components/include/include.test.ts index cd91d8ec..b3d37190 100644 --- a/src/components/include/include.test.ts +++ b/src/components/include/include.test.ts @@ -6,7 +6,9 @@ import type SlInclude from './include'; describe('', () => { it('should load content and emit sl-load', async () => { - const el = await fixture(html` `); + const el = await fixture( + html` ` + ); const loadHandler = sinon.spy(); el.addEventListener('sl-load', loadHandler); @@ -17,7 +19,7 @@ describe('', () => { }); it('should emit sl-error when content cannot be loaded', async () => { - const el = await fixture(html` `); + const el = await fixture(html` `); const loadHandler = sinon.spy(); el.addEventListener('sl-error', loadHandler); diff --git a/src/components/input/input.test.ts b/src/components/input/input.test.ts index 40835bf5..fb27016a 100644 --- a/src/components/input/input.test.ts +++ b/src/components/input/input.test.ts @@ -6,26 +6,26 @@ import type SlInput from './input'; describe('', () => { it('should be disabled with the disabled attribute', async () => { - const el = await fixture(html` `); + const el = await fixture(html` `); const input = el.shadowRoot?.querySelector('[part="input"]') as HTMLInputElement; expect(input.disabled).to.be.true; }); it('should be valid by default', async () => { - const el = (await fixture(html` `)) as SlInput; + const el = await fixture(html` `); expect(el.invalid).to.be.false; }); it('should be invalid when required and empty', async () => { - const el = (await fixture(html` `)) as SlInput; + const el = await fixture(html` `); expect(el.invalid).to.be.true; }); it('should be invalid when required and after removing disabled ', async () => { - const el = (await fixture(html` `)) as SlInput; + const el = await fixture(html` `); el.disabled = false; await el.updateComplete; diff --git a/src/components/radio/radio.test.ts b/src/components/radio/radio.test.ts new file mode 100644 index 00000000..bf90ec53 --- /dev/null +++ b/src/components/radio/radio.test.ts @@ -0,0 +1,47 @@ +import { expect, fixture, html, oneEvent } from '@open-wc/testing'; +import { sendKeys } from '@web/test-runner-commands'; + +import '../../../dist/shoelace.js'; +import type SlRadio from './radio'; + +describe('', () => { + it('should be disabled with the disabled attribute', async () => { + const el = await fixture(html` `); + const radio = el.shadowRoot?.querySelector('input'); + + expect(radio.disabled).to.be.true; + }); + + it('should be valid by default', async () => { + const el = await fixture(html` `); + + expect(el.invalid).to.be.false; + }); + + it('should fire sl-change when clicked', async () => { + const el = await fixture(html` `); + setTimeout(() => el.shadowRoot?.querySelector('input').click()); + const event = await oneEvent(el, 'sl-change'); + expect(event.target).to.equal(el); + expect(el.checked).to.be.true; + }); + + it('should fire sl-change when toggled via keyboard', async () => { + const el = await fixture(html` `); + const input = el.shadowRoot?.querySelector('input'); + input.focus(); + setTimeout(() => sendKeys({ press: ' ' })); + const event = await oneEvent(el, 'sl-change'); + 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 () => { + const el = await fixture(html` `); + el.addEventListener('sl-change', () => expect.fail('event fired')); + el.checked = true; + await el.updateComplete; + el.checked = false; + await el.updateComplete; + }); +}); diff --git a/src/components/radio/radio.ts b/src/components/radio/radio.ts index fc3d16ab..f6b3223f 100644 --- a/src/components/radio/radio.ts +++ b/src/components/radio/radio.ts @@ -104,11 +104,11 @@ export default class SlRadio extends LitElement { if (this.checked) { this.getSiblingRadios().map(radio => (radio.checked = false)); } - emit(this, 'sl-change'); } handleClick() { this.checked = true; + emit(this, 'sl-change'); } @watch('disabled') diff --git a/src/components/select/select.test.ts b/src/components/select/select.test.ts index 0d676fb3..25adf9ef 100644 --- a/src/components/select/select.test.ts +++ b/src/components/select/select.test.ts @@ -6,13 +6,13 @@ import type SlSelect from './select'; describe('', () => { it('should emit sl-change when the value changes', async () => { - const el = (await fixture(html` + const el = await fixture(html` Option 1 Option 2 Option 3 - `)) as SlSelect; + `); const changeHandler = sinon.spy(); el.addEventListener('sl-change', changeHandler); diff --git a/src/components/switch/switch.test.ts b/src/components/switch/switch.test.ts new file mode 100644 index 00000000..faaad61c --- /dev/null +++ b/src/components/switch/switch.test.ts @@ -0,0 +1,57 @@ +import { expect, fixture, html, oneEvent } from '@open-wc/testing'; +import { sendKeys } from '@web/test-runner-commands'; + +import '../../../dist/shoelace.js'; +import type SlSwitch from './switch'; + +describe('', () => { + it('should be disabled with the disabled attribute', async () => { + const el = await fixture(html` `); + const input = el.shadowRoot?.querySelector('input'); + + expect(input.disabled).to.be.true; + }); + + it('should be valid by default', async () => { + const el = await fixture(html` `); + + expect(el.invalid).to.be.false; + }); + + it('should fire sl-change when clicked', async () => { + const el = await fixture(html` `); + setTimeout(() => el.shadowRoot?.querySelector('input').click()); + const event = await oneEvent(el, 'sl-change'); + expect(event.target).to.equal(el); + expect(el.checked).to.be.true; + }); + + it('should fire sl-change when toggled with spacebar', async () => { + const el = await fixture(html` `); + el.focus(); + setTimeout(() => sendKeys({ press: ' ' })); + const event = await oneEvent(el, 'sl-change'); + expect(event.target).to.equal(el); + expect(el.checked).to.be.true; + }); + + // TODO - arrow key doesn't seem to be sending + // TODO - test for left arrow too + // it('should fire sl-change when toggled with the right arrow', async () => { + // const el = await fixture(html` `); + // el.focus(); + // setTimeout(() => sendKeys({ press: 'ArrowRight' })); + // const event = await oneEvent(el, 'sl-change'); + // 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 () => { + const el = await fixture(html` `); + el.addEventListener('sl-change', () => expect.fail('event fired')); + el.checked = true; + await el.updateComplete; + el.checked = false; + await el.updateComplete; + }); +}); diff --git a/src/components/switch/switch.ts b/src/components/switch/switch.ts index 86350041..6ca37c6b 100644 --- a/src/components/switch/switch.ts +++ b/src/components/switch/switch.ts @@ -97,12 +97,12 @@ export default class SlSwitch extends LitElement { if (this.input) { this.input.checked = this.checked; this.invalid = !this.input.checkValidity(); - emit(this, 'sl-change'); } } handleClick() { this.checked = !this.checked; + emit(this, 'sl-change'); } @watch('disabled') @@ -123,11 +123,13 @@ export default class SlSwitch extends LitElement { if (event.key === 'ArrowLeft') { event.preventDefault(); this.checked = false; + // emit(this, 'sl-change'); } if (event.key === 'ArrowRight') { event.preventDefault(); this.checked = true; + // emit(this, 'sl-change'); } } diff --git a/src/components/textarea/textarea.test.ts b/src/components/textarea/textarea.test.ts index 18ce3378..5c54896a 100644 --- a/src/components/textarea/textarea.test.ts +++ b/src/components/textarea/textarea.test.ts @@ -6,26 +6,26 @@ import type SlTextarea from './textarea'; describe('', () => { it('should be disabled with the disabled attribute', async () => { - const el = await fixture(html` `); + const el = await fixture(html` `); const textarea = el.shadowRoot?.querySelector('[part="textarea"]') as HTMLInputElement; expect(textarea.disabled).to.be.true; }); it('should be valid by default', async () => { - const el = (await fixture(html` `)) as SlTextarea; + const el = await fixture(html` `); expect(el.invalid).to.be.false; }); it('should be invalid when required and empty', async () => { - const el = (await fixture(html` `)) as SlTextarea; + const el = await fixture(html` `); expect(el.invalid).to.be.true; }); it('should be invalid when required and after removing disabled ', async () => { - const el = (await fixture(html` `)) as SlTextarea; + const el = await fixture(html` `); el.disabled = false; await el.updateComplete; diff --git a/src/components/tooltip/tooltip.test.ts b/src/components/tooltip/tooltip.test.ts index d9d9dfcb..d7b5e1b9 100644 --- a/src/components/tooltip/tooltip.test.ts +++ b/src/components/tooltip/tooltip.test.ts @@ -6,7 +6,7 @@ import type SlTooltip from './tooltip'; describe('', () => { it('should be visible with the open attribute', async () => { - const el = await fixture(html` + const el = await fixture(html` Hover Me @@ -17,7 +17,7 @@ describe('', () => { }); it('should not be visible without the open attribute', async () => { - const el = await fixture(html` + const el = await fixture(html` Hover Me @@ -28,11 +28,11 @@ describe('', () => { }); it('should emit sl-show and sl-after-show when calling show()', async () => { - const el = (await fixture(html` + const el = await fixture(html` Hover Me - `)) as SlTooltip; + `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const showHandler = sinon.spy(); const afterShowHandler = sinon.spy(); @@ -50,11 +50,11 @@ describe('', () => { }); it('should emit sl-hide and sl-after-hide when calling hide()', async () => { - const el = (await fixture(html` + const el = await fixture(html` Hover Me - `)) as SlTooltip; + `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const hideHandler = sinon.spy(); const afterHideHandler = sinon.spy(); @@ -72,11 +72,11 @@ describe('', () => { }); it('should emit sl-show and sl-after-show when setting open = true', async () => { - const el = (await fixture(html` + const el = await fixture(html` Hover Me - `)) as SlTooltip; + `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const showHandler = sinon.spy(); const afterShowHandler = sinon.spy(); @@ -94,11 +94,11 @@ describe('', () => { }); it('should emit sl-hide and sl-after-hide when setting open = false', async () => { - const el = (await fixture(html` + const el = await fixture(html` Hover Me - `)) as SlTooltip; + `); const base = el.shadowRoot?.querySelector('[part="base"]') as HTMLElement; const hideHandler = sinon.spy(); const afterHideHandler = sinon.spy();