1
- //import chai from "chai";
2
1
import { expect } from "chai" ;
3
2
4
- //import sinon from "sinon";
5
- //import sinonChai from "sinon-chai";
6
- //chai.use(sinonChai);
7
-
8
3
import Vue from "vue" ;
4
+ import VueFormGenerator from "src/index" ;
9
5
import AbstractField from "src/fields/abstractField" ;
10
6
11
7
Vue . component ( "AbstractField" , AbstractField ) ;
12
8
13
9
let el , vm , field ;
14
10
15
- function createField ( schema = { } , model = null , disabled = false ) {
11
+ function createField ( schema = { } , model = null , disabled = false , options ) {
16
12
el = document . createElement ( "div" ) ;
17
13
el . innerHTML = `<abstract-field :schema="schema" :model="model" :disabled="disabled" v-ref:field></abstract-field>` ;
18
14
vm = new Vue ( {
19
15
el : el ,
20
16
data : {
21
17
schema,
22
18
model,
23
- disabled
19
+ disabled,
20
+ options
24
21
}
25
22
} ) ;
26
23
@@ -56,32 +53,236 @@ describe("abstractField", () => {
56
53
57
54
} ) ;
58
55
59
- /* describe("check value as get/set function", () => {
56
+ describe ( "check value as get/set function" , ( ) => {
60
57
let schema = {
61
58
type : "text" ,
62
59
label : "Name" ,
63
60
model : "name" ,
64
- get(model) {
65
- return "John Smith"
66
- },
67
- set: sinon.spy()
61
+ get : sinon . stub ( ) . returns ( "John Smith" ) ,
62
+ set : sinon . stub ( )
68
63
} ;
69
64
let model = { } ;
70
65
71
66
beforeEach ( ( ) => {
72
67
createField ( schema , model ) ;
73
68
} ) ;
74
69
75
- it("should give the model static value ", () => {
70
+ it ( "should be called the schema.get function " , ( ) => {
76
71
expect ( field ) . to . be . exist ;
77
- expect(field.value).to.be.equal("John Doe");
72
+ field . schema . get . reset ( ) ;
73
+ expect ( field . value ) . to . be . equal ( "John Smith" ) ;
74
+ expect ( field . schema . get . calledOnce ) . to . be . true ;
78
75
} ) ;
79
76
80
77
it ( "should set new value to model if value changed" , ( ) => {
78
+ field . schema . set . reset ( ) ;
79
+ field . value = "John Roe" ;
80
+ expect ( field . schema . set . calledOnce ) . to . be . true ;
81
+ expect ( field . schema . set . calledWith ( model , "John Roe" ) ) . to . be . true ;
82
+ } ) ;
83
+
84
+ } ) ;
85
+
86
+ describe ( "check formatValueToField & formatValueToModel function" , ( ) => {
87
+ let schema = {
88
+ type : "text" ,
89
+ label : "Name" ,
90
+ model : "name"
91
+ } ;
92
+ let model = { name : "John Doe" } ;
93
+
94
+ beforeEach ( ( ) => {
95
+ createField ( schema , model ) ;
96
+ field . formatValueToField = function ( value ) {
97
+ return "**" + value + "**" ;
98
+ } ;
99
+
100
+ field . formatValueToModel = function ( value ) {
101
+ return "!!" + value + "!!" ;
102
+ } ;
103
+ } ) ;
104
+
105
+ it ( "should return the formatted value" , ( ) => {
106
+ expect ( field . value ) . to . be . equal ( "**John Doe**" ) ;
107
+ } ) ;
108
+
109
+ it ( "should set the formatted value to model" , ( ) => {
81
110
field . value = "Foo Bar" ;
82
- expect(model.name).to.be.equal("Foo Bar");
111
+ expect ( model . name ) . to . be . equal ( "!!Foo Bar!!" ) ;
112
+ } ) ;
113
+
114
+ } ) ;
115
+
116
+ describe ( "check schema onChanged event" , ( ) => {
117
+ let schema = {
118
+ type : "text" ,
119
+ label : "Name" ,
120
+ model : "name" ,
121
+ onChanged : sinon . spy ( )
122
+ } ;
123
+ let model = { name : "John Doe" } ;
124
+
125
+ beforeEach ( ( ) => {
126
+ createField ( schema , model ) ;
127
+ } ) ;
128
+
129
+ it ( "should called once the schema.onChanged" , ( done ) => {
130
+ schema . onChanged . reset ( ) ;
131
+ model . name = "Jane Doe" ;
132
+ vm . $nextTick ( ( ) => {
133
+ expect ( schema . onChanged . calledOnce ) . to . be . true ;
134
+ expect ( schema . onChanged . calledWith ( model , "Jane Doe" , "John Doe" , schema ) ) . to . be . true ;
135
+ done ( ) ;
136
+ } )
137
+ } ) ;
138
+
139
+ } ) ;
140
+
141
+ describe ( "check validateAfterChanged option" , ( ) => {
142
+ let schema = {
143
+ type : "text" ,
144
+ label : "Name" ,
145
+ model : "name"
146
+ } ;
147
+
148
+ let model = { name : "John Doe" } ;
149
+ let options = {
150
+ validateAfterChanged : false
151
+ }
152
+
153
+ beforeEach ( ( ) => {
154
+ createField ( schema , model , false , options ) ;
155
+ field . validate = sinon . spy ( ) ;
156
+ } ) ;
157
+
158
+ it ( "should not call validate function after value changed" , ( done ) => {
159
+ model . name = "Jane Doe" ;
160
+ vm . $nextTick ( ( ) => {
161
+ expect ( field . validate . callCount ) . to . be . equal ( 0 ) ;
162
+ done ( ) ;
163
+ } ) ;
164
+ } ) ;
165
+
166
+ it ( "should not call validate function after value changed" , ( done ) => {
167
+ options . validateAfterChanged = true ;
168
+ model . name = "Jane Roe" ;
169
+ vm . $nextTick ( ( ) => {
170
+ expect ( field . validate . callCount ) . to . be . equal ( 1 ) ;
171
+ done ( ) ;
172
+ } ) ;
173
+ } ) ;
174
+
175
+ } ) ;
176
+
177
+ describe ( "check validate function with one validator" , ( ) => {
178
+ let schema = {
179
+ type : "text" ,
180
+ label : "Name" ,
181
+ model : "name" ,
182
+ validator : sinon . spy ( )
183
+ } ;
184
+
185
+ let model = { name : "John Doe" } ;
186
+
187
+ beforeEach ( ( ) => {
188
+ createField ( schema , model ) ;
189
+ } ) ;
190
+
191
+ it ( "should call schema validator" , ( ) => {
192
+ schema . validator . reset ( ) ;
193
+ field . validate ( ) ;
194
+ expect ( schema . validator . calledOnce ) . to . be . true ;
195
+ expect ( schema . validator . calledWith ( field . value , schema , model ) ) . to . be . true ;
196
+ } ) ;
197
+
198
+ } ) ;
199
+
200
+ describe ( "check validate function with validator array" , ( ) => {
201
+ let spy1 = sinon . spy ( ) ;
202
+ let spy2 = sinon . spy ( ) ;
203
+ let schema = {
204
+ type : "text" ,
205
+ label : "Name" ,
206
+ model : "name" ,
207
+ validator : [ spy1 , spy2 ]
208
+ } ;
209
+
210
+ let model = { name : "John Doe" } ;
211
+
212
+ beforeEach ( ( ) => {
213
+ createField ( schema , model ) ;
214
+ } ) ;
215
+
216
+ it ( "should call schema validator" , ( ) => {
217
+ spy1 . reset ( ) ;
218
+ spy2 . reset ( ) ;
219
+ field . validate ( ) ;
220
+
221
+ expect ( spy1 . calledOnce ) . to . be . true ;
222
+ expect ( spy1 . calledWith ( field . value , schema , model ) ) . to . be . true ;
223
+
224
+ expect ( spy2 . calledOnce ) . to . be . true ;
225
+ expect ( spy2 . calledWith ( field . value , schema , model ) ) . to . be . true ;
226
+ } ) ;
227
+
228
+ } ) ;
229
+
230
+ describe ( "check schema onValidated event" , ( ) => {
231
+ let schema = {
232
+ type : "text" ,
233
+ label : "Name" ,
234
+ model : "name" ,
235
+ validator : sinon . stub ( ) . returns ( "Validation error!" ) ,
236
+ onValidated : sinon . spy ( )
237
+ } ;
238
+ let model = { name : "John Doe" } ;
239
+
240
+ beforeEach ( ( ) => {
241
+ createField ( schema , model ) ;
242
+ } ) ;
243
+
244
+ it ( "should called once the schema.onValidated" , ( ) => {
245
+ schema . onValidated . reset ( ) ;
246
+ let res = field . validate ( ) ;
247
+ expect ( res ) . to . be . an . array ;
248
+ expect ( res . length ) . to . be . equal ( 1 ) ;
249
+ expect ( res [ 0 ] ) . to . be . equal ( "Validation error!" ) ;
250
+
251
+ expect ( schema . onValidated . calledOnce ) . to . be . true ;
252
+ expect ( schema . onValidated . calledWith ( model , field . schema . errors , schema ) ) . to . be . true ;
253
+ } ) ;
254
+
255
+ } ) ;
256
+
257
+ describe ( "check clearValidationErrors" , ( ) => {
258
+ let schema = {
259
+ type : "text" ,
260
+ label : "Name" ,
261
+ model : "name" ,
262
+ validator : sinon . stub ( ) . returns ( "Validation error!" )
263
+ } ;
264
+ let model = { name : "John Doe" } ;
265
+
266
+ before ( ( ) => {
267
+ createField ( schema , model ) ;
268
+ } ) ;
269
+
270
+ it ( "should be undefined" , ( ) => {
271
+ expect ( schema . errors ) . to . be . undefined ;
272
+ } ) ;
273
+
274
+ it ( "should be an empty array" , ( ) => {
275
+ field . clearValidationErrors ( ) ;
276
+ expect ( schema . errors ) . to . be . defined ;
277
+ expect ( schema . errors ) . to . be . length ( 0 ) ;
278
+ } ) ;
279
+
280
+ it ( "should contain one error string" , ( ) => {
281
+ field . validate ( ) ;
282
+ expect ( schema . errors ) . to . be . length ( 1 ) ;
283
+ expect ( schema . errors [ 0 ] ) . to . be . equal ( "Validation error!" ) ;
83
284
} ) ;
84
285
85
- });*/
286
+ } ) ;
86
287
87
288
} ) ;
0 commit comments