|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { render, waitFor } from "@testing-library/react"; |
| 5 | +import Highcharts from "highcharts"; |
| 6 | + |
| 7 | +import "highcharts/modules/no-data-to-display"; |
| 8 | +import "@cloudscape-design/components/test-utils/dom"; |
| 9 | +import { CloudscapeHighcharts, CloudscapeHighchartsProps } from "../../../lib/components/core/chart-core"; |
| 10 | +import testClasses from "../../../lib/components/core/test-classes/styles.selectors"; |
| 11 | +import createWrapper, { ElementWrapper } from "../../../lib/components/test-utils/dom"; |
| 12 | + |
| 13 | +class TestWrapper extends ElementWrapper { |
| 14 | + findNoData = () => this.findByClassName(testClasses["no-data"]); |
| 15 | +} |
| 16 | + |
| 17 | +function renderChart(props: Partial<CloudscapeHighchartsProps>, Component = CloudscapeHighcharts) { |
| 18 | + render(<Component highcharts={Highcharts} className="test-chart" options={{}} {...props} />); |
| 19 | + const wrapper = new TestWrapper(createWrapper().findByClassName("test-chart")!.getElement()); |
| 20 | + return wrapper; |
| 21 | +} |
| 22 | + |
| 23 | +const series: Highcharts.SeriesOptionsType[] = [{ type: "line", name: "Line series", data: [1, 2, 3] }]; |
| 24 | + |
| 25 | +const noDataContent = { |
| 26 | + loading: "no-data: loading", |
| 27 | + empty: "no-data: empty", |
| 28 | + noMatch: "no-data: no-match", |
| 29 | + error: "no-data: error", |
| 30 | +}; |
| 31 | + |
| 32 | +describe("CloudscapeHighcharts: no-data", () => { |
| 33 | + test('does not render no-data when statusType="finished"', () => { |
| 34 | + const wrapper = renderChart({ |
| 35 | + options: { series }, |
| 36 | + noData: { statusType: "finished", ...noDataContent }, |
| 37 | + }); |
| 38 | + |
| 39 | + expect(wrapper.findNoData()).toBe(null); |
| 40 | + }); |
| 41 | + |
| 42 | + test('renders no-data loading when statusType="loading"', async () => { |
| 43 | + const wrapper = renderChart({ |
| 44 | + options: { series }, |
| 45 | + noData: { statusType: "loading", ...noDataContent }, |
| 46 | + }); |
| 47 | + |
| 48 | + await waitFor(() => { |
| 49 | + expect(wrapper.findNoData()).not.toBe(null); |
| 50 | + expect(wrapper.findNoData()!.getElement().textContent).toBe("no-data: loading"); |
| 51 | + expect(wrapper.findLiveRegion()!.getElement()).toHaveTextContent("no-data: loading"); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + test('renders no-data loading when statusType="error"', async () => { |
| 56 | + const wrapper = renderChart({ |
| 57 | + options: { series }, |
| 58 | + noData: { statusType: "error", ...noDataContent }, |
| 59 | + }); |
| 60 | + |
| 61 | + await waitFor(() => { |
| 62 | + expect(wrapper.findNoData()).not.toBe(null); |
| 63 | + expect(wrapper.findNoData()!.getElement().textContent).toBe("no-data: error"); |
| 64 | + expect(wrapper.findLiveRegion()!.getElement()).toHaveTextContent("no-data: error"); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + test('renders no-data empty when statusType="finished" and no series provided', async () => { |
| 69 | + const wrapper = renderChart({ |
| 70 | + options: { series: [] }, |
| 71 | + noData: { statusType: "finished", ...noDataContent }, |
| 72 | + }); |
| 73 | + |
| 74 | + await waitFor(() => { |
| 75 | + expect(wrapper.findNoData()).not.toBe(null); |
| 76 | + expect(wrapper.findNoData()!.getElement().textContent).toBe("no-data: empty"); |
| 77 | + expect(wrapper.findLiveRegion()).toBe(null); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + test('renders no-data no-match when statusType="finished" and no series visible', async () => { |
| 82 | + const wrapper = renderChart({ |
| 83 | + options: { series }, |
| 84 | + noData: { statusType: "finished", ...noDataContent }, |
| 85 | + visibleSeries: [], |
| 86 | + }); |
| 87 | + |
| 88 | + await waitFor(() => { |
| 89 | + expect(wrapper.findNoData()).not.toBe(null); |
| 90 | + expect(wrapper.findNoData()!.getElement().textContent).toBe("no-data: no-match"); |
| 91 | + expect(wrapper.findLiveRegion()).toBe(null); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments