Skip to content

Commit 109cf1d

Browse files
committed
Merge pull request #4 from redux-observable/factory
feat(factories): dispatchOnMount() now expects factories
2 parents 9bbf972 + 13aa477 commit 109cf1d

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/dispatchOnMount.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Subscription } from 'rxjs/Subscription';
33

44
const $$reduxObservableSubscription = '@@reduxObservableSubscription';
55

6-
export function dispatchOnMount(...toDispatch) {
6+
export function dispatchOnMount(...factories) {
77
return (ComposedComponent) =>
88
class DispatchOnMountComponent extends Component {
99
static contextTypes = {
@@ -12,7 +12,7 @@ export function dispatchOnMount(...toDispatch) {
1212

1313
componentDidMount() {
1414
this[$$reduxObservableSubscription] = new Subscription();
15-
toDispatch.map(a => this.context.store.dispatch(a))
15+
factories.map(factory => this.context.store.dispatch(factory(this.props)))
1616
.forEach(sub => sub && this[$$reduxObservableSubscription].add(sub));
1717
}
1818

@@ -21,7 +21,7 @@ export function dispatchOnMount(...toDispatch) {
2121
}
2222

2323
render() {
24-
return (<ComposedComponent {...this.props}/>);
24+
return (<ComposedComponent {...this.props} />);
2525
}
2626
};
2727
}

test/dispatchOnMount-spec.js

+34-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('dispatchOnMount', () => {
2222
};
2323
let store = createStore(reducer, applyMiddleware(reduxObservable()));
2424

25-
@dispatchOnMount(() => Observable.of({ type: 'TEST' }))
25+
@dispatchOnMount(() => () => Observable.of({ type: 'TEST' }))
2626
class TestComponent extends Component {
2727
}
2828

@@ -56,7 +56,7 @@ describe('dispatchOnMount', () => {
5656
};
5757
});
5858

59-
@dispatchOnMount(() => source1, () => source2)
59+
@dispatchOnMount(() => () => source1, () => () => source2)
6060
class TestComponent extends Component {
6161
}
6262

@@ -85,7 +85,7 @@ describe('dispatchOnMount', () => {
8585
let source1 = Observable.of({ type: 'SOURCE1' });
8686
let source2 = Observable.of({ type: 'SOURCE2' });
8787

88-
@dispatchOnMount(() => source1, () => source2)
88+
@dispatchOnMount(() => () => source1, () => () => source2)
8989
class TestComponent extends Component {
9090
}
9191

@@ -111,7 +111,7 @@ describe('dispatchOnMount', () => {
111111

112112
let source2 = Observable.of({ type: 'SOURCE2' });
113113

114-
@dispatchOnMount({ type: 'PLAIN_ACTION' }, () => source2)
114+
@dispatchOnMount(() => ({ type: 'PLAIN_ACTION' }), () => () => source2)
115115
class TestComponent extends Component {
116116
}
117117

@@ -130,4 +130,34 @@ describe('dispatchOnMount', () => {
130130
// let's just be really sure we don't break the unsub in the componentWillUnmount
131131
expect(() => comp.componentWillUnmount()).not.to.throw();
132132
});
133+
134+
it('should accept factories that get invoked with props', () => {
135+
let reducedActions = [];
136+
let reducer = (state, action) => {
137+
reducedActions.push(action);
138+
return state;
139+
};
140+
let store = createStore(reducer, applyMiddleware(reduxObservable()));
141+
142+
@dispatchOnMount(
143+
(props) => ({ type: 'PLAIN_ACTION', value: props.value }),
144+
(props) => () => Observable.of({ type: 'SOURCE2', value: props.value }))
145+
class TestComponent extends Component {
146+
}
147+
148+
let comp = new TestComponent({ value: 'Bilbo Bagginses' });
149+
// fake connection?
150+
comp.context = { store };
151+
comp.componentDidMount();
152+
153+
expect(reducedActions).to.deep.equal([
154+
{ type: '@@redux/INIT' },
155+
{ type: 'PLAIN_ACTION', value: 'Bilbo Bagginses' },
156+
{ type: 'SOURCE2', value: 'Bilbo Bagginses' }
157+
]);
158+
159+
// since plain actions don't return subscriptions, because they're not functions
160+
// let's just be really sure we don't break the unsub in the componentWillUnmount
161+
expect(() => comp.componentWillUnmount()).not.to.throw();
162+
});
133163
});

0 commit comments

Comments
 (0)