Skip to content

Commit

Permalink
fix: emit sl-change from radio and switch on user interaction only
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Aug 10, 2021
1 parent 9b99a92 commit 9216e72
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/components/checkbox/checkbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, fixture, html, oneEvent } from '@open-wc/testing';
import { sendKeys } from '@web/test-runner-commands'

import '../../../dist/shoelace.js';
import type SlInput from './checkbox';
import type SlCheckbox from './checkbox';

describe('<sl-checkbox>', () => {
it('should be disabled with the disabled attribute', async () => {
Expand All @@ -13,21 +13,21 @@ describe('<sl-checkbox>', () => {
});

it('should be valid by default', async () => {
const el = (await fixture<SlInput>(html` <sl-checkbox></sl-checkbox> `))
const el = (await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `))

expect(el.invalid).to.be.false;
});

it('should fire sl-change when clicked', async () => {
const el = (await fixture<SlInput>(html` <sl-checkbox></sl-checkbox> `))
const el = (await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `))
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<SlInput>(html` <sl-checkbox></sl-checkbox> `))
const el = (await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `))
const input = el.shadowRoot?.querySelector('input');
input.focus();
setTimeout(() => sendKeys({ press: ' ' }));
Expand All @@ -37,7 +37,7 @@ describe('<sl-checkbox>', () => {
});

it('should not fire sl-change when checked is set by javascript', async () => {
const el = (await fixture<SlInput>(html` <sl-checkbox></sl-checkbox> `))
const el = (await fixture<SlCheckbox>(html` <sl-checkbox></sl-checkbox> `))
el.addEventListener('sl-change', () => expect.fail('event fired'))
el.checked = true;
await el.updateComplete;
Expand Down
47 changes: 47 additions & 0 deletions src/components/radio/radio.test.ts
Original file line number Diff line number Diff line change
@@ -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('<sl-radio>', () => {
it('should be disabled with the disabled attribute', async () => {
const el = await fixture(html` <sl-radio disabled></sl-radio> `);
const radio = el.shadowRoot?.querySelector('input');

expect(radio.disabled).to.be.true;
});

it('should be valid by default', async () => {
const el = (await fixture<SlRadio>(html` <sl-radio></sl-radio> `))

expect(el.invalid).to.be.false;
});

it('should fire sl-change when clicked', async () => {
const el = (await fixture<SlRadio>(html` <sl-radio></sl-radio> `))
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<SlRadio>(html` <sl-radio></sl-radio> `))
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<SlRadio>(html` <sl-radio></sl-radio> `))
el.addEventListener('sl-change', () => expect.fail('event fired'))
el.checked = true;
await el.updateComplete;
el.checked = false;
await el.updateComplete;
});
});
2 changes: 1 addition & 1 deletion src/components/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
47 changes: 47 additions & 0 deletions src/components/switch/switch.test.ts
Original file line number Diff line number Diff line change
@@ -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 SlSwitch from './switch';

describe('<sl-switch>', () => {
it('should be disabled with the disabled attribute', async () => {
const el = await fixture(html` <sl-switch disabled></sl-switch> `);
const input = el.shadowRoot?.querySelector('input');

expect(input.disabled).to.be.true;
});

it('should be valid by default', async () => {
const el = (await fixture<SlSwitch>(html` <sl-switch></sl-switch> `))

expect(el.invalid).to.be.false;
});

it('should fire sl-change when clicked', async () => {
const el = (await fixture<SlSwitch>(html` <sl-switch></sl-switch> `))
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<SlSwitch>(html` <sl-switch></sl-switch> `))
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<SlSwitch>(html` <sl-switch></sl-switch> `))
el.addEventListener('sl-change', () => expect.fail('event fired'))
el.checked = true;
await el.updateComplete;
el.checked = false;
await el.updateComplete;
});
});
2 changes: 1 addition & 1 deletion src/components/switch/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 9216e72

Please sign in to comment.