Skip to content

Commit 7cced37

Browse files
committed
Handle dispatches before componentDidMount. Fixes #28
1 parent 0591554 commit 7cced37

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

src/components/createConnect.js

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export default function createConnect(React) {
8080
trySubscribe() {
8181
if (shouldSubscribe && !this.unsubscribe) {
8282
this.unsubscribe = this.context.store.subscribe(::this.handleChange);
83+
this.handleChange();
8384
}
8485
}
8586

test/components/connect.spec.js

+54-9
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,32 @@ describe('React', () => {
109109
expect(div.props.string).toBe('ab');
110110
});
111111

112+
it('should handle dispatches before componentDidMount', () => {
113+
const store = createStore(stringBuilder);
114+
115+
@connect(state => ({ string: state }) )
116+
class Container extends Component {
117+
componentWillMount() {
118+
store.dispatch({ type: 'APPEND', body: 'a'});
119+
}
120+
121+
render() {
122+
return <div {...this.props}/>;
123+
}
124+
}
125+
126+
const tree = TestUtils.renderIntoDocument(
127+
<Provider store={store}>
128+
{() => (
129+
<Container />
130+
)}
131+
</Provider>
132+
);
133+
134+
const div = TestUtils.findRenderedDOMComponentWithTag(tree, 'div');
135+
expect(div.props.string).toBe('a');
136+
});
137+
112138
it('should handle additional prop changes in addition to slice', () => {
113139
const store = createStore(() => ({
114140
foo: 'bar'
@@ -469,6 +495,18 @@ describe('React', () => {
469495
}
470496
}
471497

498+
@connect(
499+
() => ({ foo: 'bar' }),
500+
() => ({ scooby: 'boo' })
501+
)
502+
class ContainerNext extends Component {
503+
render() {
504+
return (
505+
<div {...this.props} />
506+
);
507+
}
508+
}
509+
472510
let container;
473511
TestUtils.renderIntoDocument(
474512
<Provider store={store}>
@@ -479,18 +517,25 @@ describe('React', () => {
479517
expect(div.props.foo).toEqual(undefined);
480518
expect(div.props.scooby).toEqual('doo');
481519

482-
// Crude imitation of hot reloading that does the job
483-
Object.keys(ContainerAfter.prototype).filter(key =>
484-
typeof ContainerAfter.prototype[key] === 'function'
485-
).forEach(key => {
486-
if (key !== 'render') {
487-
ContainerBefore.prototype[key] = ContainerAfter.prototype[key];
488-
}
489-
});
520+
function imitateHotReloading(TargetClass, SourceClass) {
521+
// Crude imitation of hot reloading that does the job
522+
Object.keys(SourceClass.prototype).filter(key =>
523+
typeof SourceClass.prototype[key] === 'function'
524+
).forEach(key => {
525+
if (key !== 'render') {
526+
TargetClass.prototype[key] = SourceClass.prototype[key];
527+
}
528+
});
529+
container.forceUpdate();
530+
}
490531

491-
container.forceUpdate();
532+
imitateHotReloading(ContainerBefore, ContainerAfter);
492533
expect(div.props.foo).toEqual('baz');
493534
expect(div.props.scooby).toEqual('foo');
535+
536+
imitateHotReloading(ContainerBefore, ContainerNext);
537+
expect(div.props.foo).toEqual('bar');
538+
expect(div.props.scooby).toEqual('boo');
494539
});
495540

496541
it('should set the displayName correctly', () => {

0 commit comments

Comments
 (0)