Skip to content

Commit ae0753a

Browse files
authored
[]: Migrate widgets to use enzyme-free command for unit tests (#1627)
2 parents e150f96 + b184c62 commit ae0753a

File tree

27 files changed

+719
-1464
lines changed

27 files changed

+719
-1464
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
]
4040
},
4141
"overrides": {
42-
"@mendix/pluggable-widgets-tools": "10.21.0",
42+
"@mendix/pluggable-widgets-tools": "10.21.1",
4343
"react": "^18.0.0",
4444
"react-dom": "^18.0.0",
4545
"prettier": "3.5.3",

packages/pluggableWidgets/accordion-web/jest.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/pluggableWidgets/accordion-web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"publish-marketplace": "rui-publish-marketplace",
3939
"release": "pluggable-widgets-tools release:web",
4040
"start": "pluggable-widgets-tools start:server",
41-
"test": "jest --projects jest.config.js",
41+
"test": "pluggable-widgets-tools test:unit:web:enzyme-free",
4242
"update-changelog": "rui-update-changelog-widget",
4343
"verify": "rui-verify-package-format"
4444
},

packages/pluggableWidgets/badge-button-web/jest.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/pluggableWidgets/badge-button-web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"publish-marketplace": "rui-publish-marketplace",
3838
"release": "cross-env MPKOUTPUT=BadgeButton.mpk pluggable-widgets-tools release:web",
3939
"start": "cross-env MPKOUTPUT=BadgeButton.mpk pluggable-widgets-tools start:server",
40-
"test": "jest --projects jest.config.js",
40+
"test": "pluggable-widgets-tools test:unit:web:enzyme-free",
4141
"update-changelog": "rui-update-changelog-widget",
4242
"verify": "rui-verify-package-format"
4343
},

packages/pluggableWidgets/badge-web/jest.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/pluggableWidgets/badge-web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"publish-marketplace": "rui-publish-marketplace",
3838
"release": "cross-env MPKOUTPUT=Badge.mpk pluggable-widgets-tools release:web",
3939
"start": "cross-env MPKOUTPUT=Badge.mpk pluggable-widgets-tools start:server",
40-
"test": "jest --projects jest.config.js",
40+
"test": "pluggable-widgets-tools test:unit:web:enzyme-free",
4141
"update-changelog": "rui-update-changelog-widget",
4242
"verify": "rui-verify-package-format"
4343
},

packages/pluggableWidgets/color-picker-web/jest.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/pluggableWidgets/color-picker-web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"publish-marketplace": "rui-publish-marketplace",
3939
"release": "cross-env MPKOUTPUT=ColorPicker.mpk pluggable-widgets-tools release:web",
4040
"start": "cross-env MPKOUTPUT=ColorPicker.mpk pluggable-widgets-tools start:server",
41-
"test": "jest --projects jest.config.js",
41+
"test": "pluggable-widgets-tools test:unit:web:enzyme-free",
4242
"update-changelog": "rui-update-changelog-widget",
4343
"verify": "rui-verify-package-format"
4444
},

packages/pluggableWidgets/column-chart-web/src/__tests__/ColumnChart.spec.tsx

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
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";
1+
import { ChartWidget, setupBasicSeries } from "@mendix/shared-charts/main";
2+
import { listExpression } from "@mendix/widget-plugin-test-utils";
43
import { render, RenderResult } from "@testing-library/react";
54
import { createElement } from "react";
65
import { ColumnChartContainerProps, SeriesType } from "../../typings/ColumnChartProps";
@@ -26,7 +25,7 @@ describe("The ColumnChart widget", () => {
2625
name="column-chart-test"
2726
class="column-chart-class"
2827
barmode="group"
29-
series={configs.map(setupBasicSeries)}
28+
series={configs.map(setupBasicColumnSeries)}
3029
showLegend={false}
3130
widthUnit="percentage"
3231
width={0}
@@ -58,7 +57,7 @@ describe("The ColumnChart widget", () => {
5857
});
5958

6059
it("sets the bar color on the data series based on the barColor value", () => {
61-
renderColumnChart([{ staticBarColor: listExp(() => "red") }, { staticBarColor: undefined }]);
60+
renderColumnChart([{ staticBarColor: listExpression(() => "red") }, { staticBarColor: undefined }]);
6261

6362
const mockCalls = (ChartWidget as jest.Mock).mock.calls;
6463
const lastCallProps = mockCalls[mockCalls.length - 1][0];
@@ -105,26 +104,11 @@ describe("The ColumnChart widget", () => {
105104
});
106105
});
107106

108-
function setupBasicSeries(overwriteConfig: Partial<SeriesType>): SeriesType {
109-
const xAttribute = new ListAttributeValueBuilder<Big>().build();
110-
const getXAttributeMock = jest.fn();
111-
getXAttributeMock.mockReturnValueOnce(new EditableValueBuilder<Big>().withValue(new Big(1)).build());
112-
getXAttributeMock.mockReturnValueOnce(new EditableValueBuilder<Big>().withValue(new Big(2)).build());
113-
xAttribute.get = getXAttributeMock;
114-
115-
const yAttribute = new ListAttributeValueBuilder<Big>().build();
116-
const getYAttributeMock = jest.fn();
117-
getYAttributeMock.mockReturnValueOnce(new EditableValueBuilder<Big>().withValue(new Big(3)).build());
118-
getYAttributeMock.mockReturnValueOnce(new EditableValueBuilder<Big>().withValue(new Big(6)).build());
119-
yAttribute.get = getYAttributeMock;
107+
function setupBasicColumnSeries(overwriteConfig: Partial<SeriesType>): SeriesType {
108+
const basicSeries = setupBasicSeries(overwriteConfig) as SeriesType;
120109

121110
return {
122-
dataSet: "static",
123-
customSeriesOptions: overwriteConfig.customSeriesOptions ?? "",
124-
aggregationType: overwriteConfig.aggregationType ?? "avg",
125-
staticBarColor: overwriteConfig.staticBarColor ?? undefined,
126-
staticDataSource: list(2),
127-
staticXAttribute: xAttribute,
128-
staticYAttribute: yAttribute
111+
...basicSeries,
112+
staticBarColor: overwriteConfig.staticBarColor ?? undefined
129113
};
130114
}

packages/pluggableWidgets/combobox-web/jest.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/pluggableWidgets/combobox-web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"publish-marketplace": "rui-publish-marketplace",
4343
"release": "pluggable-widgets-tools release:web",
4444
"start": "pluggable-widgets-tools start:server",
45-
"test": "jest --projects jest.config.js",
45+
"test": "pluggable-widgets-tools test:unit:web:enzyme-free",
4646
"update-changelog": "rui-update-changelog-widget",
4747
"verify": "rui-verify-package-format"
4848
},

packages/pluggableWidgets/datagrid-web/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
...require("@mendix/pluggable-widgets-tools/test-config/jest.config"),
2+
...require("@mendix/pluggable-widgets-tools/test-config/jest.enzyme-free.config.js"),
33
/**
44
* `nanoevents` package is ESM module and because ESM is not supported by Jest yet
55
* we mark `nanoevents` as a module that should be transformed by ts-jest.

packages/pluggableWidgets/datagrid-web/src/components/__tests__/ColumnResizer.spec.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { render } from "enzyme";
1+
import "@testing-library/jest-dom";
2+
import { render } from "@testing-library/react";
23
import { createElement } from "react";
34
import { ColumnResizer } from "../ColumnResizer";
45

packages/pluggableWidgets/datagrid-web/src/components/__tests__/Header.spec.tsx

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,86 @@
1-
import { render, shallow } from "enzyme";
1+
import "@testing-library/jest-dom";
2+
import { render } from "@testing-library/react";
3+
import userEvent from "@testing-library/user-event";
24
import { createElement } from "react";
3-
import { GridColumn } from "../../typings/GridColumn";
5+
import { GridColumn, ColumnId } from "../../typings/GridColumn";
46
import { ColumnResizer } from "../ColumnResizer";
57
import { Header, HeaderProps } from "../Header";
68

79
describe("Header", () => {
810
it("renders the structure correctly", () => {
911
const component = render(<Header {...mockHeaderProps()} />);
1012

11-
expect(component).toMatchSnapshot();
13+
expect(component.asFragment()).toMatchSnapshot();
1214
});
1315

1416
it("renders the structure correctly when sortable", () => {
1517
const props = mockHeaderProps();
1618
props.column.canSort = true;
1719
props.sortable = true;
18-
1920
const component = render(<Header {...props} />);
2021

21-
expect(component).toMatchSnapshot();
22+
expect(component.asFragment()).toMatchSnapshot();
2223
});
2324

2425
it("renders the structure correctly when resizable", () => {
2526
const props = mockHeaderProps();
2627
props.column.canResize = true;
2728
props.resizable = true;
28-
2929
const component = render(<Header {...props} />);
3030

31-
expect(component).toMatchSnapshot();
31+
expect(component.asFragment()).toMatchSnapshot();
3232
});
3333

3434
it("renders the structure correctly when draggable", () => {
3535
const props = mockHeaderProps();
3636
props.column.canDrag = true;
3737
props.draggable = true;
38-
3938
const component = render(<Header {...props} />);
4039

41-
expect(component).toMatchSnapshot();
40+
expect(component.asFragment()).toMatchSnapshot();
4241
});
4342

4443
it("renders the structure correctly when filterable with no custom filter", () => {
4544
const props = mockHeaderProps();
4645
props.filterable = true;
47-
4846
const component = render(<Header {...props} />);
4947

50-
expect(component).toMatchSnapshot();
48+
expect(component.asFragment()).toMatchSnapshot();
5149
});
5250

5351
it("renders the structure correctly when filterable with custom filter", () => {
5452
const props = mockHeaderProps();
55-
const filterWidget = (
53+
props.filterable = true;
54+
props.filterWidget = (
5655
<div>
5756
<label>Date picker filter</label>
5857
<input type="date" />
5958
</div>
6059
);
61-
props.filterable = true;
62-
63-
const component = render(<Header {...props} filterWidget={filterWidget} />);
60+
const component = render(<Header {...props} />);
6461

65-
expect(component).toMatchSnapshot();
62+
expect(component.asFragment()).toMatchSnapshot();
6663
});
6764

68-
it("calls setSortBy store function with correct parameters when sortable", () => {
65+
it("calls setSortBy store function with correct parameters when sortable", async () => {
66+
const user = userEvent.setup();
6967
const mockedFunction = jest.fn();
70-
const column = {
71-
columnId: "0",
68+
const props = mockHeaderProps();
69+
props.sortable = true;
70+
props.column = {
71+
...props.column,
72+
columnId: "0" as ColumnId,
7273
columnNumber: 0,
7374
header: "My sortable column",
7475
canSort: true,
7576
sortDir: undefined,
7677
toggleSort: mockedFunction
7778
} as any;
79+
const component = render(<Header {...props} />);
80+
const button = component.getByRole("button");
7881

79-
const component = shallow(<Header {...mockHeaderProps()} column={column} sortable />);
80-
81-
const clickableRegion = component.find(".column-header");
82-
83-
expect(clickableRegion).toHaveLength(1);
84-
85-
clickableRegion.simulate("click");
82+
expect(button).toBeInTheDocument();
83+
await user.click(button);
8684
expect(mockedFunction).toHaveBeenCalled();
8785
});
8886

@@ -92,7 +90,7 @@ describe("Header", () => {
9290

9391
const component = render(<Header {...props} className="my-custom-class" />);
9492

95-
expect(component).toMatchSnapshot();
93+
expect(component.asFragment()).toMatchSnapshot();
9694
});
9795

9896
it("renders the structure correctly when is hidden and preview", () => {
@@ -103,7 +101,7 @@ describe("Header", () => {
103101

104102
const component = render(<Header {...props} />);
105103

106-
expect(component).toMatchSnapshot();
104+
expect(component.asFragment()).toMatchSnapshot();
107105
});
108106

109107
it("renders the structure correctly when value is empty", () => {
@@ -112,19 +110,35 @@ describe("Header", () => {
112110

113111
const component = render(<Header {...props} />);
114112

115-
expect(component).toMatchSnapshot();
113+
expect(component.asFragment()).toMatchSnapshot();
116114
});
117115
});
118116

119117
function mockHeaderProps(): HeaderProps {
120118
return {
121119
gridId: "dg1",
122120
column: {
123-
columnId: "dg1-column0",
121+
columnId: "dg1-column0" as ColumnId,
124122
columnIndex: 0,
125123
header: "Test",
126124
sortDir: undefined,
127-
toggleSort: () => undefined
125+
toggleSort: () => undefined,
126+
setHeaderElementRef: jest.fn(),
127+
alignment: "left",
128+
canDrag: false,
129+
columnClass: () => undefined,
130+
initiallyHidden: false,
131+
renderCellContent: () => createElement("div"),
132+
isAvailable: true,
133+
wrapText: false,
134+
canHide: false,
135+
isHidden: false,
136+
toggleHidden: () => undefined,
137+
canSort: false,
138+
canResize: false,
139+
size: undefined,
140+
setSize: () => undefined,
141+
getCssWidth: () => "100px"
128142
} as GridColumn,
129143
draggable: false,
130144
dropTarget: undefined,

0 commit comments

Comments
 (0)