Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/react-google-charts/src/hooks/useLoadScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions packages/react-google-charts/test/Chart.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@ describe("<Chart />", () => {
expect(getByText("Loading Chart")).toBeVisible();
});

it("should render errorElement", async () => {
const { findByText } = render(
<Chart
chartType="AreaChart"
chartLoaderScriptUrl="http://0.0.0.0/invalid-errorElement.js"
errorElement={<div>Don't Panic</div>} />,
);

expect(await findByText("Don't Panic")).toBeVisible();

});

it("should render loader then errorElement", async () => {
const { getByText, queryByText, findByText } = render(
<Chart
chartType="AreaChart"
chartLoaderScriptUrl="http://0.0.0.0/invalid-loader-errorElement.js"
loader={<div>Loading Chart</div>}
errorElement={<div>Don't Panic</div>} />,
);

expect(getByText("Loading Chart")).toBeVisible();
expect(queryByText("Don't Panic")).toBeNull();

expect(await findByText("Don't Panic")).toBeVisible();
expect(queryByText("Loading Chart")).toBeNull();
});

it("should draw chart", async () => {
const { getByTestId } = render(
<Chart
Expand Down