1
1
import * as React from "react" ;
2
2
import * as ReactDOM from "react-dom" ;
3
3
import { shallow , mount } from "enzyme" ;
4
- import { spy } from "sinon" ;
4
+ import * as Sinon from "sinon" ;
5
5
6
6
import { FormStoresHandler , FSHContainer } from "../../src/stores/form-stores-handler" ;
7
7
import { FormStore } from "../../src/stores/form-store" ;
@@ -10,10 +10,17 @@ import { MyTestField, MyFieldProps } from "../test-components/test-field";
10
10
import { FormChildContext } from "../../src/contracts/form" ;
11
11
12
12
describe ( "Field Base" , ( ) => {
13
+ let sandbox : Sinon . SinonSandbox ;
14
+
13
15
beforeEach ( ( ) => {
16
+ sandbox = Sinon . sandbox . create ( ) ;
14
17
FSHContainer . SetFormStoresHandler ( new FormStoresHandler ( ) , true ) ;
15
18
} ) ;
16
19
20
+ afterEach ( function ( ) {
21
+ sandbox . restore ( ) ;
22
+ } ) ;
23
+
17
24
it ( "works" , ( ) => {
18
25
expect ( true ) . toBe ( true ) ;
19
26
} ) ;
@@ -164,15 +171,15 @@ describe("Field Base", () => {
164
171
const newValue = "NEW_VALUE" ;
165
172
const formId = "FORM_ID" ;
166
173
167
- spy ( FormStore . prototype , "ValueChanged " ) ;
174
+ sandbox . spy ( FormStore . prototype , "UpdateFieldValue " ) ;
168
175
169
176
const fieldName = "fieldName" ;
170
177
const form = mount ( < MyTestForm formId = { formId } >
171
178
< MyTestField name = { fieldName } > </ MyTestField >
172
179
</ MyTestForm > ) ;
173
180
const formStore = FSHContainer . FormStoresHandler . GetStore ( formId ) ;
174
181
175
- expect ( ( FormStore . prototype . ValueChanged as any ) . callCount ) . toEqual ( 0 ) ;
182
+ expect ( ( FormStore . prototype . UpdateFieldValue as any ) . callCount ) . toEqual ( 0 ) ;
176
183
177
184
const input = form . find ( "input" ) ;
178
185
@@ -182,7 +189,7 @@ describe("Field Base", () => {
182
189
// Simulate value change
183
190
input . simulate ( "change" , { target : { value : newValue } } ) ;
184
191
185
- expect ( ( FormStore . prototype . ValueChanged as any ) . callCount ) . toEqual ( 1 ) ;
192
+ expect ( ( FormStore . prototype . UpdateFieldValue as any ) . callCount ) . toEqual ( 1 ) ;
186
193
// Check if it really changed value in form store
187
194
expect ( formStore . GetField ( fieldName ) . Value ) . toBe ( newValue ) ;
188
195
} ) ;
@@ -236,8 +243,46 @@ describe("Field Base", () => {
236
243
} ;
237
244
238
245
// Set spies on methods
239
- spy ( FormStore . prototype , "UpdateProps" ) ;
240
- spy ( MyTestField . prototype , "componentWillReceiveProps" ) ;
246
+ sandbox . spy ( FormStore . prototype , "UpdateFieldProps" ) ;
247
+ sandbox . spy ( MyTestField . prototype , "componentWillReceiveProps" ) ;
248
+
249
+ // Render form to create FormStore
250
+ shallow ( < MyTestForm formId = { formId } > </ MyTestForm > ) ;
251
+
252
+ const formStore = FSHContainer . FormStoresHandler . GetStore ( formId ) ;
253
+
254
+ // Mount with formId as a context
255
+ const field = mount < MyFieldProps > ( < MyTestField { ...fieldProps } /> , {
256
+ context : {
257
+ FormId : formId
258
+ } as FormChildContext
259
+ } ) ;
260
+
261
+ // Update MyTestField props
262
+ field . setProps ( fieldPropsNext ) ;
263
+
264
+ expect ( ( FormStore . prototype . UpdateFieldProps as any ) . callCount ) . toEqual ( 1 ) ;
265
+ expect ( ( MyTestField . prototype . componentWillReceiveProps as any ) . callCount ) . toEqual ( 1 ) ;
266
+ expect ( ( formStore . GetField ( fieldId ) . Props as MyFieldProps ) . value ) . toBe ( fieldPropsNext . value ) ;
267
+ } ) ;
268
+
269
+ it ( "updates props when componentWillReceiveProps is called with multi-level object" , ( ) => {
270
+ const formId = "FORM-ID" ;
271
+ const fieldId = "field" ;
272
+ const fieldProps : MyFieldProps = {
273
+ name : "field" ,
274
+ value : "initialValue" ,
275
+ deepObject : { value : "1" }
276
+ } ;
277
+ const fieldPropsNext : MyFieldProps = {
278
+ name : fieldProps . name ,
279
+ value : "Updated value" ,
280
+ deepObject : { value : "2" }
281
+ } ;
282
+
283
+ // Set spies on methods
284
+ sandbox . spy ( FormStore . prototype , "UpdateFieldProps" ) ;
285
+ sandbox . spy ( MyTestField . prototype , "componentWillReceiveProps" ) ;
241
286
242
287
// Render form to create FormStore
243
288
shallow ( < MyTestForm formId = { formId } > </ MyTestForm > ) ;
@@ -254,7 +299,7 @@ describe("Field Base", () => {
254
299
// Update MyTestField props
255
300
field . setProps ( fieldPropsNext ) ;
256
301
257
- expect ( ( FormStore . prototype . UpdateProps as any ) . callCount ) . toEqual ( 1 ) ;
302
+ expect ( ( FormStore . prototype . UpdateFieldProps as any ) . callCount ) . toEqual ( 1 ) ;
258
303
expect ( ( MyTestField . prototype . componentWillReceiveProps as any ) . callCount ) . toEqual ( 1 ) ;
259
304
expect ( ( formStore . GetField ( fieldId ) . Props as MyFieldProps ) . value ) . toBe ( fieldPropsNext . value ) ;
260
305
} ) ;
0 commit comments