Skip to content

Commit c42d1b2

Browse files
test(bubble-chart-web): update tests to use react-testing-library and refactor for readability
1 parent a2f0e33 commit c42d1b2

File tree

3 files changed

+62
-56
lines changed

3 files changed

+62
-56
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
...require("@mendix/pluggable-widgets-tools/test-config/jest.enzyme-free.config.js")
3+
};

packages/pluggableWidgets/bubble-chart-web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"lint": "eslint src/ package.json",
3434
"release": "pluggable-widgets-tools release:web",
3535
"start": "pluggable-widgets-tools start:server",
36-
"test": "pluggable-widgets-tools test:unit:web",
36+
"test": "jest --projects jest.config.js",
3737
"update-changelog": "rui-update-changelog-widget",
3838
"verify": "rui-verify-package-format"
3939
},

packages/pluggableWidgets/bubble-chart-web/src/__tests__/BubbleChart.spec.tsx

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1-
import { ChartWidget } from "@mendix/shared-charts/main";
2-
import { EditableValueBuilder, ListAttributeValueBuilder, list, listExp } from "@mendix/widget-plugin-test-utils";
3-
import Big from "big.js";
4-
import { ReactWrapper, mount } from "enzyme";
1+
jest.mock("@mendix/shared-charts/main", () => {
2+
const actualModule = jest.requireActual("@mendix/shared-charts/main");
3+
return {
4+
...actualModule,
5+
ChartWidget: jest.fn(() => null)
6+
};
7+
});
8+
9+
import { ChartWidget, setupBasicSeries } from "@mendix/shared-charts/main";
10+
import { listExpression } from "@mendix/widget-plugin-test-utils";
11+
import "@testing-library/jest-dom";
12+
import { render, RenderResult } from "@testing-library/react";
513
import { createElement } from "react";
614
import { LinesType } from "../../typings/BubbleChartProps";
715
import { BubbleChart } from "../BubbleChart";
816

917
jest.mock("react-plotly.js", () => jest.fn(() => null));
1018

1119
describe("The Bubble widget", () => {
12-
function renderBubbleChart(configs: Array<Partial<LinesType>>): ReactWrapper {
13-
return mount(
20+
function renderBubbleChart(configs: Array<Partial<LinesType>> = [{}]): RenderResult {
21+
return render(
1422
<BubbleChart
1523
name="bubble-chart-test"
1624
class="bubble-chart-class"
17-
lines={configs.map(setupBasicSeries)}
25+
lines={configs.map(setupBasicBubbleSeries)}
1826
showLegend={false}
1927
widthUnit="percentage"
2028
width={0}
@@ -31,66 +39,61 @@ describe("The Bubble widget", () => {
3139
}
3240

3341
it("visualizes data as a bubble chart", () => {
34-
const bubbleChart = renderBubbleChart([{}]);
35-
const data = bubbleChart.find(ChartWidget).prop("data");
36-
expect(data).toHaveLength(1);
37-
expect(data[0]).toHaveProperty("type", "scatter");
38-
expect(data[0]).toHaveProperty("mode", "markers");
42+
renderBubbleChart();
43+
44+
expect(ChartWidget).toHaveBeenCalledWith(
45+
expect.objectContaining({
46+
data: expect.arrayContaining([expect.objectContaining({ type: "scatter", mode: "markers" })])
47+
}),
48+
{}
49+
);
3950
});
4051

4152
it("sets the marker color on the data series based on the markerColor value", () => {
42-
const bubbleChart = renderBubbleChart([
43-
{ staticMarkerColor: listExp(() => "red") },
44-
{ staticMarkerColor: undefined }
45-
]);
46-
const data = bubbleChart.find(ChartWidget).prop("data");
47-
expect(data).toHaveLength(2);
48-
expect(data[0]).toHaveProperty("marker.color", "red");
49-
expect(data[1]).toHaveProperty("marker.color", undefined);
53+
renderBubbleChart([{ staticMarkerColor: listExpression(() => "red") }, { staticMarkerColor: undefined }]);
54+
55+
expect(ChartWidget).toHaveBeenCalledWith(
56+
expect.objectContaining({
57+
data: expect.arrayContaining([
58+
expect.objectContaining({ marker: expect.objectContaining({ color: "red" }) }),
59+
expect.objectContaining({ marker: expect.objectContaining({ color: undefined }) })
60+
])
61+
}),
62+
{}
63+
);
5064
});
5165

5266
it("sets the appropriate transforms on the data series based on the aggregation type", () => {
53-
const bubbleChart = renderBubbleChart([{ aggregationType: "none" }, { aggregationType: "avg" }]);
54-
const data = bubbleChart.find(ChartWidget).prop("data");
55-
expect(data).toHaveLength(2);
56-
expect(data[0]).toHaveProperty("transforms", []);
57-
expect(data[1]).toHaveProperty("transforms", [
58-
{
59-
type: "aggregate",
60-
groups: ["1", "2"],
61-
aggregations: [
62-
{
63-
target: "y",
64-
enabled: true,
65-
func: "avg"
66-
}
67-
]
68-
}
69-
]);
67+
renderBubbleChart([{ aggregationType: "none" }, { aggregationType: "avg" }]);
68+
69+
expect(ChartWidget).toHaveBeenCalledWith(
70+
expect.objectContaining({
71+
data: expect.arrayContaining([
72+
expect.objectContaining({ transforms: expect.arrayContaining([]) }),
73+
expect.objectContaining({
74+
transforms: expect.arrayContaining([
75+
expect.objectContaining({
76+
type: "aggregate",
77+
groups: expect.arrayContaining(["1", "2"]),
78+
aggregations: expect.arrayContaining([
79+
expect.objectContaining({ target: "y", enabled: true, func: "avg" })
80+
])
81+
})
82+
])
83+
})
84+
])
85+
}),
86+
{}
87+
);
7088
});
7189
});
7290

73-
function setupBasicSeries(overwriteConfig: Partial<LinesType>): LinesType {
74-
const xAttribute = new ListAttributeValueBuilder<Big>().build();
75-
const getXAttributeMock = jest.fn();
76-
getXAttributeMock.mockReturnValueOnce(new EditableValueBuilder<Big>().withValue(new Big(1)).build());
77-
getXAttributeMock.mockReturnValueOnce(new EditableValueBuilder<Big>().withValue(new Big(2)).build());
78-
xAttribute.get = getXAttributeMock;
79-
80-
const yAttribute = new ListAttributeValueBuilder<Big>().build();
81-
const getYAttributeMock = jest.fn();
82-
getYAttributeMock.mockReturnValueOnce(new EditableValueBuilder<Big>().withValue(new Big(3)).build());
83-
getYAttributeMock.mockReturnValueOnce(new EditableValueBuilder<Big>().withValue(new Big(6)).build());
84-
yAttribute.get = getYAttributeMock;
91+
function setupBasicBubbleSeries(overwriteConfig: Partial<LinesType>): LinesType {
92+
const basicSeries = setupBasicSeries(overwriteConfig) as LinesType;
8593

8694
return {
87-
dataSet: "static",
88-
customSeriesOptions: overwriteConfig.customSeriesOptions ?? "",
89-
aggregationType: overwriteConfig.aggregationType ?? "avg",
95+
...basicSeries,
9096
staticMarkerColor: overwriteConfig.staticMarkerColor ?? undefined,
91-
staticDataSource: list(2),
92-
staticXAttribute: xAttribute,
93-
staticYAttribute: yAttribute,
9497
autosize: true,
9598
sizeref: 10
9699
};

0 commit comments

Comments
 (0)