学习如何使用玩笑

时间:2018-09-22 18:26:09

标签: typescript jestjs enzyme

我正在尝试学习Jest和Enzyme的测试,并且我编写的此测试未通过。我收到两个UnhandledPromiseRejectionWarnings:TypeError: Cannot read property 'preventDefault' of undefinednhandled 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
 });
};

0 个答案:

没有答案