enzymejs / enzyme

JavaScript Testing utilities for React
https://enzymejs.github.io/enzyme/
MIT License
19.95k stars 2.01k forks source link

how to use invoke with async state #2527

Open chanelnumberseven opened 3 years ago

chanelnumberseven commented 3 years ago

index.test.tsx:

 it('delete', async () => {
    const onDel = jest.fn();
    const wrapper = mount(
      <Component
        onDel={onDel}
      />,
    );

    const modal = wrapper.find(Modal);

    await modal.invoke('onOk')().then(()=>{
      expect(onDel.mock.calls).toHaveLength(1);
    });
  });
});

the onOk prop look like:

onOk={async () => {
  await form.validateFields(['target']).then(() => {
    onDel();
    setVisible.setFalse();
  });
}}

get warning:

E2A965DD-6051-41EB-B648-8013D8E396B2

it work well when onOk look like(just move the state change outside the promise) :

onOk={async () => {
  setVisible.setFalse();
  await form.validateFields(['target']).then(() => {
    onDel();
  });
}}