Skip to content

Commit 32b7e04

Browse files
authored
test: clean up validation tests, improve test coverage (#6284)
1 parent 3512e25 commit 32b7e04

4 files changed

Lines changed: 28 additions & 23 deletions

File tree

‎packages/custom-field/test/custom-field.common.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, focusin, focusout, nextRender, nextUpdate } from '@vaadin/testing-helpers';
2+
import { fire, fixtureSync, focusin, focusout, nextRender, nextUpdate } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
4-
import { dispatchChange } from './helpers.js';
54

65
describe('custom field', () => {
76
let customField;
@@ -57,7 +56,7 @@ describe('custom field', () => {
5756
it('should update value on change event', () => {
5857
customField.inputs.forEach((el) => {
5958
el.value = '1';
60-
dispatchChange(el);
59+
fire(el, 'change');
6160
});
6261
expect(customField.value).to.equal('1\t1');
6362
});
@@ -123,7 +122,7 @@ describe('custom field', () => {
123122

124123
customField.inputs.forEach((el) => {
125124
el.value = '1';
126-
dispatchChange(el);
125+
fire(el, 'change');
127126
});
128127

129128
await nextUpdate(customField);

‎packages/custom-field/test/helpers.js‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎packages/custom-field/test/slot-wrapper.common.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, nextFrame, nextRender } from '@vaadin/testing-helpers';
2+
import { fire, fixtureSync, nextFrame, nextRender } from '@vaadin/testing-helpers';
33
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
4-
import { dispatchChange } from './helpers.js';
54

65
class XField extends PolymerElement {
76
static get template() {
@@ -118,7 +117,7 @@ Object.keys(fixtures).forEach((set) => {
118117
it('should update value when input is removed', async () => {
119118
const secondInput = parent.children[1];
120119
secondInput.value = '1';
121-
dispatchChange(secondInput);
120+
fire(secondInput, 'change');
122121
expect(customField.value).to.equal('\t1');
123122
parent.removeChild(secondInput);
124123
await nextFrame();

‎packages/custom-field/test/validation.test.js‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
2+
import { fire, fixtureSync, nextRender } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import '../src/vaadin-custom-field.js';
5-
import { dispatchChange } from './helpers.js';
65

76
describe('validation', () => {
87
let customField;
98

109
describe('basic', () => {
10+
let validateSpy;
11+
1112
beforeEach(async () => {
1213
customField = fixtureSync(`
1314
<vaadin-custom-field>
@@ -16,19 +17,29 @@ describe('validation', () => {
1617
</vaadin-custom-field>
1718
`);
1819
await nextRender();
20+
validateSpy = sinon.spy(customField, 'validate');
1921
});
2022

21-
it('should check validity on validate', () => {
22-
const spy = sinon.spy(customField, 'checkValidity');
23-
customField.validate();
24-
expect(spy.called).to.be.true;
23+
it('should pass validation by default', () => {
24+
expect(customField.validate()).to.be.true;
25+
expect(customField.checkValidity()).to.be.true;
26+
});
27+
28+
it('should validate on blur', () => {
29+
customField.inputs[0].focus();
30+
customField.inputs[0].blur();
31+
expect(validateSpy.calledOnce).to.be.true;
2532
});
2633

27-
it('should run validation on input change', () => {
28-
const spy = sinon.spy(customField, 'checkValidity');
34+
it('should validate on input change', () => {
2935
customField.inputs[0].value = 'foo';
30-
dispatchChange(customField.inputs[0]);
31-
expect(spy.called).to.be.true;
36+
fire(customField.inputs[0], 'change');
37+
expect(validateSpy.calledOnce).to.be.true;
38+
});
39+
40+
it('should validate on value change', () => {
41+
customField.value = 'foo,1';
42+
expect(validateSpy.calledOnce).to.be.true;
3243
});
3344

3445
it('should fire a validated event on validation success', () => {
@@ -76,14 +87,14 @@ describe('validation', () => {
7687

7788
it('should become valid after receiving a non-empty value from "change" event', () => {
7889
customField.inputs[0].value = 'foo';
79-
dispatchChange(customField.inputs[0]);
90+
fire(customField.inputs[0], 'change');
8091
expect(customField.invalid).to.be.false;
8192
});
8293

8394
it('should become invalid after receiving an empty value from "change" event', () => {
8495
customField.value = 'foo';
8596
customField.inputs[0].value = '';
86-
dispatchChange(customField.inputs[0]);
97+
fire(customField.inputs[0], 'change');
8798
expect(customField.invalid).to.be.true;
8899
});
89100
});

0 commit comments

Comments
 (0)