field-form icon indicating copy to clipboard operation
field-form copied to clipboard

form.validateFields return error with `outOfDate:=true` in test

Open samkahchiin opened this issue 5 years ago • 4 comments

The code works perfectly fine in the apps. However, when I tested it using Jest, it will always return error upon calling form.validateFields(). The sample error returned is

   {
      values: { value: 'Updated' },
      errorFields: [],
      outOfDate: true
    }

I try to look at the source code and it said outOfDate means change when validating.

Does anybody knows how to avoid this error?

Here is my sample test codes

    test('should be able to update the values', async () => {
      renderComponent()
      const valueElem = await screen.findByText('value 1')
      userEvent.click(valueElem)

      const valueInput = await screen.findByTestId('editable-cell-input')
      userEvent.clear(valueInput)
      userEvent.type(valueInput, 'abc {enter}', { allAtOnce: false })

      await waitFor(() => {
        expect(screen.queryByTestId('editable-cell-input')).toBeNull()
        expect(screen.getByText('value 1 Updated')).toBeDefined()
        expect(updateInventoryValue).toHaveBeenCalledWith(1, {
          value: 'value 1 Updated',
        })
      })
    })

In apps

  const save = async () => {
    try {
      const params = await form.validateFields()
      toggleEdit()
      handleSave(record.id, params)
    } catch (errInfo) {
      console.log(errInfo)
      notification.error({
        message: 'Something went wrong!',
        description: 'Please try again.',
      })
    }
  }

      <Form form={form}>
        <Form.Item
          style={{
            margin: 0,
          }}
          name={title}
        >
          <Input
            ref={inputRef}
            onPressEnter={save}
            onBlur={save}
            data-testid="editable-cell-input"
          />
        </Form.Item>
      </Form>

samkahchiin avatar Sep 14 '20 07:09 samkahchiin

Have you found a solution ?

yosang003 avatar Nov 23 '20 12:11 yosang003

For those reading this thread later, a possible workaround is to use a setTimeout around the form.validateFields(). For some reason, it will validate without the outOfDate error. As a helper method, you can use something like:

const formValidateFields = (form: FormInstance): Promise<any> => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      form
        .validateFields()
        .then((values) => resolve(values))
        .catch((e) => reject(e))
    })
  })
}

sjoerdsmink avatar Mar 07 '22 16:03 sjoerdsmink