我的问题:
我想知道我做错了什么或应该怎么做才能使其通过并测试工作。
下面的代码是我对该功能的当前测试。
import React from "react";
import {mount, shallow} from "enzyme";
import { expect } from "chai";
import FormationList from "../component/FormationList/FormationList";
import Formation from "../component/Formation/Formation";
describe("<Default />", () => {
it("Test the submit to call send input to api", () => {
const wrapper = mount(
<FormationList/>
);
wrapper.update();
wrapper.find('input.search-bar').simulate('change', {target : {value : "chicken"}})
expect( wrapper.find('input.search-bar').props().value).to.equal("chicken");
wrapper.find('button.search-button').simulate('submit');
expect( wrapper.find('input.search-bar').props().value).to.equal("");
wrapper.find('form.search-form').simulate('submit');
console.log(wrapper.debug());
});
});
答案 0 :(得分:0)
您可以传递useEffect用作道具的Fetcher
并用jest.fn模拟,然后可以断言是否被调用。
此外,您可以使用Fetcher
作为模块并模拟该模块:
const newFetcher = (oldFetcher) => jest.fn(oldFetcher);
jest.mock('Fetcher', () => {
const FetcherModule = require.requireActual('Fetcher');
const MockFetcherModule = {
Fetcher: newFetcher(FetcherModule.Fetcher),
};
return MockFetcherModule;
});
// ... then you could assert the newFetcher if was called