1
1
import expect from 'expect' ;
2
+ let sinon = require ( 'sinon' ) ;
2
3
import { createStore } from 'redux' ;
3
4
import Connector from '../../src/components/connector' ;
4
5
import _ from 'lodash' ;
5
6
6
7
describe ( 'Connector' , ( ) => {
7
8
let store ;
8
9
let connect ;
9
- let scopeStub ;
10
+ let targetObj ;
10
11
11
12
beforeEach ( ( ) => {
12
13
store = createStore ( ( state , action ) => ( {
13
14
foo : 'bar' ,
14
15
baz : action . payload
15
16
} ) ) ;
16
- scopeStub = {
17
- $on : ( ) => { } ,
18
- $destroy : ( ) => { }
19
- } ;
17
+ targetObj = { } ;
20
18
connect = Connector ( store ) ;
21
19
} ) ;
22
20
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 ( ) ;
28
28
29
- expect ( connect . bind ( connect , scopeStub , ( ) => ( { } ) ) ) . toNotThrow ( ) ;
30
29
} ) ;
31
30
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 ( ) ;
34
33
} ) ;
35
34
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 (
39
37
( ) => ( {
40
38
vm : { test : 1 }
41
- } ) ) ;
39
+ } ) ) ( targetObj ) ;
42
40
43
- expect ( scopeStub . vm ) . toEqual ( { test : 1 } ) ;
41
+ expect ( targetObj . vm ) . toEqual ( { test : 1 } ) ;
44
42
} ) ;
45
43
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 ) ;
48
46
store . dispatch ( { type : 'ACTION' , payload : 0 } ) ;
49
- expect ( scopeStub . baz ) . toBe ( 0 ) ;
47
+ expect ( targetObj . baz ) . toBe ( 0 ) ;
50
48
store . dispatch ( { type : 'ACTION' , payload : 1 } ) ;
51
- expect ( scopeStub . baz ) . toBe ( 1 ) ;
49
+ expect ( targetObj . baz ) . toBe ( 1 ) ;
52
50
} ) ;
53
51
54
52
it ( 'Should prevent unnecessary updates when state does not change (shallowly)' , ( ) => {
55
- connect ( scopeStub , state => state ) ;
53
+ connect ( state => state ) ( targetObj ) ;
56
54
store . dispatch ( { type : 'ACTION' , payload : 5 } ) ;
57
55
58
- expect ( scopeStub . baz ) . toBe ( 5 ) ;
56
+ expect ( targetObj . baz ) . toBe ( 5 ) ;
59
57
60
- scopeStub . baz = 0 ;
58
+ targetObj . baz = 0 ;
61
59
62
60
//this should not replace our mutation, since the state didn't change
63
61
store . dispatch ( { type : 'ACTION' , payload : 5 } ) ;
64
62
65
- expect ( scopeStub . baz ) . toBe ( 0 ) ;
63
+ expect ( targetObj . baz ) . toBe ( 0 ) ;
64
+
65
+ } ) ;
66
66
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 ) ;
67
71
} ) ;
68
72
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
+
73
85
} ) ;
74
86
75
- it ( 'Should provide dispatch to mapDispatchToScope when receiving a Function' , ( ) => {
87
+ it ( 'Should provide dispatch to mapDispatchToTarget when receiving a Function' , ( ) => {
76
88
let receivedDispatch ;
77
- connect ( scopeStub , ( ) => ( { } ) , dispatch => { receivedDispatch = dispatch } ) ;
89
+ connect ( ( ) => ( { } ) , dispatch => { receivedDispatch = dispatch } ) ( targetObj ) ;
78
90
expect ( receivedDispatch ) . toBe ( store . dispatch ) ;
79
91
} ) ;
80
92
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
+
81
100
} ) ;
0 commit comments