11import React , { Component } from 'react' ;
22import Form from '../src/Form' ;
3- import { bindState , updated } from '../src/utils' ;
3+ import { bindState , updated , buildHandlersCache } from '../src/utils' ;
44import { shallow } from 'enzyme' ;
55import expect from 'expect' ;
66
77describe ( 'updated' , function ( ) {
88 it ( 'carefully sets deeply nested item: deeply nested array' , function ( ) {
99 const obj = { foo : { bar : { baz : [ 1 , 2 , 3 ] } } , bak : { big : 1 } } ;
1010 const upd = updated ( obj , 'foo.bar.baz.1' , 4 ) ;
11-
11+
1212 expect ( obj === upd ) . toBe ( false , 'obj should not be updated in place' ) ;
1313 expect ( obj . foo === upd . foo ) . toBe ( false , 'obj.foo should not be updated in place' ) ;
1414 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() {
2020 it ( 'carefully sets deeply nested item: deeply nested object' , function ( ) {
2121 const obj = { foo : { bar : [ { baz : 'baz1' } , { baz : 'baz2' } ] } , bak : { big : 1 } } ;
2222 const upd = updated ( obj , 'foo.bar.1.baz' , 'baz3' ) ;
23-
23+
2424 expect ( obj === upd ) . toBe ( false , 'obj should not be updated in place' ) ;
2525 expect ( obj . foo === upd . foo ) . toBe ( false , 'obj.foo should not be updated in place' ) ;
2626 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() {
3232 it ( 'carefully sets deeply nested item, path collections are not defined' , function ( ) {
3333 const obj = { bak : { big : 1 } } ;
3434 const upd = updated ( obj , 'foo.bar.baz.1' , 4 ) ;
35-
35+
3636 expect ( obj . bak === upd . bak ) . toBe ( true , 'obj.bak should not be cloned' ) ;
3737 expect ( upd . foo . bar . baz ) . toMatch ( [ undefined , 4 ] , 'value under desired name should be updated' ) ;
3838 } ) ;
@@ -80,3 +80,25 @@ describe('bindState', function() {
8080 expect ( wrapper . state ( ) ) . toEqual ( { form : { foo : 'bar' } } ) ;
8181 } ) ;
8282} ) ;
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