jest Unit Test を記述するための JSX ファイルが 1 つあります。
../dist/コンテナ/インデックス
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> }
)
)
テストファイルでは、
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'});
})
});
以下のエラーが表示されます。
expect(jest.fn())[.not].toBeCalledWith()
jest.fn() の値は、モック関数またはスパイでなければなりません。受信: オブジェクト: {"a": "2"}
componentwillmount 呼び出し全体をモックしたいので、showName を取得する必要がありますが、常に No Name を取得しています。サポートはありますか?