/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ import React, { useRef, useState } from "react"; import { render, act, within } from "@testing-library/react"; import { BarChart, Bar, XAxis, YAxis, // @ts-ignore CategoricalChartWrapper, } from "recharts"; import { useYAxisMetadata, useXAxisMetadata, useWindowSize } from "../chart-hooks"; interface Props { significantDigitLabels: boolean; data: any[]; } const data = [ { chapter: "Algorithms", pages: 100, }, { chapter: "Data Structure", pages: 300, }, ]; describe("useYAxisMetadata", () => { const YBarChart = (props: Props) => { const { data, significantDigitLabels } = props; const chartRef = useRef(null); const [chartLoaded, setChartLoaded] = useState(false); const { yAxisLargestValue } = useYAxisMetadata( chartRef, chartLoaded, significantDigitLabels, ); return ( <> {yAxisLargestValue} { chartRef.current = el; setChartLoaded(!!el); }} > ); }; test("should use YAxis metadata", async () => { const { getByTestId } = render(); expect(within(getByTestId("yAxisLargestValue")).getByText("300")).toBeInTheDocument(); }); }); describe("useXAxisMetadata", () => { const XBarChart = (props: Props) => { const { data, significantDigitLabels } = props; const chartRef = useRef(null); const [chartLoaded, setChartLoaded] = useState(false); const { xAxisLargestValue } = useXAxisMetadata( chartRef, chartLoaded, significantDigitLabels, ); return ( <> {xAxisLargestValue} { chartRef.current = el; setChartLoaded(!!el); }} > ); }; test("should use XAxis metadata", async () => { const { getByTestId } = render(); expect(within(getByTestId("xAxisLargestValue")).getByText("1")).toBeInTheDocument(); }); }); describe("useWindowSize", () => { const ResizableComponent = () => { const windowSize = useWindowSize(); return ( <> {windowSize.height} {windowSize.width} ); }; test("should handle window resize", async () => { const currentWidth = window.innerWidth; const currenHeight = window.innerHeight; const { getByText } = render(); expect(getByText(`${currentWidth}`)).toBeInTheDocument(); expect(getByText(`${currenHeight}`)).toBeInTheDocument(); const resizedWidth = currentWidth + 800; const resizedHeight = currenHeight + 600; const resizeWindow = (width: number, height: number) => { window.innerWidth = width; window.innerHeight = height; window.dispatchEvent(new Event("resize")); }; await act(async () => { resizeWindow(resizedWidth, resizedHeight); }); expect(getByText(`${resizedWidth}`)).toBeInTheDocument(); expect(getByText(`${resizedHeight}`)).toBeInTheDocument(); }); });