11import { expect } from '@esm-bundle/chai' ;
2- import { fixtureSync , nextRender } from '@vaadin/testing-helpers' ;
2+ import { fire , fixtureSync , nextRender } from '@vaadin/testing-helpers' ;
33import sinon from 'sinon' ;
44import '../src/vaadin-custom-field.js' ;
5- import { dispatchChange } from './helpers.js' ;
65
76describe ( '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