-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for reset
button
#134
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'ember-headless-form': patch | ||
--- | ||
|
||
Add support for reset button | ||
|
||
Click a native `reset` button will reset the state of the form. |
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
156 changes: 156 additions & 0 deletions
156
test-app/tests/integration/components/headless-form-reset-test.gts
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 |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* eslint-disable no-undef -- Until https://github.com/ember-cli/eslint-plugin-ember/issues/1747 is resolved... */ | ||
/* eslint-disable simple-import-sort/imports,padding-line-between-statements,decorator-position/decorator-position -- Can't fix these manually, without --fix working in .gts */ | ||
|
||
import { click, fillIn, render } from '@ember/test-helpers'; | ||
import { module, test } from 'qunit'; | ||
|
||
import { HeadlessForm } from 'ember-headless-form'; | ||
import { setupRenderingTest } from 'test-app/tests/helpers'; | ||
|
||
interface TestFormData { | ||
firstName?: string; | ||
lastName?: string; | ||
} | ||
|
||
module('Integration Component HeadlessForm > Reset', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
module('reset button', function () { | ||
test('dirty fields are resetted', async function (assert) { | ||
const data: TestFormData = { firstName: 'Tony', lastName: 'Ward' }; | ||
|
||
await render(<template> | ||
<HeadlessForm @data={{data}} as |form|> | ||
<form.Field @name="firstName" as |field|> | ||
<field.Label>First Name</field.Label> | ||
<field.Input data-test-first-name /> | ||
</form.Field> | ||
<form.Field @name="lastName" as |field|> | ||
<field.Label>Last Name</field.Label> | ||
<field.Input data-test-last-name /> | ||
</form.Field> | ||
<button type="reset" data-test-reset>Reset</button> | ||
</HeadlessForm> | ||
</template>); | ||
|
||
await fillIn('[data-test-first-name]', 'Nicole'); | ||
await click('[data-test-reset'); | ||
simonihmig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
assert.dom('[data-test-first-name]').hasValue('Tony'); | ||
assert.dom('[data-test-last-name]').hasValue('Ward'); | ||
}); | ||
|
||
test('validation errors are cleared', async function (assert) { | ||
const data: TestFormData = {}; | ||
|
||
await render(<template> | ||
<HeadlessForm @data={{data}} as |form|> | ||
<form.Field @name="firstName" as |field|> | ||
<field.Label>First Name</field.Label> | ||
<field.Input required data-test-first-name /> | ||
<field.Errors data-test-first-name-errors /> | ||
{{#if field.isInvalid}} | ||
<div data-test-invalid /> | ||
{{/if}} | ||
</form.Field> | ||
<form.Field @name="lastName" as |field|> | ||
<field.Label>Last Name</field.Label> | ||
<field.Input data-test-last-name /> | ||
<field.Errors data-test-last-name-errors /> | ||
</form.Field> | ||
<button type="submit" data-test-submit>Submit</button> | ||
<button type="reset" data-test-reset>Reset</button> | ||
</HeadlessForm> | ||
</template>); | ||
|
||
await click('[data-test-submit]'); | ||
|
||
assert | ||
.dom('[data-test-first-name-errors]') | ||
.exists({ count: 1 }, 'validation errors appear when validation fails'); | ||
assert.dom('[data-test-first-name]').hasAria('invalid', 'true'); | ||
assert.dom('[data-test-invalid]').exists(); | ||
|
||
await click('[data-test-reset]'); | ||
|
||
assert | ||
.dom('[data-test-first-name-errors]') | ||
.doesNotExist('validation errors are removed on reset'); | ||
assert.dom('[data-test-first-name]').doesNotHaveAria('invalid'); | ||
assert.dom('[data-test-invalid]').doesNotExist(); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is great! 🎉 |
||
|
||
test('validation state is resetted', async function (assert) { | ||
const data: TestFormData = {}; | ||
|
||
await render(<template> | ||
<HeadlessForm @data={{data}} as |form|> | ||
<form.Field @name="firstName" as |field|> | ||
<field.Label>First Name</field.Label> | ||
<field.Input required data-test-first-name /> | ||
</form.Field> | ||
<button type="submit" data-test-submit>Submit</button> | ||
<button type="reset" data-test-reset>Reset</button> | ||
{{#if form.validationState}} | ||
<div data-test-validation-state>{{form.validationState.state}}</div> | ||
{{/if}} | ||
</HeadlessForm> | ||
</template>); | ||
|
||
assert | ||
.dom('[data-test-validation-state]') | ||
.doesNotExist( | ||
'form.validationState is not present until first validation' | ||
); | ||
|
||
await click('[data-test-submit]'); | ||
|
||
assert | ||
.dom('[data-test-validation-state]') | ||
.hasText('RESOLVED', 'form.validationState has resolved'); | ||
|
||
await click('[data-test-reset]'); | ||
|
||
assert | ||
.dom('[data-test-validation-state]') | ||
.doesNotExist('form.validationState is resetted'); | ||
}); | ||
|
||
test('submission state is resetted', async function (assert) { | ||
const data: TestFormData = {}; | ||
const submitHandler = () => 'ok'; | ||
|
||
await render(<template> | ||
<HeadlessForm @data={{data}} @onSubmit={{submitHandler}} as |form|> | ||
<form.Field @name="firstName" as |field|> | ||
<field.Label>First Name</field.Label> | ||
<field.Input data-test-first-name /> | ||
</form.Field> | ||
<button type="submit" data-test-submit>Submit</button> | ||
<button type="reset" data-test-reset>Reset</button> | ||
{{#if form.submissionState}} | ||
<div data-test-submission-state>{{form.submissionState.state}}</div> | ||
{{/if}} | ||
</HeadlessForm> | ||
</template>); | ||
|
||
assert | ||
.dom('[data-test-submission-state]') | ||
.doesNotExist( | ||
'form.submissionState is not present until first validation' | ||
); | ||
|
||
await click('[data-test-submit]'); | ||
|
||
assert | ||
.dom('[data-test-submission-state]') | ||
.hasText('RESOLVED', 'form.submissionState has resolved'); | ||
|
||
await click('[data-test-reset]'); | ||
|
||
assert | ||
.dom('[data-test-submission-state]') | ||
.doesNotExist('form.submissionState is resetted'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Late to this, but: are we going to have explicit states for
pristine/dirty
or the consumer can figure this out from thevalidationState
and thesubmissionState
?