diff --git a/app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx b/app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx new file mode 100644 index 00000000000..b569208e7f4 --- /dev/null +++ b/app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx @@ -0,0 +1,125 @@ +import { RenderModes } from "constants/WidgetConstants"; +import ChartWidget, { type ChartWidgetProps } from "."; +import { LabelOrientation, type ChartData } from "../constants"; +import { EmptyChartData } from "../component/EmptyChartData"; +import type { WidgetError } from "widgets/BaseWidget"; + +describe("ChartWidget getWidgetView", () => { + let chartWidget: ChartWidget; + + const seriesData1: ChartData = { + seriesName: "series1", + data: [{ x: "x1", y: 1 }], + color: "series1color", + }; + const seriesData2: ChartData = { + seriesName: "series2", + data: [{ x: "x1", y: 2 }], + color: "series2color", + }; + const defaultProps: ChartWidgetProps = { + allowScroll: true, + showDataPointLabel: true, + chartData: { + seriesID1: seriesData1, + seriesID2: seriesData2, + }, + chartName: "chart name", + type: "CHART_WIDGET", + chartType: "AREA_CHART", + customEChartConfig: {}, + customFusionChartConfig: { type: "type", dataSource: undefined }, + hasOnDataPointClick: true, + isVisible: true, + isLoading: false, + setAdaptiveYMin: false, + labelOrientation: LabelOrientation.AUTO, + onDataPointClick: "", + widgetId: "widgetID", + xAxisName: "xaxisname", + yAxisName: "yaxisname", + borderRadius: "1", + boxShadow: "1", + primaryColor: "primarycolor", + fontFamily: "fontfamily", + dimensions: { componentWidth: 11, componentHeight: 11 }, + parentColumnSpace: 1, + parentRowSpace: 1, + topRow: 0, + bottomRow: 0, + leftColumn: 0, + rightColumn: 0, + widgetName: "widgetName", + version: 1, + renderMode: RenderModes.CANVAS, + }; + const errors: WidgetError[] = [ + { + name: "error", + type: "configuration", + message: "We have a error", + }, + ]; + + describe("loading state", () => { + it("isLoading: true", () => { + chartWidget = new ChartWidget({ ...defaultProps, isLoading: true }); + + const view = chartWidget.getWidgetView(); + + expect(view.props.children.props.isLoading).toBe(true); + }); + + it("isLoading: true + errors", () => { + chartWidget = new ChartWidget({ + ...defaultProps, + isLoading: true, + errors, + }); + + const view = chartWidget.getWidgetView(); + + expect(view.props.children.props.isLoading).toBe(true); + }); + it("isLoading: true + emptyChartData", () => { + chartWidget = new ChartWidget({ + ...defaultProps, + isLoading: true, + chartData: {}, + }); + + const view = chartWidget.getWidgetView(); + + expect(view.props.children.props.isLoading).toBe(true); + }); + }); + + it("renders error state", () => { + chartWidget = new ChartWidget({ + ...defaultProps, + errors, + }); + + const view = chartWidget.getWidgetView(); + + expect(view.props.error.message).toBe("We have a error"); + }); + + it("renders empty chart data state", () => { + chartWidget = new ChartWidget({ ...defaultProps, chartData: {} }); + + const view = chartWidget.getWidgetView(); + + expect(view.type).toEqual(EmptyChartData); + }); + + it("renders chart with data", () => { + chartWidget = new ChartWidget(defaultProps); + const view = chartWidget.getWidgetView(); + + expect(view.props.children.props.chartData).toEqual({ + seriesID1: seriesData1, + seriesID2: seriesData2, + }); + }); +}); diff --git a/app/client/src/widgets/ChartWidget/widget/index.tsx b/app/client/src/widgets/ChartWidget/widget/index.tsx index db04a5f907d..9b95ed66518 100644 --- a/app/client/src/widgets/ChartWidget/widget/index.tsx +++ b/app/client/src/widgets/ChartWidget/widget/index.tsx @@ -226,42 +226,50 @@ class ChartWidget extends BaseWidget { getWidgetView() { const errors = syntaxErrorsFromProps(this.props); - if (errors.length == 0) { - if (emptyChartData(this.props)) { - return ; - } else { - return ( - }> - - - ); - } - } else { + if (this.props.isLoading) { + return this.renderChartWithData(); + } + + if (errors.length > 0) { return ; } + + if (emptyChartData(this.props)) { + return ; + } + + return this.renderChartWithData(); + } + + renderChartWithData() { + return ( + }> + + + ); } }