如何在Formik handleSubmit中测试诺言/酶?

时间:2019-01-09 14:15:53

标签: reactjs jestjs enzyme formik

我正在尝试在.formn的handleSubmit中测试一个诺言...对于两个诺言,我有两个Expect(),对第一个诺言的测试有效,但对第二个诺言却无效。

这是我的考试:

it('should submit the form with success', () => {

    let patchUserPromise = jest
      .fn()
      .mockImplementation((userId, value) => Promise.resolve(userId, value));

    let fetchUserPromise = jest
      .fn()
      .mockImplementation((value) => Promise.resolve(value));

    const renderedComponent = mount(
      <Provider store={store}>
        <MemoryRouter>
          <UserEditForm
            user={values}
            patchUserPromise={patchUserPromise}
            fetchUserPromise={fetchUserPromise}
            currentDomain={{name: 'example.net', id: 6}}
          />
        </MemoryRouter>
      </Provider>,
    );

    renderedComponent.update();

    renderedComponent.find('input#firstname').simulate('change', {
      persist: noop,
      target: {
        id: 'firstname',
        value: 'toti@example.net',
      },
    });
    renderedComponent.find('input#firstname').simulate('blur', {
      persist: noop,
      target: {
        id: 'firstname',
      },
    });


    userInnerform.find('form').simulate('submit', jest.fn());

    return new Promise((resolve) => setImmediate(resolve)).then(() => {
      expect(patchUserPromise).toBeCalled();

      //this expect doesn't work
      expect(fetchUserPromise).toBeCalled();
    });
  });

这是我的handleSubmit:

  handleSubmit: (values, bag) => {
    const {props, setSubmitting} = bag;
    const {userId} = props;
    const patchPromise =
      props.loggedUserEmail === values.email
        ? props.patchAdminPromise
        : props.patchUserPromise;
    const submittedValues = cleanSubmitDatas(values, props.user);


    return patchPromise({userId, values: submittedValues})
      .then((res) => props.fetchUserPromise({userId, values}))
      .then((res) => {
        setSubmitting(false);
        toast.success(<FormattedMessage id="form.savedSuccesfully" />, {
          autoClose: 5000,
        });
      })

我还不太清楚如何使用Jest或Enzyme,我是React测试的新手。对我来说.then有问题,我的测试找不到我的模拟函数,或者我的测试很糟糕!我迷失了承诺测试!

谢谢您的回答=)!

0 个答案:

没有答案