-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(all) added docs for helpers and added a few tests/assertions
- Loading branch information
1 parent
38a2583
commit 5c6709b
Showing
3 changed files
with
144 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,67 @@ | ||
import { aTimeout, expect, fixture, fixtureCleanup, html } from '@open-wc/testing'; | ||
import * as sinon from 'sinon'; | ||
import { submit } from '../src'; | ||
|
||
let submitted = false; | ||
const submitCallback = (event: Event) => { | ||
const submitCallbackPrevented = (event: Event) => { | ||
event.preventDefault(); | ||
submitted = true; | ||
}; | ||
const submitCallback = (event: Event) => { | ||
submitted = true; | ||
}; | ||
|
||
describe('The submit form helper', () => { | ||
let form: HTMLFormElement; | ||
let formSubmitStub: sinon.SinonSpy; | ||
|
||
beforeEach(async () => { | ||
form = await fixture<HTMLFormElement>(html`<form @submit="${submitCallback}"> | ||
<input> | ||
</form>`); | ||
formSubmitStub = sinon.stub(form, 'submit').callsFake(() => {}); | ||
submitted = false; | ||
}); | ||
|
||
afterEach(fixtureCleanup); | ||
afterEach(() => { | ||
sinon.restore(); | ||
fixtureCleanup(); | ||
}); | ||
|
||
it('will submit a form that is valid', async () => { | ||
submit(form); | ||
await aTimeout(0); | ||
expect(submitted).to.be.true; | ||
// form.submit() is called | ||
expect(formSubmitStub.callCount).to.equal(1); | ||
}); | ||
|
||
it('will not submit a form that is invalid', async () => { | ||
it('will not fire the submit event for a form that is invalid', async () => { | ||
const input = form.querySelector<HTMLInputElement>('input')!; | ||
input.required = true; | ||
submit(form); | ||
expect(submitted).to.be.false; | ||
}); | ||
|
||
it('will not submit a form that is invalid', async () => { | ||
const input = form.querySelector<HTMLInputElement>('input')!; | ||
input.required = true; | ||
submit(form); | ||
|
||
expect(formSubmitStub.callCount).to.equal(0); | ||
}); | ||
|
||
it('will not submit a form when the submit event is `defaultPrevented`', async () => { | ||
form = await fixture<HTMLFormElement>(html`<form @submit="${submitCallbackPrevented}"> | ||
<input> | ||
</form>`); | ||
|
||
formSubmitStub = sinon.stub(form, 'submit').callsFake(() => {}); | ||
|
||
submit(form); | ||
|
||
expect(formSubmitStub.callCount).to.equal(0); | ||
}); | ||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "lib" | ||
} | ||
} | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "lib" | ||
} | ||
} |