【问题标题】:How to write a test if React-Error-Boundary recovers from error?如果 React-Error-Boundary 从错误中恢复,如何编写测试?
【发布时间】:2021-07-28 06:48:23
【问题描述】:

我的应用程序从API 获取数据,然后呈现数据。如果输入了不存在的值,则error boundary 触发并捕获错误。

我正在使用来自react-error-boundary 库的ErrorBoundary 和来自react-query 库的QueryErrorResetBoundary

使用我的React-Error-Boundary 设置,当error 发生时,我的应用程序有一个error boundary 触发器,并且能够通过重置stateerror 中恢复。当error 发生时,我目前对error boundary 触发进行了通过测试。现在我想测试error boundary 是否可以从触发的error boundary 中恢复并重置state。请让我知道如何使用 JestReact Testing Library 来解决这个问题。

应用组件

const ErrorFallback = ({ error, resetErrorBoundary }) => {
  return (
    <div role="alert">
      <p>Something went wrong:</p>
      <pre style={{ color: "red" }}>{error.message}</pre>
      <button onClick={resetErrorBoundary}>Try again</button>
    </div>
  );
};

const App = () => {
  const [idQuery, setIdQuery] = useState(0);

  return (
    <div>
      <QueryErrorResetBoundary>
        <ErrorBoundary
          FallbackComponent={ErrorFallback}
          onReset={() => {
            setIdQuery(0);
          }}
          resetKeys={[idQuery]}
        >
          <Home idQuery={idQuery} setIdQuery={setIdQuery} />
        </ErrorBoundary>
      </QueryErrorResetBoundary>
      <ReactQueryDevtools initialIsOpen={true} />
    </div>
  );
};

App.test.js

const App = () => {
    const [state, setState] = useState(0)
    return (
        <QueryErrorResetBoundary>
            <ErrorBoundary
                FallbackComponent={ErrorFallback}
                onReset={() => {
                    setState(0);
                }}
                resetKeys={[state]}
            >
                <Child />
            </ErrorBoundary>
        </QueryErrorResetBoundary>
    )
}

const Child = () => {
    throw new Error()
}

describe("Error Boundary", () => {
    beforeEach(() => {
        render(
            <App />
        );
    });

    it("should trigger the Error Boundary when an error occurs", () => {
        const errorMessage = screen.getByTestId("error-boundary-message");
        expect(errorMessage).toBeInTheDocument();
    });

    it("should recover from Error Boundary", () => {
        // ???
    })

});

【问题讨论】:

    标签: reactjs jestjs react-hooks react-testing-library react-error-boundary


    【解决方案1】:

    在不知道你实际想要做什么的情况下,我怀疑这样的事情可能会帮助你开始:

    const App = () => {
        const [isRecovered,setIsRecovered] = useState(false)
        return (
            <QueryErrorResetBoundary>
                <ErrorBoundary
                    FallbackComponent={ErrorFallback}
                    onReset={() => {
                        setIsRecovered(true)
                    }}
                    resetKeys={[isRecovered]}
                >
                    <Child isRecovered={isRecovered} />
                </ErrorBoundary>
            </QueryErrorResetBoundary>
        )
    }
    
    const Child = ({isRecovered}) => {
        if(!isRecovered) throw new Error();
        return 'All Good';
    }
    

    至少您可以将ErrorBoundary 的代码添加到您的问题中。

    【讨论】:

    • 谢谢你,@Adam。抱歉,我已经编辑了我的问题以添加描述以及我的代码沙箱。 ErrorBoundary 实际上来自 React-Error-Boundary 库。你给了我一个关于“应该从错误边界恢复”的测试用例的想法。测试ErrorFallback中的Try Again按钮是否被点击并查看isRecovered状态是否重置回false是否正确?
    • @ln09nv2 - 是的 - 您正在使用孩子的All Good(或其他您喜欢的东西)来让您知道您已经康复。或者,根据您的测试库,您可以只检查组件的状态,这完全取决于您。
    猜你喜欢
    • 2022-12-02
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 2020-04-29
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    相关资源
    最近更新 更多