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