Skip to content

Commit

Permalink
feat: new onLoad prop (#451)
Browse files Browse the repository at this point in the history
* feat: introduce new prop: `onLoad` callback

* chore: fix conflicts

* refactor: call `onLoad` inside `onLoad` method
chore: add test

* refactor: cleanup
  • Loading branch information
newsiberian authored Dec 14, 2021
1 parent 1709e36 commit 855cc4f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/ReactGoogleCharts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export class Chart extends React.Component<
}

onLoad = (google: GoogleViz) => {
if (this.props.onLoad) {
this.props.onLoad(google);
}
if (this.isFullyLoaded(google)) {
this.onSuccess(google);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ export type ReactGoogleChartProps = {
graph_id?: string;
legendToggle?: boolean;
legend_toggle?: boolean;
onLoad?: (google: GoogleViz) => void;
getChartWrapper?: (
chartWrapper: GoogleChartWrapper,
google: GoogleViz
Expand Down
23 changes: 21 additions & 2 deletions test/Chart.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,26 @@ describe("<Chart />", () => {

const container = root.parentElement;

expect(container.style.width).toBe("456px");
expect(container.style.height).toBe("345px");
expect(container?.style.width).toBe("456px");
expect(container?.style.height).toBe("345px");
});

it("should call `onLoad` prop", async () => {
const handleLoad = jest.fn().mockImplementationOnce((/* google */) => {});
expect(handleLoad).not.toHaveBeenCalled();

const { getByTestId } = render(
<Chart
chartType="AreaChart"
rootProps={{ "data-testid": "1" }}
onLoad={handleLoad}
/>
);

await waitFor(() => getByTestId("1"), {
timeout: 5000,
});

expect(handleLoad).toHaveBeenCalled();
});
});

0 comments on commit 855cc4f

Please sign in to comment.