dom-testing-library-fireEvent两次触发更改事件

时间:2019-06-19 08:55:32

标签: javascript reactjs jestjs

我正在尝试为我的react应用编写测试用例。 测试案例:文件上传时应启用“保存”按钮。

测试用例工作正常。但是,fireEvent会触发两次更改事件。

以前有人遇到过这个问题吗?有可能的解决方法吗?

test('Toggle save button disable on file upload', async () => {
    store.dispatch(setSurveyData(LoadStatus.SUCCESS, survey));
    const { container, getByText } = renderWithRedux(
      <BulkOptions
        questionId={question.id}
        options={question.options}
        onClose={setState}
      />
    );

    const uploadBtn: HTMLButtonElement = (await getByText(
      'Upload'
    )) as HTMLButtonElement;
    const fileInput: HTMLInputElement = uploadBtn.querySelector('input');

    const saveButton: HTMLButtonElement = (await getByText(
      'Save'
    )) as HTMLButtonElement;

    expect(saveButton.hasAttribute('disabled')).toBe(true);
    fileInput.onchange = e => {
      console.log(e);
    };
    fireEvent.change(fileInput, {
      target: {
        files: [
          new File(['option 11'], 'test.csv', {
            type: 'text/csv'
          })
        ]
      }
    });
    await waitForDomChange({ container })
      .then(() => {})
      .catch(err => console.log(`Error you need to deal with: ${err}`));
    console.log(
      container.querySelectorAll('textarea').length,
      container.querySelector('textarea').value
    );
    expect(saveButton.hasAttribute('disabled')).toBe(false);
  });

0 个答案:

没有答案