|
1 | 1 | import userEvent from "@testing-library/user-event";
|
2 | 2 | import * as React from "react";
|
| 3 | +import { _testLoad } from "../../src/AwaitLoader"; |
3 | 4 | import { createLoader } from "../../src/createLoader";
|
4 | 5 | import { _testCreateUseCreateQuery } from "../../src/createQuery";
|
5 | 6 | import { CustomLoaderProps } from "../../src/types";
|
6 | 7 | import { withLoader } from "../../src/withLoader";
|
7 |
| -import { _testLoad } from "../../src/AwaitLoader"; |
8 | 8 | import {
|
9 | 9 | useGetPokemonByNameQuery,
|
10 | 10 | useGetPokemonsQuery,
|
@@ -428,6 +428,64 @@ describe("withLoader", () => {
|
428 | 428 | render(<Component />);
|
429 | 429 | expect(screen.getByText("Success")).toBeVisible();
|
430 | 430 | });
|
| 431 | + |
| 432 | + test("Can remount a component that has a failed query", async () => { |
| 433 | + const Component = withLoader( |
| 434 | + (props, loaderData) => { |
| 435 | + return ( |
| 436 | + <div> |
| 437 | + Success{" "} |
| 438 | + {loaderData.queries.error.data.name.includes( |
| 439 | + "charizard" |
| 440 | + )} |
| 441 | + </div> |
| 442 | + ); |
| 443 | + }, |
| 444 | + createLoader({ |
| 445 | + queriesArg: (props: { error: boolean }) => props.error, |
| 446 | + useQueries: (shouldError) => { |
| 447 | + const error = useGetPokemonByNameQuery( |
| 448 | + shouldError ? "unprocessable" : "charizard" |
| 449 | + ); |
| 450 | + return { |
| 451 | + queries: { |
| 452 | + error, |
| 453 | + }, |
| 454 | + }; |
| 455 | + }, |
| 456 | + onError: () => <div>onError</div>, |
| 457 | + }) |
| 458 | + ); |
| 459 | + const Wrapper = () => { |
| 460 | + const [shouldError, setShouldError] = useState(true); |
| 461 | + return ( |
| 462 | + <div> |
| 463 | + <button onClick={() => setShouldError(!shouldError)}> |
| 464 | + Toggle |
| 465 | + </button> |
| 466 | + <ErrorBoundary fallback="Component threw"> |
| 467 | + {shouldError ? ( |
| 468 | + <Component error /> |
| 469 | + ) : ( |
| 470 | + <div>Success</div> |
| 471 | + )} |
| 472 | + </ErrorBoundary> |
| 473 | + </div> |
| 474 | + ); |
| 475 | + }; |
| 476 | + render(<Wrapper />); |
| 477 | + await waitFor(() => |
| 478 | + expect(screen.getByText("onError")).toBeVisible() |
| 479 | + ); |
| 480 | + await userEvent.click(screen.getByRole("button")); |
| 481 | + await waitFor(() => |
| 482 | + expect(screen.getByText("Success")).toBeVisible() |
| 483 | + ); |
| 484 | + await userEvent.click(screen.getByRole("button")); |
| 485 | + await waitFor(() => |
| 486 | + expect(screen.getByText("onError")).toBeVisible() |
| 487 | + ); |
| 488 | + }); |
431 | 489 | });
|
432 | 490 |
|
433 | 491 | describe("createLoader", () => {
|
@@ -814,7 +872,7 @@ describe("createLoader", () => {
|
814 | 872 | );
|
815 | 873 | });
|
816 | 874 |
|
817 |
| - test("Can partially extend config", async () => { |
| 875 | + test.skip("Can partially extend config", async () => { |
818 | 876 | const CustomLoader = (props: CustomLoaderProps) => {
|
819 | 877 | if (props.query.isError) {
|
820 | 878 | return <div>Custom error!</div>;
|
@@ -861,3 +919,40 @@ describe("createLoader", () => {
|
861 | 919 | });
|
862 | 920 | });
|
863 | 921 | });
|
| 922 | + |
| 923 | +class ErrorBoundary extends React.Component< |
| 924 | + { |
| 925 | + children?: React.ReactNode; |
| 926 | + fallback?: React.ReactNode; |
| 927 | + }, |
| 928 | + { |
| 929 | + hasError: boolean; |
| 930 | + } |
| 931 | +> { |
| 932 | + public state = { |
| 933 | + hasError: false, |
| 934 | + }; |
| 935 | + |
| 936 | + public static getDerivedStateFromError(_: Error) { |
| 937 | + return { hasError: true }; |
| 938 | + } |
| 939 | + |
| 940 | + public componentDidCatch( |
| 941 | + error: Error, |
| 942 | + errorInfo: React.ErrorInfo |
| 943 | + ) { |
| 944 | + console.error("Uncaught error:", error, errorInfo); |
| 945 | + } |
| 946 | + |
| 947 | + public render() { |
| 948 | + if (this.state.hasError) { |
| 949 | + return ( |
| 950 | + <h1>{this.props.fallback ?? "_error_boundary_"}</h1> |
| 951 | + ); |
| 952 | + } |
| 953 | + |
| 954 | + return this.props.children; |
| 955 | + } |
| 956 | +} |
| 957 | + |
| 958 | +export default ErrorBoundary; |
0 commit comments