|
1 | 1 | import expect from 'expect'; |
2 | 2 | import {createStore} from 'redux'; |
3 | | -import {Connector, default as connectorFactory} from '../../src/components/connector'; |
| 3 | +import {default as Connector, copyArray} from '../../src/components/connector'; |
4 | 4 |
|
5 | 5 | describe('Connector', () => { |
6 | 6 | let store; |
7 | | - let ngRedux; |
| 7 | + let connector; |
8 | 8 | beforeEach(() => { |
9 | 9 | store = createStore((state, action) => { |
10 | | - return {foo: 'bar', baz: action.payload}; |
| 10 | + return {foo: 'bar', baz: action.payload, anotherState: 12}; |
11 | 11 | }); |
12 | | - ngRedux = { |
13 | | - getStore: () => store |
14 | | - }; |
| 12 | + connector = Connector(store); |
15 | 13 | }); |
16 | 14 |
|
17 | 15 | it('Should throw when not passed a function as callback', () => { |
18 | | - expect(() => new Connector(ngRedux, state => state, {})).toThrow(); |
19 | | - }); |
20 | | - |
21 | | - it('Should throw when not passed a function as selector', () => { |
22 | | - expect(() => new Connector(ngRedux, {}, () => {})).toThrow(); |
| 16 | + expect(connector.connect.bind(connector, () => {}, undefined)).toThrow(); |
| 17 | + expect(connector.connect.bind(connector, () => {}, {})).toThrow(); |
| 18 | + expect(connector.connect.bind(connector, () => {}, 15)).toThrow(); |
23 | 19 | }); |
24 | 20 |
|
25 | 21 | it('Callback should be called once directly after creation to allow initialization', () => { |
26 | 22 | let counter = 0; |
27 | 23 | let callback = () => counter++; |
28 | | - let connector = new Connector(ngRedux, state => state, callback); |
| 24 | + connector.connect(state => state, callback); |
29 | 25 | expect(counter).toBe(1); |
30 | 26 | }); |
31 | 27 |
|
32 | 28 | it('Should call the function passed to connect when the store updates', () => { |
33 | 29 | let counter = 0; |
34 | 30 | let callback = () => counter++; |
35 | | - let connector = new Connector(ngRedux, state => state, callback); |
| 31 | + connector.connect(state => state, callback); |
36 | 32 | store.dispatch({type: 'ACTION', payload: 0}); |
37 | 33 | store.dispatch({type: 'ACTION', payload: 1}); |
38 | 34 | expect(counter).toBe(3); |
39 | 35 | }); |
40 | 36 |
|
41 | | - it('Should prevent unnecessary updates when state does not change', () => { |
| 37 | + it('Should accept a function or an array of function as selector', () => { |
| 38 | + let receivedState1, receivedState2; |
| 39 | + connector.connect(state => state.foo, newState => receivedState1 = newState); |
| 40 | + connector.connect([state => state.foo], newState => receivedState2 = newState); |
| 41 | + expect(receivedState1).toBe('bar'); |
| 42 | + expect(receivedState1).toBe(receivedState2); |
| 43 | + }) |
| 44 | + |
| 45 | + it('Should prevent unnecessary updates when state does not change (shallowly)', () => { |
42 | 46 | let counter = 0; |
43 | 47 | let callback = () => counter++; |
44 | | - let connector = new Connector(ngRedux, state => state, callback); |
| 48 | + connector.connect(state => state.baz, callback); |
45 | 49 | store.dispatch({type: 'ACTION', payload: 0}); |
46 | 50 | store.dispatch({type: 'ACTION', payload: 0}); |
47 | | - store.dispatch({type: 'ACTION', payload: 0}); |
48 | | - expect(counter).toBe(2); |
| 51 | + store.dispatch({type: 'ACTION', payload: 1}); |
| 52 | + expect(counter).toBe(3); |
49 | 53 | }); |
50 | 54 |
|
51 | 55 | it('Should pass the selected state as argument to the callback', () => { |
52 | 56 | let receivedState; |
53 | | - let connector = new Connector(ngRedux, state => state.foo, newState => receivedState = newState); |
| 57 | + connector.connect(state => state.foo, newState => receivedState = newState); |
54 | 58 | store.dispatch({type: 'ACTION', payload: 1}); |
55 | 59 | expect(receivedState).toBe('bar'); |
56 | 60 | }); |
57 | 61 |
|
58 | | - it('Should unsubscribe when disconnect is called', () => { |
59 | | - let counter = 0; |
60 | | - let callback = () => counter++; |
61 | | - let connector = new Connector(ngRedux, state => state, callback); |
62 | | - store.dispatch({type: 'ACTION', payload: 0}); |
63 | | - connector.unsubscribe(); |
64 | | - store.dispatch({type: 'ACTION', payload: 2}); |
65 | | - expect(counter).toBe(2); |
66 | | - }); |
67 | | - |
68 | | - it('Factory: connect should create a new Connector', () => { |
69 | | - let api = connectorFactory(ngRedux); |
70 | | - let counter = 0; |
71 | | - let callback = () => counter++; |
72 | | - api.connect(state => state, callback); |
73 | | - store.dispatch({type: 'ACTION', payload: 0}); |
| 62 | + it('Should pass all the selected state as argument to the callback when provided an array of selectors', () => { |
| 63 | + connector.connect([state => state.foo, state => state.anotherState], |
| 64 | + (foo, anotherState) => { |
| 65 | + expect(foo).toBe('bar'); |
| 66 | + expect(anotherState).toBe(12); |
| 67 | + }); |
74 | 68 | store.dispatch({type: 'ACTION', payload: 1}); |
75 | | - store.dispatch({type: 'ACTION', payload: 2}); |
76 | | - expect(counter).toBe(4); |
77 | 69 | }); |
78 | 70 |
|
79 | | - it('Factory: should allow multiple Connector creation', () => { |
80 | | - let api = connectorFactory(ngRedux); |
| 71 | + it('Should return an unsubscribing function', () => { |
81 | 72 | let counter = 0; |
82 | 73 | let callback = () => counter++; |
83 | | - api.connect(state => state, callback); |
84 | | - api.connect(state => state, callback); |
85 | | - store.dispatch({type: 'ACTION', payload: 0}); |
86 | | - // 2 initialization + each connection responding once to the action = 4 |
87 | | - expect(counter).toBe(4); |
88 | | - }) |
89 | | - |
90 | | - it('Factory: connect should return an unsubscribing function', () => { |
91 | | - let api = connectorFactory(ngRedux); |
92 | | - let counter = 0; |
93 | | - let callback = () => counter++; |
94 | | - let unsubscribe = api.connect(state => state, callback); |
| 74 | + let unsubscribe = connector.connect(state => state, callback); |
95 | 75 | store.dispatch({type: 'ACTION', payload: 0}); |
96 | 76 | unsubscribe(); |
97 | | - store.dispatch({type: 'ACTION', payload: 1}); |
98 | 77 | store.dispatch({type: 'ACTION', payload: 2}); |
99 | 78 | expect(counter).toBe(2); |
100 | 79 | }); |
|
0 commit comments