1- import { mount } from "@vue/test-utils" ;
2- import EmptyApp from "./components/EmptyApp.vue" ;
3- import Form from "../../src/classes/Form" ;
1+ import { DOMWrapper , mount , VueWrapper } from "@vue/test-utils" ;
2+ import EmptyApp from "../components/EmptyApp.vue" ;
43import { defineComponent , h } from "vue" ;
5- import { InputField } from "../../src/index" ;
6- import wait from "../wait" ;
7- import { FormFieldValidationCallback } from "@/types" ;
8- import STORE from "../../src/config/store" ;
9- import AppInputTextPretty from "./components/input-text/AppInputTextPretty.vue"
4+ import { InputField , STORE , Form , FormField } from "../../../src/index" ;
5+ import { FormFieldValidationCallback } from "../../../src/types" ;
6+ import AppInputTextPretty from "../components/input-text/AppInputTextPretty.vue"
7+
8+ const name = 'username'
9+ function defineTextComponent ( ) {
10+ return defineComponent ( {
11+ template : `<div><form-field type = "text" name = "${ name } " required label = "Username" /></div>` ,
12+ components : { FormField}
13+ } )
14+ }
15+ function defaultMount ( component = defineTextComponent ( ) ) {
16+ return mount ( EmptyApp , {
17+ slots : {
18+ default : component
19+ } ,
20+ attachTo : document . body
21+ } )
22+ }
1023
1124describe ( "Input text" , ( ) => {
25+ let app : VueWrapper < any > ;
26+ let form : Form
27+ let input : DOMWrapper < HTMLInputElement >
28+ beforeEach ( ( ) => {
29+ app = defaultMount ( )
30+ form = ( app . vm as any ) . form
31+ input = app . find ( 'input' )
32+ } )
33+
1234 test ( "Default empty input-text" , async ( ) => {
13- const component = defineComponent ( {
14- template : `<input-field name = "username"/>` ,
15- components : { InputField}
16- } )
17- const app = mount ( EmptyApp , {
18- slots : {
19- default : component
20- } ,
21- } ) ;
22-
23- await wait ( )
24-
25- const input = app . get ( 'input' ) ;
2635 expect ( input . element . value ) . toBe ( "" )
2736 } )
2837 test ( "Input-text by default should take value from form" , async ( ) => {
29- const component = defineComponent ( {
30- template : `<input-field name = "username"/>` ,
31- components : { InputField}
32- } )
33- const app = mount ( EmptyApp , {
34- slots : {
35- default : component
36- } ,
37- } ) ;
38- const form = ( app . vm as any ) . form as Form ;
3938 form . setValues ( {
4039 username : "Jack"
4140 } )
42-
43- await wait ( )
44-
45- const input = app . get ( 'input' ) ;
41+ await app . vm . $nextTick ( )
4642 expect ( input . element . value ) . toBe ( "Jack" )
4743 } )
4844 test ( "Input-text be reactive input value" , async ( ) => {
49- const component = defineComponent ( {
50- template : `<input-field name = "username"/>` ,
51- components : { InputField}
52- } )
53- const app = mount ( EmptyApp , {
54- slots : {
55- default : component
56- } ,
57- } ) ;
58- const form = ( app . vm as any ) . form as Form ;
5945 form . setValues ( {
6046 username : "Jack"
6147 } )
6248 const input = app . get ( 'input' ) ;
63- await wait ( )
49+ await app . vm . $nextTick ( )
6450 expect ( input . element . value ) . toBe ( "Jack" ) ;
6551 form . change ( {
6652 username : "TTT"
6753 } )
68- await wait ( )
54+ await app . vm . $nextTick ( )
6955 expect ( input . element . value ) . toBe ( "TTT" ) ;
7056 form . setValues ( {
7157 username : "Jenesius"
7258 } )
73- await wait ( )
59+ await app . vm . $nextTick ( )
7460 expect ( input . element . value ) . toBe ( "Jenesius" )
7561 } )
7662 test ( "Input-text should update form after entering data" , async ( ) => {
77- const component = defineComponent ( {
78- template : `<input-field name = "username"/>` ,
79- components : { InputField}
80- } )
81- const app = mount ( EmptyApp , {
82- slots : {
83- default : component
84- } ,
85- } ) ;
86- const form = ( app . vm as any ) . form as Form ;
87-
88- await wait ( )
63+ await app . vm . $nextTick ( )
8964
9065 const input = app . get ( 'input' ) ;
9166 await input . setValue ( "TEST" )
9267 expect ( input . element . value ) . toBe ( "TEST" )
9368 expect ( form . getValueByName ( "username" ) ) . toBe ( "TEST" )
9469 } )
9570 test ( "By default input should be not disable" , ( ) => {
96- const component = defineComponent ( {
97- template : `<input-field name = "username"/>` ,
98- components : { InputField}
99- } )
100- const app = mount ( EmptyApp , {
101- slots : {
102- default : component
103- } ,
104- } ) ;
105- const form = ( app . vm as any ) . form as Form ;
10671 const input = app . get ( 'input' ) ;
10772 expect ( form . checkFieldDisable ( 'username' ) ) . toBe ( false )
10873 expect ( input . element . disabled ) . toBe ( false )
10974 } )
11075 test ( "Input should has disabled class after form.disable" , async ( ) => {
111- const component = defineComponent ( {
112- template : `<div><input-field name = "username"/></div>` ,
113- components : { InputField}
114- } )
115- const app = mount ( EmptyApp , {
116- slots : {
117- default : component
118- } ,
119- } ) ;
120-
121- // update prop, and wait a tick to allow it to take effect
122- // app.vm.loadingResource = true
12376 await app . vm . $nextTick ( )
124-
125- const form = ( app . vm as any ) . form as Form ;
12677 form . disable ( )
127-
128- await wait ( 100 )
129-
130- const input = app . get ( 'input' ) ;
131-
78+ await app . vm . $nextTick ( )
13279 expect ( input . element . disabled ) . toBe ( true )
13380 } )
13481 test ( "Input should remove disable class after form.enable" , async ( ) => {
135- const component = defineComponent ( {
136- template : `<input-field name = "username"/>` ,
137- components : { InputField}
138- } )
139- const app = mount ( EmptyApp , {
140- slots : {
141- default : component
142- } ,
143- } ) ;
144- // update prop, and wait a tick to allow it to take effect
145- // app.vm.loadingResource = true
146- await app . vm . $nextTick ( )
147-
148- const form = ( app . vm as any ) . form as Form ;
149- const input = app . get ( 'input' ) ;
150-
15182 form . disable ( )
15283 await app . vm . $nextTick ( )
15384 expect ( input . element . disabled ) . toBe ( true )
@@ -160,17 +91,13 @@ describe("Input text", () => {
16091 template : `<div><input-field name = "username"/> <input-field name="username"/></div>` ,
16192 components : { InputField}
16293 } )
163- const app = mount ( EmptyApp , {
164- slots : {
165- default : component
166- } ,
167- } ) ;
94+ const app = defaultMount ( component )
16895 const form = ( app . vm as any ) . form as Form ;
16996 form . setValues ( {
17097 username : "T"
17198 } )
172-
173- await wait ( )
99+
100+ await app . vm . $nextTick ( )
174101
175102 const inputs = app . findAll ( 'input' ) ;
176103 expect ( inputs . map ( a => a . element . value ) ) . toEqual ( [ "T" , "T" ] )
@@ -229,7 +156,7 @@ describe("Input text", () => {
229156 const validateResult = form . validate ( )
230157 expect ( validateResult ) . toBe ( false ) ;
231158 await app . vm . $nextTick ( ) ;
232- await wait ( )
159+
233160 expect ( app . find ( '.container-input-text_error' ) . exists ( ) ) . toBe ( true ) ;
234161 expect ( app . text ( ) ) . toBe ( ERROR_TEXT ) ;
235162
@@ -238,7 +165,6 @@ describe("Input text", () => {
238165 } )
239166 form . validate ( )
240167 await app . vm . $nextTick ( )
241- await wait ( )
242168
243169 expect ( app . find ( '.container-input-text_error' ) . exists ( ) ) . toBe ( false ) ;
244170 expect ( app . text ( ) ) . toBe ( "" ) ;
@@ -256,15 +182,15 @@ describe("Input text", () => {
256182 await app . vm . $nextTick ( )
257183
258184 expect ( form . validate ( ) ) . toBe ( false ) ;
259-
260- await wait ( ) ;
185+
186+ await app . vm . $nextTick ( )
261187
262188 expect ( app . text ( ) ) . toBe ( STORE . requiredMessage ) ;
263189 form . setValues ( {
264190 username : "PP"
265191 } )
266192 expect ( form . validate ( ) ) . toBe ( true ) ;
267- await wait ( )
193+ await app . vm . $nextTick ( )
268194 expect ( app . text ( ) ) . toBe ( "" )
269195 } )
270196 test ( "If label is provided it should be visible" , async ( ) => {
@@ -327,19 +253,19 @@ describe("Input text", () => {
327253 const input = app . get ( "input" ) ;
328254 await input . setValue ( "jack" )
329255 await app . vm . $nextTick ( ) ;
330- await wait ( )
256+ await app . vm . $nextTick ( )
331257
332258 expect ( form . getValueByName ( 'name' ) ) . toBe ( "jack" )
333259 expect ( input . element . value ) . toBe ( '-jack-' )
334260
335261
336262 await input . setValue ( "a" )
337- await wait ( )
263+ await app . vm . $nextTick ( )
338264 expect ( form . getValueByName ( 'name' ) ) . toBe ( "a" )
339265 expect ( input . element . value ) . toBe ( "-a-" )
340266
341267 await input . setValue ( "" )
342- await wait ( )
268+ await app . vm . $nextTick ( )
343269 expect ( form . getValueByName ( 'name' ) ) . toBe ( "" )
344270 expect ( input . element . value ) . toBe ( "" )
345271 } )
@@ -369,13 +295,11 @@ describe("Input text", () => {
369295 const form = ( app . vm as any ) . form as Form ;
370296 //app.vm.loadingResource = true;
371297 await app . vm . $nextTick ( )
372- await wait ( )
298+
373299 const input = app . get ( 'input' ) ;
374300
375301 await input . setValue ( "123" ) ;
376302 await app . vm . $nextTick ( )
377- await wait ( )
378-
379303
380304 expect ( input . element . value ) . toBe ( "123" )
381305 expect ( form . getValueByName ( 'username' ) ) . toBe ( "123" )
@@ -402,7 +326,7 @@ describe("Input text", () => {
402326
403327 const input = app . get ( "input" ) ;
404328 await input . setValue ( "12345678910" )
405- await wait ( ) ;
329+ await app . vm . $nextTick ( )
406330
407331 expect ( form . getValueByName ( "test" ) ) . toBe ( "123456" ) ;
408332 expect ( input . element . value ) . toBe ( "123456" )
@@ -454,33 +378,19 @@ describe("Input text", () => {
454378
455379 const input = app . get ( "input" ) ;
456380 await input . setValue ( "Jenesius 24" )
457- await wait ( ) ;
381+ await app . vm . $nextTick ( )
458382
459383 expect ( form . getValueByName ( "age" ) ) . toBe ( 24 ) ;
460384 expect ( input . element . value ) . toBe ( "24" )
461385
462386 } )
463387 test ( "Empty value for undefined value in form" , async ( ) => {
464- const component = defineComponent ( {
465- template : `<div><input-field name = "age"/></div>` ,
466- components : { InputField}
467- } )
468- const app = mount ( EmptyApp , {
469- slots : {
470- default : component
471- }
472- } )
473- // app.vm.loadingResource = true;
474- await app . vm . $nextTick ( ) ;
475- const form = ( app . vm as any ) . form as Form
476388 form . setValues ( {
477- age : undefined
389+ [ name ] : undefined
478390 } )
391+ await app . vm . $nextTick ( )
479392
480- const input = app . get ( "input" ) ;
481- await wait ( ) ;
482-
483- expect ( form . getValueByName ( "age" ) ) . toBe ( undefined ) ;
393+ expect ( form . getValueByName ( name ) ) . toBe ( undefined ) ;
484394 expect ( input . element . value ) . toBe ( "" )
485395
486396 } )
0 commit comments