@@ -8,6 +8,13 @@ import { FieldStatePropsRecord, FieldStateProps } from "../../src/contracts/fiel
8
8
import { MyFieldProps } from "../test-components/test-field" ;
9
9
10
10
describe ( "Form store" , ( ) => {
11
+ const formId = "form-id" ;
12
+ let formStore : FormStore ;
13
+
14
+ beforeEach ( ( ) => {
15
+ formStore = new FormStore ( formId ) ;
16
+ } ) ;
17
+
11
18
it ( "returns state" , ( ) => {
12
19
const formId = "FORM-ID" ;
13
20
const formStore = new FormStore ( formId ) ;
@@ -16,10 +23,8 @@ describe("Form store", () => {
16
23
} ) ;
17
24
18
25
it ( "returns fieldId from fieldName and fieldGroupId" , ( ) => {
19
- const formId = "FORM-ID" ;
20
26
const fieldName = "FIELD-NAME" ;
21
27
const fieldGroupId = "FIELD-GROUP-ID" ;
22
- const formStore = new FormStore ( formId ) ;
23
28
24
29
const fieldId = formStore . GetFieldId ( fieldName , fieldGroupId ) ;
25
30
@@ -29,11 +34,9 @@ describe("Form store", () => {
29
34
} ) ;
30
35
31
36
it ( "registers a field" , ( ) => {
32
- const formId = "FORM-ID" ;
33
37
const fieldId = "FIELD-ID" ;
34
38
const initialValue = "INITIAL-VALUE" ;
35
39
const defaultValue = "DEFAULT-VALUE" ;
36
- const formStore = new FormStore ( formId ) ;
37
40
38
41
formStore . RegisterField ( fieldId , initialValue , defaultValue ) ;
39
42
expect ( formStore . HasField ( fieldId ) ) . toBe ( true ) ;
@@ -43,11 +46,9 @@ describe("Form store", () => {
43
46
} ) ;
44
47
45
48
it ( "unregisters a field" , ( ) => {
46
- const formId = "FORM-ID" ;
47
49
const fieldId = "FIELD-ID" ;
48
50
const initialValue = "INITIAL-VALUE" ;
49
51
const defaultValue = "DEFAULT-VALUE" ;
50
- const formStore = new FormStore ( formId ) ;
51
52
52
53
formStore . RegisterField ( fieldId , initialValue , defaultValue ) ;
53
54
formStore . UnregisterField ( fieldId ) ;
@@ -56,36 +57,30 @@ describe("Form store", () => {
56
57
} ) ;
57
58
58
59
it ( "has a field" , ( ) => {
59
- const formId = "FORM-ID" ;
60
60
const fieldId = "FIELD-ID" ;
61
61
const initialValue = "INITIAL-VALUE" ;
62
62
const defaultValue = "DEFAULT-VALUE" ;
63
- const formStore = new FormStore ( formId ) ;
64
63
65
64
expect ( formStore . HasField ( fieldId ) ) . toBe ( false ) ;
66
65
formStore . RegisterField ( fieldId , initialValue , defaultValue ) ;
67
66
expect ( formStore . HasField ( fieldId ) ) . toBe ( true ) ;
68
67
} ) ;
69
68
70
69
it ( "get a field" , ( ) => {
71
- const formId = "FORM-ID" ;
72
70
const fieldId = "FIELD-ID" ;
73
71
const initialValue = "INITIAL-VALUE" ;
74
72
const defaultValue = "DEFAULT-VALUE" ;
75
- const formStore = new FormStore ( formId ) ;
76
73
77
74
expect ( formStore . GetField ( fieldId ) ) . toBeUndefined ( ) ;
78
75
formStore . RegisterField ( fieldId , initialValue , defaultValue ) ;
79
76
expect ( formStore . GetField ( fieldId ) ) . not . toBeUndefined ( ) ;
80
77
} ) ;
81
78
82
79
it ( "value changed" , ( ) => {
83
- const formId = "FORM-ID" ;
84
80
const fieldId = "FIELD-ID" ;
85
81
const initialValue = "INITIAL-VALUE" ;
86
82
const defaultValue = "DEFAULT-VALUE" ;
87
83
const nextValue = "NEXT-VALUE" ;
88
- const formStore = new FormStore ( formId ) ;
89
84
90
85
formStore . RegisterField ( fieldId , initialValue , defaultValue ) ;
91
86
expect ( formStore . GetField ( fieldId ) . Value ) . toBe ( initialValue ) ;
@@ -94,11 +89,9 @@ describe("Form store", () => {
94
89
} ) ;
95
90
96
91
it ( "validate field without error" , async ( done ) => {
97
- const formId = "FORM-ID" ;
98
92
const fieldId = "FIELD-ID" ;
99
93
const initialValue = "INITIAL-VALUE" ;
100
94
const defaultValue = "DEFAULT-VALUE" ;
101
- const formStore = new FormStore ( formId ) ;
102
95
103
96
formStore . RegisterField ( fieldId , initialValue , defaultValue ) ;
104
97
const validationPromise = new Promise < never > ( ( resolve , reject ) => {
@@ -124,11 +117,9 @@ describe("Form store", () => {
124
117
} ) ;
125
118
126
119
it ( "validate field with error" , async ( done ) => {
127
- const formId = "FORM-ID" ;
128
120
const fieldId = "FIELD-ID" ;
129
121
const initialValue = "INITIAL-VALUE" ;
130
122
const defaultValue = "DEFAULT-VALUE" ;
131
- const formStore = new FormStore ( formId ) ;
132
123
const formError : FormError = { Message : "Error Message" } ;
133
124
134
125
formStore . RegisterField ( fieldId , initialValue , defaultValue ) ;
@@ -166,11 +157,9 @@ describe("Form store", () => {
166
157
} ) ;
167
158
168
159
it ( "skip validation when newValue has expired" , async ( done ) => {
169
- const formId = "FORM-ID" ;
170
160
const fieldId = "FIELD-ID" ;
171
161
const initialValue = "INITIAL-VALUE" ;
172
162
const defaultValue = "DEFAULT-VALUE" ;
173
- const formStore = new FormStore ( formId ) ;
174
163
const formError = "field error" ;
175
164
176
165
formStore . RegisterField ( fieldId , initialValue , defaultValue ) ;
@@ -210,7 +199,6 @@ describe("Form store", () => {
210
199
} ) ;
211
200
212
201
it ( "registers field with props" , ( ) => {
213
- const formId = "FORM-ID" ;
214
202
const fieldId = "FIELD-ID" ;
215
203
const defaultValue = "DEFAULT-VALUE" ;
216
204
const fieldProps : MyFieldProps = {
@@ -219,7 +207,6 @@ describe("Form store", () => {
219
207
defaultValue : defaultValue ,
220
208
randomKey : "random value"
221
209
} ;
222
- const formStore = new FormStore ( formId ) ;
223
210
224
211
formStore . RegisterField ( fieldId , fieldProps . value , fieldProps . defaultValue , fieldProps ) ;
225
212
@@ -230,7 +217,6 @@ describe("Form store", () => {
230
217
} ) ;
231
218
232
219
it ( "updates field props" , ( ) => {
233
- const formId = "FORM-ID" ;
234
220
const fieldId = "FIELD-ID" ;
235
221
const defaultValue = "DEFAULT-VALUE" ;
236
222
const fieldProps : MyFieldProps = {
@@ -246,12 +232,122 @@ describe("Form store", () => {
246
232
value : "Updated value"
247
233
} ;
248
234
const fieldPropsNextRecord = recordify < FieldStateProps , FieldStatePropsRecord > ( fieldPropsNext ) ;
249
- const formStore = new FormStore ( formId ) ;
250
235
251
236
formStore . RegisterField ( fieldId , fieldProps . value , fieldProps . defaultValue , fieldProps ) ;
252
237
formStore . UpdateProps ( fieldId , fieldPropsNext ) ;
253
238
254
239
// Deep-check the updated props
255
240
expect ( Immutable . is ( formStore . GetField ( fieldId ) . Props , fieldPropsNextRecord ) ) . toBe ( true ) ;
256
241
} ) ;
242
+
243
+ it ( "clears all fields values" , ( ) => {
244
+ let fieldsIds : string [ ] = [ ] ;
245
+ for ( let i = 0 ; i < 5 ; i ++ ) {
246
+ fieldsIds . push ( `field-id-${ i } ` ) ;
247
+ }
248
+
249
+ const defaultValue = "default value" ;
250
+ const fieldProps : MyFieldProps = {
251
+ name : "field-name" ,
252
+ value : "initial value" ,
253
+ defaultValue : defaultValue ,
254
+ randomKey : "random value"
255
+ } ;
256
+
257
+ for ( const fieldId of fieldsIds ) {
258
+ formStore . RegisterField ( fieldId , fieldProps . value , fieldProps . defaultValue , fieldProps ) ;
259
+ }
260
+ formStore . ClearFields ( ) ;
261
+
262
+ for ( const fieldId of fieldsIds ) {
263
+ const fieldState = formStore . GetField ( fieldId ) ;
264
+ expect ( fieldState . Value ) . toBe ( defaultValue ) ;
265
+ }
266
+ } ) ;
267
+
268
+ it ( "clears fields values by fieldsIds" , ( ) => {
269
+ let fieldsIds : string [ ] = [ ] ;
270
+ for ( let i = 0 ; i < 5 ; i ++ ) {
271
+ fieldsIds . push ( `field-id-${ i } ` ) ;
272
+ }
273
+ const clearedFieldId = fieldsIds [ 0 ] ;
274
+ const defaultValue = "default value" ;
275
+ const fieldProps : MyFieldProps = {
276
+ name : "field-name" ,
277
+ value : "initial value" ,
278
+ defaultValue : defaultValue ,
279
+ randomKey : "random value"
280
+ } ;
281
+
282
+ for ( const fieldId of fieldsIds ) {
283
+ formStore . RegisterField ( fieldId , fieldProps . value , fieldProps . defaultValue , fieldProps ) ;
284
+ }
285
+ formStore . ClearFields ( [ clearedFieldId ] ) ;
286
+
287
+ for ( const fieldId of fieldsIds ) {
288
+ const fieldState = formStore . GetField ( fieldId ) ;
289
+ if ( fieldId === clearedFieldId ) {
290
+ expect ( fieldState . Value ) . toBe ( defaultValue ) ;
291
+ } else {
292
+ expect ( fieldState . Value ) . not . toBe ( defaultValue ) ;
293
+ }
294
+ }
295
+ } ) ;
296
+
297
+ it ( "resets all fields values" , ( ) => {
298
+ let fieldsIds : string [ ] = [ ] ;
299
+ for ( let i = 0 ; i < 5 ; i ++ ) {
300
+ fieldsIds . push ( `field-id-${ i } ` ) ;
301
+ }
302
+ const initialValue = "initial value" ;
303
+ const nextValue = "next value" ;
304
+ const fieldProps : MyFieldProps = {
305
+ name : "field-name" ,
306
+ value : initialValue ,
307
+ defaultValue : "default value" ,
308
+ randomKey : "random value"
309
+ } ;
310
+
311
+ for ( const fieldId of fieldsIds ) {
312
+ formStore . RegisterField ( fieldId , fieldProps . value , fieldProps . defaultValue , fieldProps ) ;
313
+ formStore . ValueChanged ( fieldId , nextValue ) ;
314
+ }
315
+ formStore . ResetFields ( ) ;
316
+
317
+ for ( const fieldId of fieldsIds ) {
318
+ const fieldState = formStore . GetField ( fieldId ) ;
319
+ expect ( fieldState . Value ) . toBe ( initialValue ) ;
320
+ }
321
+ } ) ;
322
+
323
+ it ( "resets fields values by fieldsIds" , ( ) => {
324
+ let fieldsIds : string [ ] = [ ] ;
325
+ for ( let i = 0 ; i < 5 ; i ++ ) {
326
+ fieldsIds . push ( `field-id-${ i } ` ) ;
327
+ }
328
+ const resetFieldId = fieldsIds [ 0 ] ;
329
+ const initialValue = "initial value" ;
330
+ const nextValue = "next value" ;
331
+ const fieldProps : MyFieldProps = {
332
+ name : "field-name" ,
333
+ value : initialValue ,
334
+ defaultValue : "default value" ,
335
+ randomKey : "random value"
336
+ } ;
337
+
338
+ for ( const fieldId of fieldsIds ) {
339
+ formStore . RegisterField ( fieldId , fieldProps . value , fieldProps . defaultValue , fieldProps ) ;
340
+ formStore . ValueChanged ( fieldId , nextValue ) ;
341
+ }
342
+ formStore . ResetFields ( [ resetFieldId ] ) ;
343
+
344
+ for ( const fieldId of fieldsIds ) {
345
+ const fieldState = formStore . GetField ( fieldId ) ;
346
+ if ( fieldId === resetFieldId ) {
347
+ expect ( fieldState . Value ) . toBe ( initialValue ) ;
348
+ } else {
349
+ expect ( fieldState . Value ) . toBe ( nextValue ) ;
350
+ }
351
+ }
352
+ } ) ;
257
353
} ) ;
0 commit comments