-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathvalidation.common.js
227 lines (192 loc) · 6.66 KB
/
validation.common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { expect } from '@vaadin/chai-plugins';
import { fixtureSync, nextRender, nextUpdate, outsideClick } from '@vaadin/testing-helpers';
import { sendKeys } from '@web/test-runner-commands';
import sinon from 'sinon';
import '@vaadin/item/vaadin-item.js';
import '@vaadin/list-box/vaadin-list-box.js';
describe('validation', () => {
let select, validateSpy, valueChangedSpy, changeSpy;
describe('basic', () => {
beforeEach(async () => {
select = fixtureSync(
`<div>
<vaadin-select></vaadin-select>
<input id="last-global-focusable" />
</div>`,
).firstElementChild;
select.items = [
{ label: 'Option 1', value: 'option-1' },
{ label: 'Option 2', value: 'option-2' },
];
await nextRender();
validateSpy = sinon.spy(select, 'validate');
changeSpy = sinon.spy();
select.addEventListener('change', changeSpy);
valueChangedSpy = sinon.spy();
select.addEventListener('value-changed', valueChangedSpy);
});
it('should pass validation by default', () => {
expect(select.checkValidity()).to.be.true;
expect(select.validate()).to.be.true;
expect(select.invalid).to.be.false;
});
it('should validate on blur', () => {
select.focus();
select.blur();
expect(validateSpy.calledOnce).to.be.true;
});
it('should validate on outside click', async () => {
select.focus();
select.click();
await nextRender();
outsideClick();
await nextUpdate(select);
expect(validateSpy.calledOnce).to.be.true;
});
it('should not validate on close with Escape', async () => {
select.focus();
select.click();
await nextRender();
await sendKeys({ press: 'Escape' });
await nextUpdate(select);
expect(validateSpy.called).to.be.false;
});
it('should validate once on close with Tab', async () => {
select.focus();
select.click();
await nextRender();
await sendKeys({ press: 'Tab' });
await nextUpdate(select);
expect(validateSpy.calledOnce).to.be.true;
});
it('should validate between value-changed and change events on Enter', async () => {
select.focus();
select.click();
await nextRender();
await sendKeys({ press: 'Enter' });
await nextUpdate(select);
expect(valueChangedSpy.calledOnce).to.be.true;
expect(validateSpy.calledOnce).to.be.true;
expect(changeSpy.calledOnce).to.be.true;
expect(validateSpy.calledAfter(valueChangedSpy)).to.be.true;
expect(validateSpy.calledBefore(changeSpy)).to.be.true;
});
it('should validate on value change', async () => {
select.value = 'option-2';
await nextUpdate(select);
expect(validateSpy.callCount).to.be.equal(1);
select.value = '';
await nextUpdate(select);
expect(validateSpy.callCount).to.be.equal(2);
});
it('should validate when removing required property', async () => {
select.required = true;
await nextUpdate(select);
expect(validateSpy.called).to.be.false;
select.required = false;
await nextUpdate(select);
expect(validateSpy.calledOnce).to.be.true;
});
it('should fire a validated event on validation success', () => {
const validatedSpy = sinon.spy();
select.addEventListener('validated', validatedSpy);
select.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', async () => {
const validatedSpy = sinon.spy();
select.addEventListener('validated', validatedSpy);
select.required = true;
await nextUpdate(select);
select.validate();
expect(validatedSpy.calledOnce).to.be.true;
const event = validatedSpy.firstCall.args[0];
expect(event.detail.valid).to.be.false;
});
describe('document losing focus', () => {
beforeEach(() => {
sinon.stub(document, 'hasFocus').returns(false);
});
afterEach(() => {
document.hasFocus.restore();
});
it('should not validate on blur when document does not have focus', () => {
select.blur();
expect(validateSpy.called).to.be.false;
});
});
});
describe('required', () => {
beforeEach(async () => {
select = fixtureSync('<vaadin-select required></vaadin-select>');
select.items = [
{ label: '', value: '' },
{ label: 'Option 1', value: 'option-1' },
];
await nextRender();
});
it('should fail validation without value', () => {
expect(select.checkValidity()).to.be.false;
});
it('should pass validation without value when readonly', async () => {
select.readonly = true;
await nextUpdate(select);
expect(select.checkValidity()).to.be.true;
});
it('should pass validation with a value', async () => {
select.value = 'option-2';
await nextUpdate(select);
expect(select.checkValidity()).to.be.true;
});
it('should be valid when committing a non-empty value', async () => {
select.focus();
select.click();
await nextRender();
await sendKeys({ press: 'ArrowDown' });
await sendKeys({ press: 'Enter' });
await nextUpdate(select);
expect(select.invalid).to.be.false;
});
it('should be invalid when committing an empty value', async () => {
select.value = 'option-1';
await nextUpdate(select);
select.focus();
select.click();
await nextRender();
await sendKeys({ press: 'ArrowUp' });
await sendKeys({ press: 'Enter' });
await nextUpdate(select);
expect(select.invalid).to.be.true;
});
});
describe('initial', () => {
let validateSpy;
beforeEach(() => {
select = document.createElement('vaadin-select');
validateSpy = sinon.spy(select, 'validate');
});
afterEach(() => {
select.remove();
});
it('should not validate by default', async () => {
document.body.appendChild(select);
await nextRender();
expect(validateSpy.called).to.be.false;
});
it('should not validate when the field has an initial value', async () => {
select.value = 'value';
document.body.appendChild(select);
await nextRender();
expect(validateSpy.called).to.be.false;
});
it('should not validate when the field has an initial value and invalid', async () => {
select.value = 'value';
select.invalid = true;
document.body.appendChild(select);
await nextRender();
expect(validateSpy.called).to.be.false;
});
});
});