Skip to content

Commit

Permalink
Check change event value on Checkbox/Switch toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
arnellebalane committed Oct 10, 2023
1 parent b85a9e7 commit 6a48dd7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
19 changes: 17 additions & 2 deletions src/components/Checkbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,29 @@ describe('Checkbox', () => {
cy.get('input').should('be.checked');
});

it('dispatches "change" event when checkbox is toggled', () => {
it('dispatches "change" event with false when unchecked checkbox is checked', () => {
const onChange = cy.spy();

cy.mount(Checkbox).then(({ component }) => {
component.$on('change', onChange);
});

cy.get('label').click();
cy.wrap(onChange).should('have.been.called');
cy.wrap(onChange).should('have.been.calledWith', Cypress.sinon.match.has('detail', true));
});

it('dispatches "change" event with true when unchecked checkbox is checked', () => {
const onChange = cy.spy();

cy.mount(Checkbox, {
props: {
checked: true,
},
}).then(({ component }) => {
component.$on('change', onChange);
});

cy.get('label').click();
cy.wrap(onChange).should('have.been.calledWith', Cypress.sinon.match.has('detail', false));
});
});
25 changes: 18 additions & 7 deletions src/components/Switch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import Switch from './Switch.svelte';

describe('Switch', () => {
it('does not select the checkbox when value prop is false', () => {
cy.mount(Switch, {
props: {
value: false,
},
});
cy.mount(Switch);

cy.get('input').should('not.be.checked');
});
Expand All @@ -21,14 +17,29 @@ describe('Switch', () => {
cy.get('input').should('be.checked');
});

it('dispatches "change" event when switch is toggled', () => {
it('dispatches "change" event when unchecked switch is checked', () => {
const onChange = cy.spy();

cy.mount(Switch).then(({ component }) => {
component.$on('change', onChange);
});

cy.get('label').click();
cy.wrap(onChange).should('have.been.called');
cy.wrap(onChange).should('have.been.calledWith', Cypress.sinon.match.hasNested('target.checked', true));
});

it('dispatches "change" event when checked switch is unchecked', () => {
const onChange = cy.spy();

cy.mount(Switch, {
props: {
value: true,
},
}).then(({ component }) => {
component.$on('change', onChange);
});

cy.get('label').click();
cy.wrap(onChange).should('have.been.calledWith', Cypress.sinon.match.hasNested('target.checked', false));
});
});

0 comments on commit 6a48dd7

Please sign in to comment.