11import expect from 'expect' ;
2- import { createStore } from 'redux' ;
2+ import { createStore } from 'redux' ;
33import Connector from '../../src/components/connector' ;
4+ import _ from 'lodash' ;
45
56describe ( 'Connector' , ( ) => {
6- let store ;
7- let connect ;
8- let scopeStub ;
9-
10- beforeEach ( ( ) => {
11- store = createStore ( ( state , action ) => ( {
12- foo : 'bar' ,
13- baz : action . payload ,
14- anotherState : 12 ,
15- childObject : { child : true }
16- } ) ) ;
17- scopeStub = { $on : ( ) => { } , $destroy : ( ) => { } } ;
18- connect = Connector ( store ) ;
19- } ) ;
7+ let store ;
8+ let connect ;
9+ let scopeStub ;
2010
21- it ( 'Should throw when not passed a $scope object as target' , ( ) => {
22- expect ( connect . bind ( connect , ( ) => ( { } ) , ( ) => { } ) ) . toThrow ( ) ;
23- expect ( connect . bind ( connect , ( ) => ( { } ) , 15 ) ) . toThrow ( ) ;
24- expect ( connect . bind ( connect , ( ) => ( { } ) , undefined ) ) . toThrow ( ) ;
25- expect ( connect . bind ( connect , ( ) => ( { } ) , { } ) ) . toThrow ( ) ;
11+ beforeEach ( ( ) => {
12+ store = createStore ( ( state , action ) => ( {
13+ foo : 'bar' ,
14+ baz : action . payload
15+ } ) ) ;
16+ scopeStub = {
17+ $on : ( ) => { } ,
18+ $destroy : ( ) => { }
19+ } ;
20+ connect = Connector ( store ) ;
21+ } ) ;
2622
27- expect ( connect . bind ( connect , ( ) => ( { } ) , scopeStub ) ) . toNotThrow ( ) ;
28- } ) ;
23+ it ( 'Should throw when not passed a $scope object' , ( ) => {
24+ expect ( connect . bind ( connect , ( ) => { } , ( ) => ( { } ) ) ) . toThrow ( ) ;
25+ expect ( connect . bind ( connect , 15 , ( ) => ( { } ) ) ) . toThrow ( ) ;
26+ expect ( connect . bind ( connect , undefined , ( ) => ( { } ) ) ) . toThrow ( ) ;
27+ expect ( connect . bind ( connect , { } , ( ) => ( { } ) ) ) . toThrow ( ) ;
2928
30- it ( 'Should throw when selector does not return a plain object as target' , ( ) => {
31- expect ( connect . bind ( connect , state => state . foo , scopeStub ) ) . toThrow ( ) ;
29+ expect ( connect . bind ( connect , scopeStub , ( ) => ( { } ) ) ) . toNotThrow ( ) ;
3230 } ) ;
3331
34- it ( 'target should be extended with state once directly after creation' , ( ) => {
35- connect ( ( ) => ( { vm : { test : 1 } } ) , scopeStub ) ;
36- expect ( scopeStub . vm ) . toEqual ( { test : 1 } ) ;
37- } ) ;
32+ it ( 'Should throw when selector does not return a plain object as target' , ( ) => {
33+ expect ( connect . bind ( connect , scopeStub , state => state . foo ) ) . toThrow ( ) ;
34+ } ) ;
3835
39- it ( 'Should update the target passed to connect when the store updates' , ( ) => {
40- connect ( state => state , scopeStub ) ;
41- store . dispatch ( { type : 'ACTION' , payload : 0 } ) ;
42- expect ( scopeStub . baz ) . toBe ( 0 ) ;
43- store . dispatch ( { type : 'ACTION' , payload : 1 } ) ;
44- expect ( scopeStub . baz ) . toBe ( 1 ) ;
45- } ) ;
36+ it ( 'Should extend scope with selected state once directly after creation' , ( ) => {
37+ connect (
38+ scopeStub ,
39+ ( ) => ( {
40+ vm : { test : 1 }
41+ } ) ) ;
42+
43+ expect ( scopeStub . vm ) . toEqual ( { test : 1 } ) ;
44+ } ) ;
45+
46+ it ( 'Should extend scope[propertyKey] if propertyKey is passed' , ( ) => {
47+ connect (
48+ scopeStub ,
49+ ( ) => ( { test : 1 } ) ,
50+ ( ) => { } ,
51+ 'vm'
52+ ) ;
53+
54+ expect ( scopeStub . vm ) . toEqual ( { test : 1 } ) ;
55+ } ) ;
56+
57+ it ( 'Should update the scope passed to connect when the store updates' , ( ) => {
58+ connect ( scopeStub , state => state ) ;
59+ store . dispatch ( { type : 'ACTION' , payload : 0 } ) ;
60+ expect ( scopeStub . baz ) . toBe ( 0 ) ;
61+ store . dispatch ( { type : 'ACTION' , payload : 1 } ) ;
62+ expect ( scopeStub . baz ) . toBe ( 1 ) ;
63+ } ) ;
64+
65+ it ( 'Should prevent unnecessary updates when state does not change (shallowly)' , ( ) => {
66+ connect ( scopeStub , state => state ) ;
67+ store . dispatch ( { type : 'ACTION' , payload : 5 } ) ;
68+
69+ expect ( scopeStub . baz ) . toBe ( 5 ) ;
70+
71+ scopeStub . baz = 0 ;
72+
73+ //this should not replace our mutation, since the state didn't change
74+ store . dispatch ( { type : 'ACTION' , payload : 5 } ) ;
75+
76+ expect ( scopeStub . baz ) . toBe ( 0 ) ;
77+
78+ } ) ;
79+
80+ it ( 'Should extend scope with actionCreators' , ( ) => {
81+ connect ( scopeStub , ( ) => ( { } ) , { ac1 : ( ) => { } , ac2 : ( ) => { } } ) ;
82+ expect ( _ . isFunction ( scopeStub . ac1 ) ) . toBe ( true ) ;
83+ expect ( _ . isFunction ( scopeStub . ac2 ) ) . toBe ( true ) ;
84+ } ) ;
85+
86+ it ( 'Should provide dispatch to mapDispatchToScope when receiving a Function' , ( ) => {
87+ let receivedDispatch ;
88+ connect ( scopeStub , ( ) => ( { } ) , dispatch => { receivedDispatch = dispatch } ) ;
89+ expect ( receivedDispatch ) . toBe ( store . dispatch ) ;
90+ } ) ;
4691
47- //does that still makes sense?
48- /*it('Should prevent unnecessary updates when state does not change (shallowly)', () => {
49- let counter = 0;
50- let callback = () => counter++;
51- connect(state => ({baz: state.baz}), callback);
52- store.dispatch({type: 'ACTION', payload: 0});
53- store.dispatch({type: 'ACTION', payload: 0});
54- store.dispatch({type: 'ACTION', payload: 1});
55- expect(counter).toBe(3);
56- });*/
57- } ) ;
92+ } ) ;
0 commit comments