Skip to content

Commit

Permalink
fix(form): event order of reset and submit
Browse files Browse the repository at this point in the history
  • Loading branch information
tlouisse authored and daKmoR committed Dec 6, 2019
1 parent 8f50144 commit 190ba38
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/form/src/LionForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ export class LionForm extends LionFieldset {
this._submit = ev => {
ev.preventDefault();
ev.stopPropagation();
this.dispatchEvent(new Event('submit', { bubbles: true }));
this.submitGroup();
this.dispatchEvent(new Event('submit', { bubbles: true }));
};
this.formElement.addEventListener('submit', this._submit);

this._reset = ev => {
ev.preventDefault();
ev.stopPropagation();
this.dispatchEvent(new Event('reset', { bubbles: true }));
this.resetGroup();
this.dispatchEvent(new Event('reset', { bubbles: true }));
};
this.formElement.addEventListener('reset', this._reset);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/form/stories/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ storiesOf('Forms|Form', module)
const form = document.querySelector('#form');
if (!form.hasFeedbackFor.includes('error')) {
console.log(form.serializeGroup());
form.resetGroup();
}
};
return html`
Expand All @@ -60,7 +61,7 @@ storiesOf('Forms|Form', module)
>
</lion-input>
</lion-fieldset>
<button type="submit">Submit</button>
<button type="submit">Submit & Reset (if successfully submitted)</button>
<button type="button" @click=${() => document.querySelector('#form').resetGroup()}>
Reset
</button>
Expand Down
50 changes: 49 additions & 1 deletion packages/form/test/lion-form.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, fixture, html, oneEvent } from '@open-wc/testing';
import { expect, fixture, html, oneEvent, aTimeout } from '@open-wc/testing';
import { spy } from 'sinon';

import '@lion/input/lion-input.js';
Expand Down Expand Up @@ -89,4 +89,52 @@ describe('<lion-form>', () => {
expect(submitEv.bubbles).to.be.true;
expect(submitEv.composed).to.be.false;
});

it('handles internal submit handler before dispatch', async () => {
const el = await fixture(html`
<lion-form>
<form>
<button type="submit">submit</button>
</form>
</lion-form>
`);
const button = el.querySelector('button');
const internalHandlerSpy = spy(el, 'submitGroup');
const dispatchSpy = spy(el, 'dispatchEvent');
await aTimeout();
button.click();
expect(internalHandlerSpy).to.be.calledBefore(dispatchSpy);
});

it('handles internal submit handler before dispatch', async () => {
const el = await fixture(html`
<lion-form>
<form>
<button type="submit">submit</button>
</form>
</lion-form>
`);
const button = el.querySelector('button');
const internalHandlerSpy = spy(el, 'submitGroup');
const dispatchSpy = spy(el, 'dispatchEvent');
button.click();
expect(dispatchSpy.args[0][0].type).to.equal('submit');
expect(internalHandlerSpy).to.be.calledBefore(dispatchSpy);
});

it('handles internal reset handler before dispatch', async () => {
const el = await fixture(html`
<lion-form>
<form>
<button type="reset">submit</button>
</form>
</lion-form>
`);
const button = el.querySelector('button');
const internalHandlerSpy = spy(el, 'resetGroup');
const dispatchSpy = spy(el, 'dispatchEvent');
button.click();
expect(dispatchSpy.args[0][0].type).to.equal('reset');
expect(internalHandlerSpy).to.be.calledBefore(dispatchSpy);
});
});

0 comments on commit 190ba38

Please sign in to comment.