11import expect from 'expect' ;
2+ let sinon = require ( 'sinon' ) ;
23import { createStore } from 'redux' ;
34import Connector from '../../src/components/connector' ;
45import _ from 'lodash' ;
56
67describe ( 'Connector' , ( ) => {
78 let store ;
89 let connect ;
9- let scopeStub ;
10+ let targetObj ;
1011
1112 beforeEach ( ( ) => {
1213 store = createStore ( ( state , action ) => ( {
1314 foo : 'bar' ,
1415 baz : action . payload
1516 } ) ) ;
16- scopeStub = {
17- $on : ( ) => { } ,
18- $destroy : ( ) => { }
19- } ;
17+ targetObj = { } ;
2018 connect = Connector ( store ) ;
2119 } ) ;
2220
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 ( ) ;
21+ it ( 'Should throw when target is not a Function or a plain object' , ( ) => {
22+ expect ( connect ( ( ) => ( { } ) ) . bind ( connect , 15 ) ) . toThrow ( ) ;
23+ expect ( connect ( ( ) => ( { } ) ) . bind ( connect , undefined ) ) . toThrow ( ) ;
24+ expect ( connect ( ( ) => ( { } ) ) . bind ( connect , 'test' ) ) . toThrow ( ) ;
25+
26+ expect ( connect ( ( ) => ( { } ) ) . bind ( connect , { } ) ) . toNotThrow ( ) ;
27+ expect ( connect ( ( ) => ( { } ) ) . bind ( connect , ( ) => { } ) ) . toNotThrow ( ) ;
2828
29- expect ( connect . bind ( connect , scopeStub , ( ) => ( { } ) ) ) . toNotThrow ( ) ;
3029 } ) ;
3130
32- it ( 'Should throw when selector does not return a plain object as target ' , ( ) => {
33- expect ( connect . bind ( connect , scopeStub , state => state . foo ) ) . toThrow ( ) ;
31+ it ( 'Should throw when selector does not return a plain object' , ( ) => {
32+ expect ( connect . bind ( connect , state => state . foo ) ) . toThrow ( ) ;
3433 } ) ;
3534
36- it ( 'Should extend scope with selected state once directly after creation' , ( ) => {
37- connect (
38- scopeStub ,
35+ it ( 'Should extend target (Object) with selected state once directly after creation' , ( ) => {
36+ connect (
3937 ( ) => ( {
4038 vm : { test : 1 }
41- } ) ) ;
39+ } ) ) ( targetObj ) ;
4240
43- expect ( scopeStub . vm ) . toEqual ( { test : 1 } ) ;
41+ expect ( targetObj . vm ) . toEqual ( { test : 1 } ) ;
4442 } ) ;
4543
46- it ( 'Should update the scope passed to connect when the store updates' , ( ) => {
47- connect ( scopeStub , state => state ) ;
44+ it ( 'Should update the target (Object) passed to connect when the store updates' , ( ) => {
45+ connect ( state => state ) ( targetObj ) ;
4846 store . dispatch ( { type : 'ACTION' , payload : 0 } ) ;
49- expect ( scopeStub . baz ) . toBe ( 0 ) ;
47+ expect ( targetObj . baz ) . toBe ( 0 ) ;
5048 store . dispatch ( { type : 'ACTION' , payload : 1 } ) ;
51- expect ( scopeStub . baz ) . toBe ( 1 ) ;
49+ expect ( targetObj . baz ) . toBe ( 1 ) ;
5250 } ) ;
5351
5452 it ( 'Should prevent unnecessary updates when state does not change (shallowly)' , ( ) => {
55- connect ( scopeStub , state => state ) ;
53+ connect ( state => state ) ( targetObj ) ;
5654 store . dispatch ( { type : 'ACTION' , payload : 5 } ) ;
5755
58- expect ( scopeStub . baz ) . toBe ( 5 ) ;
56+ expect ( targetObj . baz ) . toBe ( 5 ) ;
5957
60- scopeStub . baz = 0 ;
58+ targetObj . baz = 0 ;
6159
6260 //this should not replace our mutation, since the state didn't change
6361 store . dispatch ( { type : 'ACTION' , payload : 5 } ) ;
6462
65- expect ( scopeStub . baz ) . toBe ( 0 ) ;
63+ expect ( targetObj . baz ) . toBe ( 0 ) ;
64+
65+ } ) ;
6666
67+ it ( 'Should extend target (object) with actionCreators' , ( ) => {
68+ connect ( ( ) => ( { } ) , { ac1 : ( ) => { } , ac2 : ( ) => { } } ) ( targetObj ) ;
69+ expect ( _ . isFunction ( targetObj . ac1 ) ) . toBe ( true ) ;
70+ expect ( _ . isFunction ( targetObj . ac2 ) ) . toBe ( true ) ;
6771 } ) ;
6872
69- it ( 'Should extend scope with actionCreators' , ( ) => {
70- connect ( scopeStub , ( ) => ( { } ) , { ac1 : ( ) => { } , ac2 : ( ) => { } } ) ;
71- expect ( _ . isFunction ( scopeStub . ac1 ) ) . toBe ( true ) ;
72- expect ( _ . isFunction ( scopeStub . ac2 ) ) . toBe ( true ) ;
73+ it ( 'Should return an unsubscribing function' , ( ) => {
74+ const unsubscribe = connect ( state => state ) ( targetObj ) ;
75+ store . dispatch ( { type : 'ACTION' , payload : 5 } ) ;
76+
77+ expect ( targetObj . baz ) . toBe ( 5 ) ;
78+
79+ unsubscribe ( ) ;
80+
81+ store . dispatch ( { type : 'ACTION' , payload : 7 } ) ;
82+
83+ expect ( targetObj . baz ) . toBe ( 5 ) ;
84+
7385 } ) ;
7486
75- it ( 'Should provide dispatch to mapDispatchToScope when receiving a Function' , ( ) => {
87+ it ( 'Should provide dispatch to mapDispatchToTarget when receiving a Function' , ( ) => {
7688 let receivedDispatch ;
77- connect ( scopeStub , ( ) => ( { } ) , dispatch => { receivedDispatch = dispatch } ) ;
89+ connect ( ( ) => ( { } ) , dispatch => { receivedDispatch = dispatch } ) ( targetObj ) ;
7890 expect ( receivedDispatch ) . toBe ( store . dispatch ) ;
7991 } ) ;
8092
93+ it ( 'Should call target (Function) with mapStateToTarget and mapDispatchToTarget results ' , ( ) => {
94+
95+ //let targetFunc = sinon.spy();
96+ //connect(targetFunc, state => state.pojo);
97+ expect ( false ) . toBe ( true ) ;
98+ } ) ;
99+
81100} ) ;
0 commit comments