Problem Unit Testing a Parent Component whose Child Inject a Store
Hi, So I have this simple App which structured like this:
- Parent Component
class Parent extends React.Component {
render() {
return (
<div>
<h1>Parent</h1>
<Child></Child>
</div>
);
}
}
- Child Component
@inject("someStore")
@observer
class Child extends React.Component {
render() {
const { someStore } = this.props as any;
return (
<p>{ someStore.text }</p>
);
}
}
- Main App
const someStore = SomeStore.create({
text: "This is a text."
});
const mainApp = (
<Provider someStore={ someStore }>
<Parent></Parent>
</Provider>
);
ReactDOM.render(
mainApp,
document.getElementById("main")
);
Now if I test the Child Component using this code:
describe("child", function () {
it("should be created without any problem", function () {
let child = TestUtils.renderIntoDocument(<Child someStore={someStore}></Child>) as React.Component;
expect(child).toBeDefined();
});
})
The test will be successful.. But if I test the Parent Component using this code:
describe("parent", function () {
it("should be created without any problem", function () {
let parent = TestUtils.renderIntoDocument(<Parent></Parent>) as React.Component;
expect(parent).toBeDefined();
});
})
The test fails with this message:
Error: MobX injector: Store 'someStore' is not available! Make sure it is provided by some Provider
I assume the problem is because the Child Component need to have someStore injected to it. But I've tried replacing <Parent></Parent> in the test file with <Parent someStore={someStore}></Parent> and it will also fails. I believe this happen because the Child Component got the someStore from its props and the Parent Component doesn't pass the someStore to the Child Component's props.
The test will be successful if I replace <Parent></Parent> in the test file with <Provider someStore={someStore}><Parent></Parent></Provider>, but then the TestUtils.renderIntoDocument() method will returns Provider instead of Parent Component. I don't want it since I need to test the Parent Component.
This app is able to run without any error. Any advice to unit test the Parent Component whose Child Component inject someStore? I can't seem to find the solution in the documention (Or maybe I missed it somewhere in the docs).
Best Regards, Samuel
One way to solve it would be to avoid this situation altogether by making the parent independent of it's children. Quite common way to do that is to pass render callback in place of children:
class Parent extends React.Component {
render() {
return (
<div>
<h1>Parent</h1>
{this.props.children()} // any prop can be used e.g. this.props.renderContent()
</div>
);
}
}
describe("parent", function () {
it("should be created without any problem", function () {
let renderChild = () => <Child someStore={someStore}></Child>; // or simply () => <span>children</span>
let parent = TestUtils.renderIntoDocument(<Parent>renderChild</Parent>) as React.Component;
expect(parent).toBeDefined();
});
})
@urugator Thanks I've tried it and that solved the injection problem!