|
| 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