1+ import expect from 'expect' ;
2+ let sinon = require ( 'sinon' ) ;
3+ import { createStore } from 'redux' ;
4+ import Connector from '../../src/components/connector' ;
5+ import _ from 'lodash' ;
6+
7+ describe ( 'Connector' , ( ) => {
8+ let store ;
9+ let connector ;
10+ let targetObj ;
11+ let defaultState ;
12+
13+ beforeEach ( ( ) => {
14+ defaultState = {
15+ foo : 'bar' ,
16+ baz : - 1
17+ } ;
18+ store = createStore ( ( state = defaultState , action ) => {
19+ return { ...state , baz : action . payload } ;
20+ } ) ;
21+ targetObj = { } ;
22+ connector = new Connector ( store ) ;
23+ } ) ;
24+
25+ it ( 'Should throw when target is not a Function or a plain object' , ( ) => {
26+ expect ( connector . connect ( ( ) => ( { } ) ) . bind ( connector , 15 ) ) . toThrow ( ) ;
27+ expect ( connector . connect ( ( ) => ( { } ) ) . bind ( connector , undefined ) ) . toThrow ( ) ;
28+ expect ( connector . connect ( ( ) => ( { } ) ) . bind ( connector , 'test' ) ) . toThrow ( ) ;
29+
30+ expect ( connector . connect ( ( ) => ( { } ) ) . bind ( connector , { } ) ) . toNotThrow ( ) ;
31+ expect ( connector . connect ( ( ) => ( { } ) ) . bind ( connector , ( ) => { } ) ) . toNotThrow ( ) ;
32+
33+ } ) ;
34+
35+ it ( 'Should throw when selector does not return a plain object' , ( ) => {
36+ expect ( connector . connect . bind ( connector , state => state . foo ) ) . toThrow ( ) ;
37+ } ) ;
38+
39+ it ( 'Should extend target (Object) with selected state once directly after creation' , ( ) => {
40+ connector . connect (
41+ ( ) => ( {
42+ vm : { test : 1 }
43+ } ) ) ( targetObj ) ;
44+
45+ expect ( targetObj . vm ) . toEqual ( { test : 1 } ) ;
46+ } ) ;
47+
48+ it ( 'Should update the target (Object) passed to connect when the store updates' , ( ) => {
49+ connector . connect ( state => state ) ( targetObj ) ;
50+ store . dispatch ( { type : 'ACTION' , payload : 0 } ) ;
51+ expect ( targetObj . baz ) . toBe ( 0 ) ;
52+ store . dispatch ( { type : 'ACTION' , payload : 7 } ) ;
53+ expect ( targetObj . baz ) . toBe ( 7 ) ;
54+ } ) ;
55+
56+ it ( 'Should prevent unnecessary updates when state does not change (shallowly)' , ( ) => {
57+ connector . connect ( state => state ) ( targetObj ) ;
58+ store . dispatch ( { type : 'ACTION' , payload : 5 } ) ;
59+
60+ expect ( targetObj . baz ) . toBe ( 5 ) ;
61+
62+ targetObj . baz = 0 ;
63+
64+ //this should not replace our mutation, since the state didn't change
65+ store . dispatch ( { type : 'ACTION' , payload : 5 } ) ;
66+
67+ expect ( targetObj . baz ) . toBe ( 0 ) ;
68+
69+ } ) ;
70+
71+ it ( 'Should extend target (object) with actionCreators' , ( ) => {
72+ connector . connect ( ( ) => ( { } ) , { ac1 : ( ) => { } , ac2 : ( ) => { } } ) ( targetObj ) ;
73+ expect ( _ . isFunction ( targetObj . ac1 ) ) . toBe ( true ) ;
74+ expect ( _ . isFunction ( targetObj . ac2 ) ) . toBe ( true ) ;
75+ } ) ;
76+
77+ it ( 'Should return an unsubscribing function' , ( ) => {
78+ const unsubscribe = connector . connect ( state => state ) ( targetObj ) ;
79+ store . dispatch ( { type : 'ACTION' , payload : 5 } ) ;
80+
81+ expect ( targetObj . baz ) . toBe ( 5 ) ;
82+
83+ unsubscribe ( ) ;
84+
85+ store . dispatch ( { type : 'ACTION' , payload : 7 } ) ;
86+
87+ expect ( targetObj . baz ) . toBe ( 5 ) ;
88+
89+ } ) ;
90+
91+ it ( 'Should provide dispatch to mapDispatchToTarget when receiving a Function' , ( ) => {
92+ let receivedDispatch ;
93+ connector . connect ( ( ) => ( { } ) , dispatch => { receivedDispatch = dispatch } ) ( targetObj ) ;
94+ expect ( receivedDispatch ) . toBe ( store . dispatch ) ;
95+ } ) ;
96+
97+ } ) ;
0 commit comments