From c89e9c966beb5dce158fa3c571d74edb9f78706f Mon Sep 17 00:00:00 2001 From: David Beitey Date: Fri, 17 Apr 2026 07:15:30 +0000 Subject: [PATCH 1/2] fix: set loading/success error states in useLoadScript --- .../src/hooks/useLoadScript.ts | 10 ++++++-- .../react-google-charts/test/Chart.spec.tsx | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/packages/react-google-charts/src/hooks/useLoadScript.ts b/packages/react-google-charts/src/hooks/useLoadScript.ts index fce20a31..39520934 100644 --- a/packages/react-google-charts/src/hooks/useLoadScript.ts +++ b/packages/react-google-charts/src/hooks/useLoadScript.ts @@ -12,14 +12,20 @@ export function useLoadScript(src: string) { const [isSuccess, setIsSuccess] = useState(false); const onLoad = () => { setIsLoading(false); + setError(null); setIsSuccess(true); }; + const onError = (error: Error | null) => { + setError(error); + setIsLoading(false); + setIsSuccess(false); + }; useEffect(() => { if (!document) { const error = new Error( `[ScriptLoadingError] document not defined when attempting to load ${src}`, ); - setError(error); + onError(error); return; } @@ -55,7 +61,7 @@ export function useLoadScript(src: string) { const error = new Error( `[ScriptLoadingError] Failed to load script: ${src}`, ); - setError(error); + onError(error); }); // Add to DOM if not yet added. diff --git a/packages/react-google-charts/test/Chart.spec.tsx b/packages/react-google-charts/test/Chart.spec.tsx index 1eb42578..95800cac 100644 --- a/packages/react-google-charts/test/Chart.spec.tsx +++ b/packages/react-google-charts/test/Chart.spec.tsx @@ -15,6 +15,31 @@ describe("", () => { expect(getByText("Loading Chart")).toBeVisible(); }); + it("should render errorElement", async () => { + const { getByText } = render( + Don't Panic} />, + ); + + await waitFor(() => expect(getByText("Don't Panic")).toBeVisible()); + }); + + it("should render loader then errorElement", async () => { + const { getByText } = render( + Loading Chart} + errorElement={
Don't Panic
} />, + ); + + expect(getByText("Loading Chart")).toBeVisible(); + + await waitFor(() => expect(getByText("Don't Panic")).toBeVisible()); + }); + it("should draw chart", async () => { const { getByTestId } = render( Date: Mon, 20 Apr 2026 14:11:58 +1000 Subject: [PATCH 2/2] Simplify test assertions, check loading/error elements have changed --- packages/react-google-charts/test/Chart.spec.tsx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/react-google-charts/test/Chart.spec.tsx b/packages/react-google-charts/test/Chart.spec.tsx index 95800cac..3fcf003e 100644 --- a/packages/react-google-charts/test/Chart.spec.tsx +++ b/packages/react-google-charts/test/Chart.spec.tsx @@ -16,18 +16,19 @@ describe("", () => { }); it("should render errorElement", async () => { - const { getByText } = render( + const { findByText } = render( Don't Panic} />, ); - await waitFor(() => expect(getByText("Don't Panic")).toBeVisible()); + expect(await findByText("Don't Panic")).toBeVisible(); + }); it("should render loader then errorElement", async () => { - const { getByText } = render( + const { getByText, queryByText, findByText } = render( ", () => { ); expect(getByText("Loading Chart")).toBeVisible(); + expect(queryByText("Don't Panic")).toBeNull(); - await waitFor(() => expect(getByText("Don't Panic")).toBeVisible()); + expect(await findByText("Don't Panic")).toBeVisible(); + expect(queryByText("Loading Chart")).toBeNull(); }); it("should draw chart", async () => {