【问题标题】:Opening and closing dialog modal and waiting for response through provider打开和关闭对话框模式并等待提供者的响应
【发布时间】:2019-09-20 15:44:36
【问题描述】:

使用 React 测试库测试对话框提供程序。我可以让它打开,并断言它出现了——但由于某种原因,我无法让它在测试中关闭。我需要重新渲染吗?

test('await the closing or confirming of the modal', async () => {
  const { debug, getByText, queryByText } = render(
    <DialogProvider>
      <Test />
    </DialogProvider>,
  );
  const openDialogButton = getByText(/click me/i);
  fireEvent.click(openDialogButton);
  await wait(() => getByText(/ok/i));
  fireEvent.click(getByText(/ok/i));
  debug();
});


function Test() {
  const confirm = useConfirmation();
  return (
    <button
      onClick={() => {
        confirm({ variant: 'info' });
      }}
    >
      click me
    </button>
  );
}

【问题讨论】:

    标签: reactjs unit-testing react-testing-library


    【解决方案1】:

    显然,以下似乎有效

      await waitForElement(() => getByText(/ok/i));
      fireEvent.click(getByText(/ok/i));
      await waitForElementToBeRemoved(() => queryByText(/ok/i));
      expect(queryByText(/ok/i)).toBeNull();
    

    【讨论】:

    • 这解决了我在角度测试库中的一个类似问题——当标准await waitFor不工作时它工作。我不完全理解为什么,因为文档建议waitForElementToBeRemoved 只是一个包装waitFor 的实用程序,但是,FWIW,如果此页面的任何访问者遇到await waitFor 的问题这可能是一个可行的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    相关资源
    最近更新 更多