Skip to content

Commit 7ff49a8

Browse files
Evan Schultzsmithad15
Evan Schultz
authored andcommitted
Merge pull request angular-redux#130 from danielfigueiredo/feat-comparer
Adding comparer to select decorator.
1 parent baccb26 commit 7ff49a8

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

packages/store/examples/counter/actions/counter-actions.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Injectable } from '@angular/core';
22
import { NgRedux } from 'ng2-redux';
3-
import * as Redux from 'redux';
43
import { RootState } from '../store';
54
import { RandomNumberService } from '../services/random-number';
65

packages/store/src/___tests___/decorators/select.spec.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'reflect-metadata';
22
import { expect, use } from 'chai';
3-
import { createStore } from 'redux';
43
import { NgRedux } from '../../components/ng-redux';
54
import { select } from '../../decorators/select';
65
import * as sinon from 'sinon';
@@ -117,4 +116,64 @@ describe('@select', () => {
117116
expect(value).to.equal(expectedValue);
118117
});
119118
});
119+
120+
describe('when passed a comparer', () => {
121+
122+
function comparer(x: any, y: any): boolean {
123+
return y === 1;
124+
}
125+
126+
it('should not trigger next when comparer returns true', () => {
127+
128+
class MockClass {
129+
@select(state => state.baz, comparer) asdf: any;
130+
}
131+
132+
const mockInstance = new MockClass();
133+
134+
mockInstance.asdf.subscribe(val => {
135+
expect(val).to.not.equal(1);
136+
});
137+
ngRedux.dispatch({type: 'nvm', payload: 1});
138+
});
139+
140+
it('should trigger next when comparer returns false', () => {
141+
142+
class MockClass {
143+
@select(state => state.baz, comparer) asdf: any;
144+
}
145+
146+
const mockInstance = new MockClass();
147+
let value;
148+
149+
mockInstance.asdf.subscribe(val => {
150+
value = val;
151+
});
152+
ngRedux.dispatch({type: 'nvm', payload: 2});
153+
expect(value).to.equal(2);
154+
});
155+
156+
it('should receive previous and next value for comparison', () => {
157+
158+
const spy = sinon.spy();
159+
160+
class MockClass {
161+
@select(state => state.baz, spy) asdf: any;
162+
}
163+
164+
const mockInstance = new MockClass();
165+
mockInstance.asdf.subscribe(val => null);
166+
167+
ngRedux.dispatch({type: 'nvm', payload: 1});
168+
ngRedux.dispatch({type: 'nvm', payload: 2});
169+
170+
expect(spy.getCall(0).args[0]).to.equal(undefined);
171+
expect(spy.getCall(0).args[1]).to.equal(1);
172+
173+
expect(spy.getCall(1).args[0]).to.equal(1);
174+
expect(spy.getCall(1).args[1]).to.equal(2);
175+
});
176+
177+
});
178+
120179
});

packages/store/src/components/ng-redux.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as Redux from 'redux';
33
import {
44
Store,
55
Action,
6-
ActionCreator,
76
Reducer,
87
createStore,
98
applyMiddleware,

packages/store/src/decorators/select.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import { NgRedux } from '../components/ng-redux';
44
* Selects an observable from the store, and attaches it to the decorated
55
* property.
66
*
7-
* @param {string | function} stateKeyOrFunc
7+
* @param {string | function} stateKeyOrFunc
8+
* @param {function} comparer function for this selector
89
* An Rxjs selector function or a string indicating the name of the store
910
* property to be selected.
1011
* */
11-
export const select = <T>(stateKeyOrFunc?) => (target, key) => {
12+
export const select = <T>(
13+
stateKeyOrFunc?,
14+
comparer?: (x: any, y: any) => boolean) => (target, key) => {
15+
1216
let bindingKey = (key.lastIndexOf('$') === key.length - 1) ?
1317
key.substring(0, key.length - 1) : key;
1418

@@ -17,9 +21,9 @@ export const select = <T>(stateKeyOrFunc?) => (target, key) => {
1721
}
1822

1923
function getter() {
24+
const isFunction = typeof stateKeyOrFunc === 'function';
2025
return NgRedux.instance
21-
.select(typeof stateKeyOrFunc === 'function' ?
22-
stateKeyOrFunc : bindingKey);
26+
.select(isFunction ? stateKeyOrFunc : bindingKey, comparer);
2327
}
2428

2529
// Delete property.

0 commit comments

Comments
 (0)