2 回答
TA贡献1806条经验 获得超5个赞
我认为以下方法可以解决问题:
describe('an error occurred while updating', () => {
beforeEach(() => {});
it('decrements the counter', async () => {
const promise = Promise.reject('boo');
axios.patch.mockImplementationOnce(() => promise);
const wrapper = mount(
<MyCoolButton initialCounter={99} />
);
// Click the button
wrapper.find(`.cool-button`).first().simulate('click');
//do catch().then to make sure test executes after
// component caught the rejection.
return promise.catch(x=>x).then(() => {
// Check for decrmented value
const body = wrapper.find('.container span');
expect(body).to.have.text('Counter value is: 98');
});
});
});
以下是一些用于开玩笑的异步示例
TA贡献1865条经验 获得超7个赞
在进行断言之前,您需要挂载组件并模拟点击事件:
describe("an error occurred while updating", () => {
let wrapper;
beforeEach(() => {
axios.patch.mockRejectedValue("Mock error message");
wrapper = mount(<MyCoolButton initialCounter={99} />);
wrapper.find(".cool-button").simulate("click");
});
it("decrements the counter", () => {
expect(wrapper.find(".container span").text()).toEqual(
"Counter value is: 98"
);
});
});
添加回答
举报