@@ -19,19 +19,23 @@ function createField2(data, methods) {
1919
2020describe ( "fieldSubmit.vue" , ( ) => {
2121 describe ( "check template" , ( ) => {
22+ let vfg = { } ;
2223 let schema = {
2324 type : "submit" ,
2425 buttonText : "Submit form" ,
2526 inputName : "" ,
2627 validateBeforeSubmit : false ,
27- onSubmit ( ) { } ,
28+ onSubmit : sinon . spy ( ) ,
2829 fieldClasses : [ "applied-class" , "another-class" ]
2930 } ;
31+ let formOptions = {
32+ validateAsync : false
33+ } ;
3034 let model = { name : "John Doe" } ;
3135 let input ;
3236
3337 before ( ( ) => {
34- createField2 ( { schema, model, disabled : false } ) ;
38+ createField2 ( { vfg , schema, formOptions , model, disabled : false } ) ;
3539 input = wrapper . find ( "input" ) ;
3640 } ) ;
3741
@@ -43,43 +47,89 @@ describe("fieldSubmit.vue", () => {
4347 } ) ;
4448
4549 describe ( "valid form" , ( ) => {
46- it . skip ( "should not call validate if validateBeforeSubmit is false" , ( ) => {
47- schema . onSubmit = sinon . spy ( ) ;
48- let cb = sinon . spy ( ) ;
49- wrapper . vm . $parent . validate = cb ;
50+ before ( ( ) => {
51+ vfg . validate = ( ) => true ;
52+ sinon . spy ( vfg , "validate" ) ;
53+ } ) ;
54+
55+ afterEach ( ( ) => {
56+ schema . onSubmit . resetHistory ( ) ;
57+ vfg . validate . resetHistory ( ) ;
58+ } ) ;
59+
60+ it ( "should not call validate but should call onSubmit if validateBeforeSubmit is false" , ( ) => {
61+ input . trigger ( "click" ) ;
5062
51- input . click ( ) ;
52- expect ( cb . called ) . to . be . false ;
63+ expect ( vfg . validate . notCalled ) . to . be . true ;
5364 expect ( schema . onSubmit . calledOnce ) . to . be . true ;
5465 expect ( schema . onSubmit . calledWith ( model , schema ) ) . to . be . true ;
5566 } ) ;
5667
57- it . skip ( "should call validate if validateBeforeSubmit is true" , ( ) => {
68+ it ( "should call validate and onSubmit if validateBeforeSubmit is true" , ( ) => {
5869 schema . validateBeforeSubmit = true ;
59- schema . onSubmit = sinon . spy ( ) ;
60- let cb = sinon . spy ( ) ;
61- wrapper . vm . $parent . validate = cb ;
6270
6371 input . trigger ( "click" ) ;
6472
65- expect ( cb . called ) . to . be . true ;
73+ expect ( vfg . validate . called ) . to . be . true ;
6674 expect ( schema . onSubmit . called ) . to . be . true ;
6775 } ) ;
6876 } ) ;
6977
7078 describe ( "invalid form" , ( ) => {
71- it . skip ( "should not call onSubmit if validateBeforeSubmit is true" , ( ) => {
79+ before ( ( ) => {
80+ vfg . validate = ( ) => false ;
81+ sinon . spy ( vfg , "validate" ) ;
82+ } ) ;
83+
84+ afterEach ( ( ) => {
85+ schema . onSubmit . resetHistory ( ) ;
86+ vfg . validate . resetHistory ( ) ;
87+ } ) ;
88+
89+ it ( "should call validate but should not call onSubmit if validateBeforeSubmit is true" , ( ) => {
7290 schema . validateBeforeSubmit = true ;
91+
92+ input . trigger ( "click" ) ;
93+
94+ expect ( vfg . validate . called ) . to . be . true ;
95+ expect ( schema . onSubmit . notCalled ) . to . be . true ;
96+ } ) ;
97+ } ) ;
98+
99+ describe ( "async validate" , ( ) => {
100+ before ( ( ) => {
101+ formOptions . validateAsync = true ;
102+ vfg . validate = sinon . stub ( ) ;
73103 schema . onSubmit = sinon . spy ( ) ;
74- let cb = sinon . spy ( ( ) => {
75- return [ "an error occurred" ] ;
104+ } ) ;
105+
106+ afterEach ( ( ) => {
107+ vfg . validate . reset ( ) ;
108+ schema . onSubmit . resetHistory ( ) ;
109+ } ) ;
110+
111+ describe ( "valid form" , ( ) => {
112+ it ( "should call validate and onSubmit if validateBeforeSubmit is true" , async function ( ) {
113+ schema . validateBeforeSubmit = true ;
114+ vfg . validate . resolves ( [ ] ) ;
115+
116+ await input . trigger ( "click" ) ;
117+
118+ expect ( vfg . validate . called ) . to . be . true ;
119+ expect ( schema . onSubmit . called ) . to . be . true ;
76120 } ) ;
77- wrapper . vm . $parent . validate = cb ;
121+ } ) ;
78122
79- input . trigger ( "click" ) ;
123+ describe ( "invalid form" , ( ) => {
124+ it ( "should call validate but should not call onSubmit if validateBeforeSubmit is true" , async function ( ) {
125+ schema . validateBeforeSubmit = true ;
126+ vfg . validate . resolves ( [ "Error" ] ) ;
80127
81- expect ( cb . called ) . to . be . true ;
82- expect ( schema . onSubmit . called ) . to . be . true ;
128+ await input . trigger ( "click" ) ;
129+
130+ expect ( vfg . validate . called ) . to . be . true ;
131+ expect ( schema . onSubmit . notCalled ) . to . be . true ;
132+ } ) ;
83133 } ) ;
84134 } ) ;
85135
0 commit comments