@@ -10,68 +10,53 @@ describe('Connector', () => {
1010 store = createStore ( ( state , action ) => ( {
1111 foo : 'bar' ,
1212 baz : action . payload ,
13- anotherState : 12
13+ anotherState : 12 ,
14+ childObject : { child : true }
1415 } ) ) ;
1516 connect = Connector ( store ) ;
1617 } ) ;
1718
18- it ( 'Should throw when not passed a function as callback' , ( ) => {
19- expect ( connect . bind ( connect , ( ) => { } , undefined ) ) . toThrow ( ) ;
20- expect ( connect . bind ( connect , ( ) => { } , { } ) ) . toThrow ( ) ;
21- expect ( connect . bind ( connect , ( ) => { } , 15 ) ) . toThrow ( ) ;
19+ it ( 'Should throw when not passed a plain object as target' , ( ) => {
20+ expect ( connect . bind ( connect , ( ) => ( { } ) , ( ) => { } ) ) . toThrow ( ) ;
21+ expect ( connect . bind ( connect , ( ) => ( { } ) , 15 ) ) . toThrow ( ) ;
22+ expect ( connect . bind ( connect , ( ) => ( { } ) , undefined ) ) . toThrow ( ) ;
23+ expect ( connect . bind ( connect , ( ) => ( { } ) , { } ) ) . toNotThrow ( ) ;
2224 } ) ;
2325
24- it ( 'Callback should be called once directly after creation to allow initialization' , ( ) => {
25- let counter = 0 ;
26- let callback = ( ) => counter ++ ;
27- connect ( state => state , callback ) ;
28- expect ( counter ) . toBe ( 1 ) ;
26+ it ( 'target should be extended with state once directly after creation' , ( ) => {
27+ let target = { } ;
28+ connect ( ( ) => ( { test : 1 } ) , target ) ;
29+ expect ( target ) . toEqual ( { test : 1 } ) ;
2930 } ) ;
3031
31- it ( 'Should call the callback passed to connect when the store updates' , ( ) => {
32- let counter = 0 ;
33- let callback = ( ) => counter ++ ;
34- connect ( state => state , callback ) ;
32+ it ( 'Should update the target passed to connect when the store updates' , ( ) => {
33+ let target = { } ;
34+ connect ( state => state , target ) ;
3535 store . dispatch ( { type : 'ACTION' , payload : 0 } ) ;
36+ expect ( target . baz ) . toBe ( 0 ) ;
3637 store . dispatch ( { type : 'ACTION' , payload : 1 } ) ;
37- expect ( counter ) . toBe ( 3 ) ;
38+ expect ( target . baz ) . toBe ( 1 ) ;
3839 } ) ;
3940
40- it ( 'Should prevent unnecessary updates when state does not change (shallowly)' , ( ) => {
41+ //does that still makes sense?
42+ /*it('Should prevent unnecessary updates when state does not change (shallowly)', () => {
4143 let counter = 0;
4244 let callback = () => counter++;
4345 connect(state => ({baz: state.baz}), callback);
4446 store.dispatch({type: 'ACTION', payload: 0});
4547 store.dispatch({type: 'ACTION', payload: 0});
4648 store.dispatch({type: 'ACTION', payload: 1});
4749 expect(counter).toBe(3);
48- } ) ;
50+ });*/
4951
50- it ( 'Should pass the selected state as argument to the callback' , ( ) => {
51- connect ( state => ( {
52- myFoo : state . foo
53- } ) , newState => {
54- expect ( newState ) . toEqual ( { myFoo : 'bar' } ) ;
55- } ) ;
56- } ) ;
57-
58- it ( 'Should allow multiple store slices to be selected' , ( ) => {
59- connect ( state => ( {
60- foo : state . foo ,
61- anotherState : state . anotherState
62- } ) , ( { foo, anotherState} ) => {
63- expect ( foo ) . toBe ( 'bar' ) ;
64- expect ( anotherState ) . toBe ( 12 ) ;
65- } ) ;
66- } ) ;
6752
6853 it ( 'Should return an unsubscribing function' , ( ) => {
69- let counter = 0 ;
70- let callback = ( ) => counter ++ ;
71- let unsubscribe = connect ( state => state , callback ) ;
72- store . dispatch ( { type : 'ACTION' , payload : 0 } ) ;
54+ let target = { } ;
55+ let unsubscribe = connect ( state => state , target ) ;
56+ store . dispatch ( { type : 'ACTION' , payload : 1 } ) ;
57+ expect ( target . baz ) . toBe ( 1 ) ;
7358 unsubscribe ( ) ;
7459 store . dispatch ( { type : 'ACTION' , payload : 2 } ) ;
75- expect ( counter ) . toBe ( 2 ) ;
60+ expect ( target . baz ) . toBe ( 1 ) ;
7661 } ) ;
7762} ) ;
0 commit comments