-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: extract validation tests into individual file (#6263)
- Loading branch information
Showing
7 changed files
with
221 additions
and
197 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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { expect } from '@esm-bundle/chai'; | ||
import { fixtureSync, nextRender } from '@vaadin/testing-helpers'; | ||
import sinon from 'sinon'; | ||
import '../src/vaadin-custom-field.js'; | ||
import { dispatchChange } from './helpers.js'; | ||
|
||
describe('validation', () => { | ||
let customField; | ||
|
||
describe('basic', () => { | ||
beforeEach(async () => { | ||
customField = fixtureSync(` | ||
<vaadin-custom-field> | ||
<input type="text" /> | ||
<input type="number" /> | ||
</vaadin-custom-field> | ||
`); | ||
await nextRender(); | ||
}); | ||
|
||
it('should check validity on validate', () => { | ||
const spy = sinon.spy(customField, 'checkValidity'); | ||
customField.validate(); | ||
expect(spy.called).to.be.true; | ||
}); | ||
|
||
it('should run validation on input change', () => { | ||
const spy = sinon.spy(customField, 'checkValidity'); | ||
customField.inputs[0].value = 'foo'; | ||
dispatchChange(customField.inputs[0]); | ||
expect(spy.called).to.be.true; | ||
}); | ||
|
||
it('should fire a validated event on validation success', () => { | ||
const validatedSpy = sinon.spy(); | ||
customField.addEventListener('validated', validatedSpy); | ||
customField.validate(); | ||
|
||
expect(validatedSpy.calledOnce).to.be.true; | ||
const event = validatedSpy.firstCall.args[0]; | ||
expect(event.detail.valid).to.be.true; | ||
}); | ||
|
||
it('should fire a validated event on validation failure', () => { | ||
const validatedSpy = sinon.spy(); | ||
customField.addEventListener('validated', validatedSpy); | ||
customField.required = true; | ||
customField.validate(); | ||
|
||
expect(validatedSpy.calledOnce).to.be.true; | ||
const event = validatedSpy.firstCall.args[0]; | ||
expect(event.detail.valid).to.be.false; | ||
}); | ||
}); | ||
|
||
describe('required', () => { | ||
beforeEach(async () => { | ||
customField = fixtureSync(` | ||
<vaadin-custom-field required> | ||
<input type="text" /> | ||
<input type="number" /> | ||
</vaadin-custom-field> | ||
`); | ||
await nextRender(); | ||
}); | ||
|
||
it('should set invalid to false by default', () => { | ||
expect(customField.invalid).to.be.false; | ||
}); | ||
|
||
it('should become invalid on validate call when empty', () => { | ||
expect(customField.invalid).to.be.false; | ||
customField.validate(); | ||
expect(customField.invalid).to.be.true; | ||
}); | ||
|
||
it('should become valid after receiving a non-empty value from "change" event', () => { | ||
customField.inputs[0].value = 'foo'; | ||
dispatchChange(customField.inputs[0]); | ||
expect(customField.invalid).to.be.false; | ||
}); | ||
|
||
it('should become invalid after receiving an empty value from "change" event', () => { | ||
customField.value = 'foo'; | ||
customField.inputs[0].value = ''; | ||
dispatchChange(customField.inputs[0]); | ||
expect(customField.invalid).to.be.true; | ||
}); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
.../email-field/test/email-field-lit.test.js → ...s/email-field/test/validation-lit.test.js
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,2 +1,2 @@ | ||
import '../src/vaadin-lit-email-field.js'; | ||
import './email-field.common.js'; | ||
import './validation.common.js'; |
2 changes: 1 addition & 1 deletion
2
...il-field/test/email-field-polymer.test.js → ...ail-field/test/validation-polymer.test.js
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,2 +1,2 @@ | ||
import '../src/vaadin-email-field.js'; | ||
import './email-field.common.js'; | ||
import './validation.common.js'; |
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
Oops, something went wrong.