1
1
import React , { Component } from 'react' ;
2
2
import Form from '../src/Form' ;
3
- import { bindState , updated } from '../src/utils' ;
3
+ import { bindState , updated , buildHandlersCache } from '../src/utils' ;
4
4
import { shallow } from 'enzyme' ;
5
5
import expect from 'expect' ;
6
6
7
7
describe ( 'updated' , function ( ) {
8
8
it ( 'carefully sets deeply nested item: deeply nested array' , function ( ) {
9
9
const obj = { foo : { bar : { baz : [ 1 , 2 , 3 ] } } , bak : { big : 1 } } ;
10
10
const upd = updated ( obj , 'foo.bar.baz.1' , 4 ) ;
11
-
11
+
12
12
expect ( obj === upd ) . toBe ( false , 'obj should not be updated in place' ) ;
13
13
expect ( obj . foo === upd . foo ) . toBe ( false , 'obj.foo should not be updated in place' ) ;
14
14
expect ( obj . foo . bar === upd . foo . bar ) . toBe ( false , 'obj.foo.bar should not be updated in place' ) ;
@@ -20,7 +20,7 @@ describe('updated', function() {
20
20
it ( 'carefully sets deeply nested item: deeply nested object' , function ( ) {
21
21
const obj = { foo : { bar : [ { baz : 'baz1' } , { baz : 'baz2' } ] } , bak : { big : 1 } } ;
22
22
const upd = updated ( obj , 'foo.bar.1.baz' , 'baz3' ) ;
23
-
23
+
24
24
expect ( obj === upd ) . toBe ( false , 'obj should not be updated in place' ) ;
25
25
expect ( obj . foo === upd . foo ) . toBe ( false , 'obj.foo should not be updated in place' ) ;
26
26
expect ( obj . foo . bar === upd . foo . bar ) . toBe ( false , 'obj.foo.bar should not be updated in place' ) ;
@@ -32,7 +32,7 @@ describe('updated', function() {
32
32
it ( 'carefully sets deeply nested item, path collections are not defined' , function ( ) {
33
33
const obj = { bak : { big : 1 } } ;
34
34
const upd = updated ( obj , 'foo.bar.baz.1' , 4 ) ;
35
-
35
+
36
36
expect ( obj . bak === upd . bak ) . toBe ( true , 'obj.bak should not be cloned' ) ;
37
37
expect ( upd . foo . bar . baz ) . toMatch ( [ undefined , 4 ] , 'value under desired name should be updated' ) ;
38
38
} ) ;
@@ -80,3 +80,25 @@ describe('bindState', function() {
80
80
expect ( wrapper . state ( ) ) . toEqual ( { form : { foo : 'bar' } } ) ;
81
81
} ) ;
82
82
} ) ;
83
+
84
+ describe ( 'buildHandlersCache' , function ( ) {
85
+ context ( 'simple case' , function ( ) {
86
+ it ( 'fetches result and caches it' , function ( ) {
87
+ const cache = buildHandlersCache ( ) ;
88
+ const result = cache . fetch ( 'foo' , ( ) => ( { } ) ) ;
89
+ expect ( cache . fetch ( 'foo' , ( ) => 'is not called' ) === result ) . toBe ( true ) ;
90
+ } ) ;
91
+ } ) ;
92
+
93
+ context ( 'complex case' , function ( ) {
94
+ it ( 'fetches result and caches it' , function ( ) {
95
+ const cache = buildHandlersCache ( ) ;
96
+ const fn = function ( ) { } ;
97
+ const obj = { } ;
98
+ const result = cache . fetch ( [ 'foo' , fn , 1 , obj , 5 ] , ( ) => fn ) ;
99
+
100
+ expect ( result ) . toBe ( fn , 'sets result according to setter' ) ;
101
+ expect ( cache . fetch ( [ 'foo' , fn , 1 , obj , 5 ] , ( ) => 'is not called' ) === result ) . toBe ( true , 'returns cached value properly' ) ;
102
+ } ) ;
103
+ } ) ;
104
+ } ) ;
0 commit comments