Skip to content

Commit 563fe5d

Browse files
authored
test: extract validation tests into individual file (#6263)
1 parent f5ff517 commit 563fe5d

7 files changed

Lines changed: 221 additions & 197 deletions

File tree

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

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -103,71 +103,6 @@ describe('custom field', () => {
103103
});
104104
});
105105

106-
describe('required', () => {
107-
beforeEach(() => {
108-
customField.required = true;
109-
});
110-
111-
it('should set invalid to false by default', () => {
112-
expect(customField.invalid).to.be.false;
113-
});
114-
115-
it('should become invalid on validate call when empty', () => {
116-
expect(customField.invalid).to.be.false;
117-
customField.validate();
118-
expect(customField.invalid).to.be.true;
119-
});
120-
121-
it('should become valid after receiving a non-empty value from "change" event', () => {
122-
customField.inputs[0].value = 'foo';
123-
dispatchChange(customField.inputs[0]);
124-
expect(customField.invalid).to.be.false;
125-
});
126-
127-
it('should become invalid after receiving an empty value from "change" event', () => {
128-
customField.value = 'foo';
129-
customField.inputs[0].value = '';
130-
dispatchChange(customField.inputs[0]);
131-
expect(customField.invalid).to.be.true;
132-
});
133-
});
134-
135-
describe('validation', () => {
136-
it('should check validity on validate', () => {
137-
const spy = sinon.spy(customField, 'checkValidity');
138-
customField.validate();
139-
expect(spy.called).to.be.true;
140-
});
141-
142-
it('should run validation on input change', () => {
143-
const spy = sinon.spy(customField, 'checkValidity');
144-
customField.inputs[0].value = 'foo';
145-
dispatchChange(customField.inputs[0]);
146-
expect(spy.called).to.be.true;
147-
});
148-
149-
it('should fire a validated event on validation success', () => {
150-
const validatedSpy = sinon.spy();
151-
customField.addEventListener('validated', validatedSpy);
152-
customField.validate();
153-
154-
expect(validatedSpy.calledOnce).to.be.true;
155-
const event = validatedSpy.firstCall.args[0];
156-
expect(event.detail.valid).to.be.true;
157-
});
158-
159-
it('should fire a validated event on validation failure', () => {
160-
const validatedSpy = sinon.spy();
161-
customField.addEventListener('validated', validatedSpy);
162-
customField.required = true;
163-
customField.validate();
164-
165-
expect(validatedSpy.calledOnce).to.be.true;
166-
const event = validatedSpy.firstCall.args[0];
167-
expect(event.detail.valid).to.be.false;
168-
});
169-
});
170-
171106
describe('custom parser and formatter', () => {
172107
it('should use custom parser if that exists', async () => {
173108
customField.parseValue = (value) => {
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
3+
import sinon from 'sinon';
4+
import '../src/vaadin-custom-field.js';
5+
import { dispatchChange } from './helpers.js';
6+
7+
describe('validation', () => {
8+
let customField;
9+
10+
describe('basic', () => {
11+
beforeEach(async () => {
12+
customField = fixtureSync(`
13+
<vaadin-custom-field>
14+
<input type="text" />
15+
<input type="number" />
16+
</vaadin-custom-field>
17+
`);
18+
await nextRender();
19+
});
20+
21+
it('should check validity on validate', () => {
22+
const spy = sinon.spy(customField, 'checkValidity');
23+
customField.validate();
24+
expect(spy.called).to.be.true;
25+
});
26+
27+
it('should run validation on input change', () => {
28+
const spy = sinon.spy(customField, 'checkValidity');
29+
customField.inputs[0].value = 'foo';
30+
dispatchChange(customField.inputs[0]);
31+
expect(spy.called).to.be.true;
32+
});
33+
34+
it('should fire a validated event on validation success', () => {
35+
const validatedSpy = sinon.spy();
36+
customField.addEventListener('validated', validatedSpy);
37+
customField.validate();
38+
39+
expect(validatedSpy.calledOnce).to.be.true;
40+
const event = validatedSpy.firstCall.args[0];
41+
expect(event.detail.valid).to.be.true;
42+
});
43+
44+
it('should fire a validated event on validation failure', () => {
45+
const validatedSpy = sinon.spy();
46+
customField.addEventListener('validated', validatedSpy);
47+
customField.required = true;
48+
customField.validate();
49+
50+
expect(validatedSpy.calledOnce).to.be.true;
51+
const event = validatedSpy.firstCall.args[0];
52+
expect(event.detail.valid).to.be.false;
53+
});
54+
});
55+
56+
describe('required', () => {
57+
beforeEach(async () => {
58+
customField = fixtureSync(`
59+
<vaadin-custom-field required>
60+
<input type="text" />
61+
<input type="number" />
62+
</vaadin-custom-field>
63+
`);
64+
await nextRender();
65+
});
66+
67+
it('should set invalid to false by default', () => {
68+
expect(customField.invalid).to.be.false;
69+
});
70+
71+
it('should become invalid on validate call when empty', () => {
72+
expect(customField.invalid).to.be.false;
73+
customField.validate();
74+
expect(customField.invalid).to.be.true;
75+
});
76+
77+
it('should become valid after receiving a non-empty value from "change" event', () => {
78+
customField.inputs[0].value = 'foo';
79+
dispatchChange(customField.inputs[0]);
80+
expect(customField.invalid).to.be.false;
81+
});
82+
83+
it('should become invalid after receiving an empty value from "change" event', () => {
84+
customField.value = 'foo';
85+
customField.inputs[0].value = '';
86+
dispatchChange(customField.inputs[0]);
87+
expect(customField.invalid).to.be.true;
88+
});
89+
});
90+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
import '../src/vaadin-lit-email-field.js';
2-
import './email-field.common.js';
2+
import './validation.common.js';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
import '../src/vaadin-email-field.js';
2-
import './email-field.common.js';
2+
import './validation.common.js';

packages/email-field/test/email-field.common.js renamed to packages/email-field/test/validation.common.js

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ const invalidAddresses = [
3030
'email@example',
3131
];
3232

33-
describe('email-field', () => {
34-
describe('default', () => {
35-
let emailField;
33+
describe('validation', () => {
34+
let emailField;
3635

36+
describe('basic', () => {
3737
beforeEach(async () => {
3838
emailField = fixtureSync('<vaadin-email-field></vaadin-email-field>');
3939
await nextRender();
@@ -60,11 +60,31 @@ describe('email-field', () => {
6060
});
6161
});
6262
});
63+
64+
it('should fire a validated event on validation success', () => {
65+
const validatedSpy = sinon.spy();
66+
emailField.addEventListener('validated', validatedSpy);
67+
emailField.validate();
68+
69+
expect(validatedSpy.calledOnce).to.be.true;
70+
const event = validatedSpy.firstCall.args[0];
71+
expect(event.detail.valid).to.be.true;
72+
});
73+
74+
it('should fire a validated event on validation failure', async () => {
75+
const validatedSpy = sinon.spy();
76+
emailField.addEventListener('validated', validatedSpy);
77+
emailField.required = true;
78+
await nextUpdate(emailField);
79+
emailField.validate();
80+
81+
expect(validatedSpy.calledOnce).to.be.true;
82+
const event = validatedSpy.firstCall.args[0];
83+
expect(event.detail.valid).to.be.false;
84+
});
6385
});
6486

6587
describe('custom pattern', () => {
66-
let emailField;
67-
6888
beforeEach(async () => {
6989
emailField = fixtureSync('<vaadin-email-field pattern=".+@example.com"></vaadin-email-field>');
7090
await nextRender();
@@ -75,85 +95,56 @@ describe('email-field', () => {
7595
});
7696
});
7797

78-
describe('initial validation', () => {
79-
let field, validateSpy;
98+
describe('initial', () => {
99+
let validateSpy;
80100

81101
beforeEach(() => {
82-
field = document.createElement('vaadin-email-field');
83-
validateSpy = sinon.spy(field, 'validate');
102+
emailField = document.createElement('vaadin-email-field');
103+
validateSpy = sinon.spy(emailField, 'validate');
84104
});
85105

86106
afterEach(() => {
87-
field.remove();
107+
emailField.remove();
88108
});
89109

90110
it('should not validate without value', async () => {
91-
document.body.appendChild(field);
111+
document.body.appendChild(emailField);
92112
await nextRender();
93113
expect(validateSpy.called).to.be.false;
94114
});
95115

96116
describe('with value', () => {
97117
beforeEach(() => {
98-
field.value = 'foo@example.com';
118+
emailField.value = 'foo@example.com';
99119
});
100120

101121
it('should validate by default', async () => {
102-
document.body.appendChild(field);
122+
document.body.appendChild(emailField);
103123
await nextRender();
104124
expect(validateSpy.calledOnce).to.be.true;
105125
});
106126

107127
it('should validate when using a custom pattern', async () => {
108-
field.pattern = '.+@example.com';
109-
document.body.appendChild(field);
128+
emailField.pattern = '.+@example.com';
129+
document.body.appendChild(emailField);
110130
await nextRender();
111131
expect(validateSpy.calledOnce).to.be.true;
112132
});
113133

114134
it('should not validate when pattern is unset', async () => {
115-
field.pattern = '';
116-
document.body.appendChild(field);
135+
emailField.pattern = '';
136+
document.body.appendChild(emailField);
117137
await nextRender();
118138
expect(validateSpy.called).to.be.false;
119139
});
120140

121141
it('should not validate when pattern is unset and the field has invalid', async () => {
122-
field.pattern = '';
123-
field.invalid = true;
124-
document.body.appendChild(field);
142+
emailField.pattern = '';
143+
emailField.invalid = true;
144+
document.body.appendChild(emailField);
125145
await nextRender();
126146
expect(validateSpy.called).to.be.false;
127147
});
128148
});
129149
});
130-
131-
describe('validation', () => {
132-
let field, validatedSpy;
133-
134-
beforeEach(async () => {
135-
field = fixtureSync('<vaadin-email-field></vaadin-email-field>');
136-
await nextRender();
137-
validatedSpy = sinon.spy();
138-
field.addEventListener('validated', validatedSpy);
139-
});
140-
141-
it('should fire a validated event on validation success', () => {
142-
field.validate();
143-
144-
expect(validatedSpy.calledOnce).to.be.true;
145-
const event = validatedSpy.firstCall.args[0];
146-
expect(event.detail.valid).to.be.true;
147-
});
148-
149-
it('should fire a validated event on validation failure', async () => {
150-
field.required = true;
151-
await nextUpdate(field);
152-
field.validate();
153-
154-
expect(validatedSpy.calledOnce).to.be.true;
155-
const event = validatedSpy.firstCall.args[0];
156-
expect(event.detail.valid).to.be.false;
157-
});
158-
});
159150
});

0 commit comments

Comments
 (0)