Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve conditional rendering in ChartWidget #36806

Merged
merged 10 commits into from
Oct 11, 2024
126 changes: 126 additions & 0 deletions app/client/src/widgets/ChartWidget/widget/ChartsRendered.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
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,
errors,
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,
});
});
});
76 changes: 42 additions & 34 deletions app/client/src/widgets/ChartWidget/widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,42 +226,50 @@ class ChartWidget extends BaseWidget<ChartWidgetProps, WidgetState> {
getWidgetView() {
const errors = syntaxErrorsFromProps(this.props);

if (errors.length == 0) {
if (emptyChartData(this.props)) {
return <EmptyChartData />;
} else {
return (
<Suspense fallback={<Skeleton />}>
<ChartComponent
allowScroll={this.props.allowScroll}
borderRadius={this.props.borderRadius}
boxShadow={this.props.boxShadow}
chartData={this.props.chartData}
chartName={this.props.chartName}
chartType={this.props.chartType}
customEChartConfig={this.props.customEChartConfig}
customFusionChartConfig={this.props.customFusionChartConfig}
dimensions={this.props}
fontFamily={ChartWidget.fontFamily}
hasOnDataPointClick={Boolean(this.props.onDataPointClick)}
isLoading={this.props.isLoading}
isVisible={this.props.isVisible}
key={this.props.widgetId}
labelOrientation={this.props.labelOrientation}
onDataPointClick={this.onDataPointClick}
primaryColor={this.props.accentColor ?? Colors.ROYAL_BLUE_2}
setAdaptiveYMin={this.props.setAdaptiveYMin}
showDataPointLabel={this.props.showDataPointLabel}
widgetId={this.props.widgetId}
xAxisName={this.props.xAxisName}
yAxisName={this.props.yAxisName}
/>
</Suspense>
);
}
} else {
if (this.props.isLoading) {
return this.renderChartWithData();
}

if (errors.length > 0) {
return <ChartErrorComponent error={errors[0]} />;
}

if (emptyChartData(this.props)) {
return <EmptyChartData />;
}

return this.renderChartWithData();
}

renderChartWithData() {
return (
<Suspense fallback={<Skeleton />}>
<ChartComponent
allowScroll={this.props.allowScroll}
borderRadius={this.props.borderRadius}
boxShadow={this.props.boxShadow}
chartData={this.props.chartData}
chartName={this.props.chartName}
chartType={this.props.chartType}
customEChartConfig={this.props.customEChartConfig}
customFusionChartConfig={this.props.customFusionChartConfig}
dimensions={this.props}
fontFamily={ChartWidget.fontFamily}
hasOnDataPointClick={Boolean(this.props.onDataPointClick)}
isLoading={this.props.isLoading}
isVisible={this.props.isVisible}
key={this.props.widgetId}
labelOrientation={this.props.labelOrientation}
onDataPointClick={this.onDataPointClick}
rahulbarwal marked this conversation as resolved.
Show resolved Hide resolved
primaryColor={this.props.accentColor ?? Colors.ROYAL_BLUE_2}
setAdaptiveYMin={this.props.setAdaptiveYMin}
showDataPointLabel={this.props.showDataPointLabel}
widgetId={this.props.widgetId}
xAxisName={this.props.xAxisName}
yAxisName={this.props.yAxisName}
/>
</Suspense>
);
}
}

Expand Down
Loading