I have one JSX file to write jest Unit Test.
../dist/container/index
constructor(props) {
super(props);
this.state = {
showName: null
}
}
componentWillMount() {
Request
.get('/api')
.end((err, res) => {
if (res) {
this.setState({
showName: res.name
});
}
});
}
render (
let { showName } = this.state;
render (
{showName ? <div> showName</div> : <div>No Name</div> }
)
)
In test File,
import landing from '../dist/container/index’;
describe(‘landing’, () => {
it(‘check’, () => {
jest.mock('../dist/container/index’, () => {});
landing.componentWillMount = jest.fn().mockImplementation(() => { return { 'a': '2'} });
const lands = shallow(<landing productDetails={productDetail} messages={messages}/>);
expect(lands.componentWillMount()).toBeCalledWith({‘a’: '2'});
})
});
I am getting the below error.
expect(jest.fn())[.not].toBeCalledWith()
jest.fn() value must be a mock function or spy. Received: object: {"a": "2"}
I want to mock the whole componentwillmount calls and need to get the showName, but i am always getting No Name. Any support?