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