Я пытаюсь научиться тестировать с Jest и Enzyme, и я написал этот тест, который не проходит.Я получаю два UnhandledPromiseRejectionWarnings: TypeError: Cannot read property 'preventDefault' of undefined
и nhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
Моя ложная ошибка: Expected mock function to have been called with:
["hot dog"]
But it was not called.
Мой тест для поиска пищевого API и возврата списка рецептов.
describe("<GetRecipe />", () => {
const filler: any = null;
it("Should call searchRecipe with the appropriate recipe", () => {
const food = "hot dog";
const searchRecipe = jest.fn();
const wrapper = shallow(
<GetRecipe
search={"hot dog"}
recipes={[filler]}
searchRecipe={searchRecipe}
/>
);
const button = wrapper.find("#getRecipe");
button.simulate("click");
expect(searchRecipe).toBeCalledWith(food);
});
});
И функция, которую я тестирую,
public searchRecipe = async (e: any) => {
e.preventDefault();
const { search } = this.state;
const res = await axios.get(
`https://api.edamam.com/search?q=${search}&app_id=${APID}&app_key=${APIKEY}&from=0&to=21`
);
console.log(res.data.hits);
this.setState({
recipes: res.data.hits
});
};