Skip to content

Commit 00bdba7

Browse files
committed
Refactored tests and removed deprecated ones
1 parent 58a8f2b commit 00bdba7

File tree

4 files changed

+36
-42
lines changed

4 files changed

+36
-42
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"dependencies": {
3232
"invariant": "^2.1.0",
33+
"lodash": "^3.10.1",
3334
"redux": "^1.0.1"
3435
}
3536
}

src/components/connector.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@ import isFunction from '../utils/isFunction';
22
import isPlainObject from '../utils/isPlainObject';
33
import shallowEqual from '../utils/shallowEqual';
44
import invariant from 'invariant';
5+
import _ from 'lodash'
56

6-
export default function Connector(store) {
7+
export default function Connector(store, $injector) {
78
return (selector, target) => {
89

10+
invariant(
11+
isPlainObject(target),
12+
'The target parameter passed to connect must be a plain object. Instead received %s.',
13+
typeof target
14+
);
15+
916
//Initial update
1017
let slice = getStateSlice(store.getState(), selector);
11-
target = angular.merge(target, slice);
18+
target = _.assign(target, slice);
1219

1320
let unsubscribe = store.subscribe(() => {
1421
let nextSlice = getStateSlice(store.getState(), selector);
1522
if (!shallowEqual(slice, nextSlice)) {
16-
target = angular.merge(target, nextSlice);
23+
target = _.assign(target, nextSlice);
1724
slice = nextSlice;
1825
}
1926
});

src/components/ngRedux.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default function ngReduxProvider() {
4040

4141
let finalCreateStore = _storeEnhancers ? compose(..._storeEnhancers, createStore) : createStore;
4242

43+
//digestMiddleware needs to be the last one.
4344
resolvedMiddleware.push(digestMiddleware($injector.get('$rootScope')));
4445

4546
store = applyMiddleware(...resolvedMiddleware)(finalCreateStore)(_reducer);

test/components/connector.spec.js

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,68 +10,53 @@ describe('Connector', () => {
1010
store = createStore((state, action) => ({
1111
foo: 'bar',
1212
baz: action.payload,
13-
anotherState: 12
13+
anotherState: 12,
14+
childObject: {child: true}
1415
}));
1516
connect = Connector(store);
1617
});
1718

18-
it('Should throw when not passed a function as callback', () => {
19-
expect(connect.bind(connect, () => {}, undefined)).toThrow();
20-
expect(connect.bind(connect, () => {}, {})).toThrow();
21-
expect(connect.bind(connect, () => {}, 15)).toThrow();
19+
it('Should throw when not passed a plain object as target', () => {
20+
expect(connect.bind(connect, () => ({}), () => {})).toThrow();
21+
expect(connect.bind(connect, () => ({}), 15)).toThrow();
22+
expect(connect.bind(connect, () => ({}), undefined)).toThrow();
23+
expect(connect.bind(connect, () => ({}), {})).toNotThrow();
2224
});
2325

24-
it('Callback should be called once directly after creation to allow initialization', () => {
25-
let counter = 0;
26-
let callback = () => counter++;
27-
connect(state => state, callback);
28-
expect(counter).toBe(1);
26+
it('target should be extended with state once directly after creation', () => {
27+
let target = {};
28+
connect(() => ({test: 1}), target);
29+
expect(target).toEqual({test: 1});
2930
});
3031

31-
it('Should call the callback passed to connect when the store updates', () => {
32-
let counter = 0;
33-
let callback = () => counter++;
34-
connect(state => state, callback);
32+
it('Should update the target passed to connect when the store updates', () => {
33+
let target = {};
34+
connect(state => state, target);
3535
store.dispatch({type: 'ACTION', payload: 0});
36+
expect(target.baz).toBe(0);
3637
store.dispatch({type: 'ACTION', payload: 1});
37-
expect(counter).toBe(3);
38+
expect(target.baz).toBe(1);
3839
});
3940

40-
it('Should prevent unnecessary updates when state does not change (shallowly)', () => {
41+
//does that still makes sense?
42+
/*it('Should prevent unnecessary updates when state does not change (shallowly)', () => {
4143
let counter = 0;
4244
let callback = () => counter++;
4345
connect(state => ({baz: state.baz}), callback);
4446
store.dispatch({type: 'ACTION', payload: 0});
4547
store.dispatch({type: 'ACTION', payload: 0});
4648
store.dispatch({type: 'ACTION', payload: 1});
4749
expect(counter).toBe(3);
48-
});
50+
});*/
4951

50-
it('Should pass the selected state as argument to the callback', () => {
51-
connect(state => ({
52-
myFoo: state.foo
53-
}), newState => {
54-
expect(newState).toEqual({myFoo: 'bar'});
55-
});
56-
});
57-
58-
it('Should allow multiple store slices to be selected', () => {
59-
connect(state => ({
60-
foo: state.foo,
61-
anotherState: state.anotherState
62-
}), ({foo, anotherState}) => {
63-
expect(foo).toBe('bar');
64-
expect(anotherState).toBe(12);
65-
});
66-
});
6752

6853
it('Should return an unsubscribing function', () => {
69-
let counter = 0;
70-
let callback = () => counter++;
71-
let unsubscribe = connect(state => state, callback);
72-
store.dispatch({type: 'ACTION', payload: 0});
54+
let target = {};
55+
let unsubscribe = connect(state => state, target);
56+
store.dispatch({type: 'ACTION', payload: 1});
57+
expect(target.baz).toBe(1);
7358
unsubscribe();
7459
store.dispatch({type: 'ACTION', payload: 2});
75-
expect(counter).toBe(2);
60+
expect(target.baz).toBe(1);
7661
});
7762
});

0 commit comments

Comments
 (0)