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