Reputation: 16480
I am trying to test a pure react component.
import React, {Component} from 'react';
class App extends Component {
constructor (props){
super(props);
props.init();
}
render() {
return (
<div className="container-wrapper">
{this.props.children}
</div>
);
}
}
App.propTypes = {
init : React.PropTypes.func,
children : React.PropTypes.element,
};
export default App;
import React from 'react';
import App from 'app/main/components/App';
import renderer from 'react-test-renderer';
jest.mock('react-dom');
const blank = jest.fn();
describe('App', () => {
it('Renders App', () => {
const component = renderer.create(<App init={blank}> </App>);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
});
When I execute the test I get below error.
console.error node_modules/fbjs/lib/warning.js:36
Warning: Failed prop type: Invalid prop `children` of type `string` supplied to `App`, expected a single ReactElement.
in App
I can understand that is says Props.children is invalid. How can I mock props.children? Or is there some other way test such components
Upvotes: 0
Views: 1713
Reputation: 10407
The previous solution still returns string. You can return any HTML element instead
it('Renders App', () => {
const component = renderer.create(
<App init={blank}>
<div />
</App>
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
Upvotes: 1
Reputation: 7073
You can simply pass a child to your <App />
component:
it('Renders App', () => {
const component = renderer.create(
<App init={blank}>
Hello App.
</App>
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
Upvotes: 2