2 回答

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊
我認(rèn)為以下方法可以解決問題:
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貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
在進(jìn)行斷言之前,您需要掛載組件并模擬點(diǎn)擊事件:
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"
);
});
});
添加回答
舉報(bào)