Skip to content

Commit 1a82ceb

Browse files
authored
test: reorganize and cleanup validation tests (#4195)
1 parent 12065c0 commit 1a82ceb

2 files changed

Lines changed: 126 additions & 115 deletions

File tree

‎packages/select/test/select.test.js‎

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
keyboardEventFor,
1212
keyDownChar,
1313
nextFrame,
14-
nextRender,
1514
spaceKeyDown,
1615
tab,
1716
} from '@vaadin/testing-helpers';
@@ -538,86 +537,6 @@ describe('vaadin-select', () => {
538537
});
539538
});
540539

541-
describe('validation', () => {
542-
it('should pass the validation when the field is valid', () => {
543-
select.validate();
544-
expect(select.checkValidity()).to.be.true;
545-
expect(select.invalid).to.be.false;
546-
});
547-
548-
it('should not pass the validation when the field is required and has no value', () => {
549-
expect(select.invalid).to.be.false;
550-
select.setAttribute('required', '');
551-
552-
enterKeyDown(valueButton);
553-
escKeyDown(valueButton);
554-
expect(select.checkValidity()).to.be.false;
555-
expect(select.invalid).to.be.true;
556-
});
557-
558-
it('should validate when closing the overlay', () => {
559-
const spy = sinon.spy();
560-
select.validate = spy;
561-
select.opened = true;
562-
563-
select.opened = false;
564-
expect(spy.called).to.be.true;
565-
});
566-
567-
it('should validate when blurring', () => {
568-
const spy = sinon.spy();
569-
select.validate = spy;
570-
select.blur();
571-
572-
expect(spy.called).to.be.true;
573-
});
574-
575-
it('should validate when setting value', () => {
576-
const spy = sinon.spy();
577-
select.validate = spy;
578-
select.value = 'v2';
579-
expect(spy.callCount).to.be.equal(1);
580-
select.value = '';
581-
expect(spy.callCount).to.be.equal(2);
582-
});
583-
584-
it('should fire a validated event on validation success', () => {
585-
const validatedSpy = sinon.spy();
586-
select.addEventListener('validated', validatedSpy);
587-
select.validate();
588-
589-
expect(validatedSpy.calledOnce).to.be.true;
590-
const event = validatedSpy.firstCall.args[0];
591-
expect(event.detail.valid).to.be.true;
592-
});
593-
594-
it('should fire a validated event on validation failure', () => {
595-
const validatedSpy = sinon.spy();
596-
select.addEventListener('validated', validatedSpy);
597-
select.required = true;
598-
select.validate();
599-
600-
expect(validatedSpy.calledOnce).to.be.true;
601-
const event = validatedSpy.firstCall.args[0];
602-
expect(event.detail.valid).to.be.false;
603-
});
604-
});
605-
606-
describe('initial validation', () => {
607-
let spy;
608-
609-
beforeEach(async () => {
610-
select.required = true;
611-
spy = sinon.spy();
612-
select.validate = spy;
613-
await nextFrame();
614-
});
615-
616-
it('should not validate the initial empty value', () => {
617-
expect(spy.called).to.be.false;
618-
});
619-
});
620-
621540
describe('change event', () => {
622541
let menu, changeSpy;
623542

@@ -672,40 +591,6 @@ describe('vaadin-select', () => {
672591
});
673592
});
674593

675-
describe('initial validation', () => {
676-
let validateSpy;
677-
678-
beforeEach(() => {
679-
select = document.createElement('vaadin-select');
680-
validateSpy = sinon.spy(select, 'validate');
681-
});
682-
683-
afterEach(() => {
684-
select.remove();
685-
});
686-
687-
it('should not validate by default', async () => {
688-
document.body.appendChild(select);
689-
await nextRender();
690-
expect(validateSpy.called).to.be.false;
691-
});
692-
693-
it('should not validate when the field has an initial value', async () => {
694-
select.value = 'value';
695-
document.body.appendChild(select);
696-
await nextRender();
697-
expect(validateSpy.called).to.be.false;
698-
});
699-
700-
it('should not validate when the field has an initial value and invalid', async () => {
701-
select.value = 'value';
702-
select.invalid = true;
703-
document.body.appendChild(select);
704-
await nextRender();
705-
expect(validateSpy.called).to.be.false;
706-
});
707-
});
708-
709594
describe('with value', () => {
710595
let menu;
711596

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { enterKeyDown, escKeyDown, fixtureSync, nextRender } from '@vaadin/testing-helpers';
3+
import sinon from 'sinon';
4+
import '@vaadin/item/vaadin-item.js';
5+
import '@vaadin/list-box/vaadin-list-box.js';
6+
import './not-animated-styles.js';
7+
import '../vaadin-select.js';
8+
import { html, render } from 'lit';
9+
10+
describe('validation', () => {
11+
let select, valueButton;
12+
13+
describe('basic', () => {
14+
beforeEach(async () => {
15+
select = fixtureSync('<vaadin-select></vaadin-select>');
16+
select.renderer = (root) => {
17+
render(
18+
html`
19+
<vaadin-list-box>
20+
<vaadin-item value="v1" label="o2">Option 2</vaadin-item>
21+
<vaadin-item value="v2" label="o2">Option 2</vaadin-item>
22+
<vaadin-item value="">Option 3</vaadin-item>
23+
</vaadin-list-box>
24+
`,
25+
root,
26+
);
27+
};
28+
valueButton = select._valueButton;
29+
await nextRender();
30+
});
31+
32+
it('should pass the validation when the field is valid', () => {
33+
select.validate();
34+
expect(select.checkValidity()).to.be.true;
35+
expect(select.invalid).to.be.false;
36+
});
37+
38+
it('should not pass the validation when the field is required and has no value', () => {
39+
select.required = true;
40+
41+
enterKeyDown(valueButton);
42+
escKeyDown(valueButton);
43+
expect(select.checkValidity()).to.be.false;
44+
expect(select.invalid).to.be.true;
45+
});
46+
47+
it('should validate when closing the overlay', () => {
48+
const spy = sinon.spy(select, 'validate');
49+
select.opened = true;
50+
51+
select.opened = false;
52+
expect(spy.called).to.be.true;
53+
});
54+
55+
it('should validate on programmatic blur', () => {
56+
const spy = sinon.spy(select, 'validate');
57+
select.blur();
58+
59+
expect(spy.called).to.be.true;
60+
});
61+
62+
it('should validate when setting value property', () => {
63+
const spy = sinon.spy(select, 'validate');
64+
select.value = 'v2';
65+
expect(spy.callCount).to.be.equal(1);
66+
67+
select.value = '';
68+
expect(spy.callCount).to.be.equal(2);
69+
});
70+
71+
it('should fire a validated event on validation success', () => {
72+
const validatedSpy = sinon.spy();
73+
select.addEventListener('validated', validatedSpy);
74+
select.validate();
75+
76+
expect(validatedSpy.calledOnce).to.be.true;
77+
const event = validatedSpy.firstCall.args[0];
78+
expect(event.detail.valid).to.be.true;
79+
});
80+
81+
it('should fire a validated event on validation failure', () => {
82+
const validatedSpy = sinon.spy();
83+
select.addEventListener('validated', validatedSpy);
84+
select.required = true;
85+
select.validate();
86+
87+
expect(validatedSpy.calledOnce).to.be.true;
88+
const event = validatedSpy.firstCall.args[0];
89+
expect(event.detail.valid).to.be.false;
90+
});
91+
});
92+
93+
describe('initial', () => {
94+
let validateSpy;
95+
96+
beforeEach(() => {
97+
select = document.createElement('vaadin-select');
98+
validateSpy = sinon.spy(select, 'validate');
99+
});
100+
101+
afterEach(() => {
102+
select.remove();
103+
});
104+
105+
it('should not validate by default', async () => {
106+
document.body.appendChild(select);
107+
await nextRender();
108+
expect(validateSpy.called).to.be.false;
109+
});
110+
111+
it('should not validate when the field has an initial value', async () => {
112+
select.value = 'value';
113+
document.body.appendChild(select);
114+
await nextRender();
115+
expect(validateSpy.called).to.be.false;
116+
});
117+
118+
it('should not validate when the field has an initial value and invalid', async () => {
119+
select.value = 'value';
120+
select.invalid = true;
121+
document.body.appendChild(select);
122+
await nextRender();
123+
expect(validateSpy.called).to.be.false;
124+
});
125+
});
126+
});

0 commit comments

Comments
 (0)