Skip to content

Commit f40ba65

Browse files
[WC-2954]: Migrate tests from enzyme to react-testing-library (#1527)
2 parents c6ed57b + 3395f62 commit f40ba65

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+4647
-6275
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/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": "pluggable-widgets-tools test:unit:web",
41+
"test": "jest --projects jest.config.js",
4242
"update-changelog": "rui-update-changelog-widget",
4343
"verify": "rui-verify-package-format"
4444
},

packages/pluggableWidgets/accordion-web/src/components/AccordionGroup.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export function AccordionGroup(props: AccordionGroupProps): ReactElement | null
117117
switch (event.key) {
118118
case "Enter":
119119
case " ":
120+
case "Space":
120121
event.preventDefault();
121122
toggleCollapsed?.();
122123
break;

packages/pluggableWidgets/accordion-web/src/components/__tests__/Accordion.spec.tsx

Lines changed: 123 additions & 153 deletions
Large diffs are not rendered by default.

packages/pluggableWidgets/accordion-web/src/components/__tests__/AccordionGroup.spec.tsx

Lines changed: 225 additions & 193 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import "@testing-library/jest-dom";
2+
import { render, RenderResult } from "@testing-library/react";
13
import { createElement, PropsWithChildren } from "react";
2-
import { shallow } from "enzyme";
3-
44
import { Header, HeaderProps } from "../Header";
55

66
describe("Header", () => {
@@ -13,39 +13,43 @@ describe("Header", () => {
1313
};
1414
});
1515

16+
function renderHeader(props: Partial<HeaderProps> = {}): RenderResult {
17+
return render(<Header {...defaultHeaderProps} {...props} />);
18+
}
19+
1620
it("renders h1", () => {
17-
const headerWrapper = shallow(<Header {...defaultHeaderProps} heading="headingOne" />);
21+
const header = renderHeader({ heading: "headingOne" });
1822

19-
expect(headerWrapper).toMatchSnapshot();
23+
expect(header.asFragment()).toMatchSnapshot();
2024
});
2125

2226
it("renders h2", () => {
23-
const headerWrapper = shallow(<Header {...defaultHeaderProps} heading="headingTwo" />);
27+
const header = renderHeader({ heading: "headingTwo" });
2428

25-
expect(headerWrapper).toMatchSnapshot();
29+
expect(header.asFragment()).toMatchSnapshot();
2630
});
2731

2832
it("renders h3", () => {
29-
const headerWrapper = shallow(<Header {...defaultHeaderProps} />);
33+
const header = renderHeader();
3034

31-
expect(headerWrapper).toMatchSnapshot();
35+
expect(header.asFragment()).toMatchSnapshot();
3236
});
3337

3438
it("renders h4", () => {
35-
const headerWrapper = shallow(<Header {...defaultHeaderProps} heading="headingFour" />);
39+
const header = renderHeader({ heading: "headingFour" });
3640

37-
expect(headerWrapper).toMatchSnapshot();
41+
expect(header.asFragment()).toMatchSnapshot();
3842
});
3943

4044
it("renders h5", () => {
41-
const headerWrapper = shallow(<Header {...defaultHeaderProps} heading="headingFive" />);
45+
const header = renderHeader({ heading: "headingFive" });
4246

43-
expect(headerWrapper).toMatchSnapshot();
47+
expect(header.asFragment()).toMatchSnapshot();
4448
});
4549

4650
it("renders h6", () => {
47-
const headerWrapper = shallow(<Header {...defaultHeaderProps} heading="headingSix" />);
51+
const header = renderHeader({ heading: "headingSix" });
4852

49-
expect(headerWrapper).toMatchSnapshot();
53+
expect(header.asFragment()).toMatchSnapshot();
5054
});
5155
});
Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import "@testing-library/jest-dom";
2+
import { render, RenderResult } from "@testing-library/react";
13
import { createElement } from "react";
2-
import { mount, render } from "enzyme";
34
import { Icon, IconProps } from "../Icon";
45

56
describe("Icon", () => {
@@ -13,53 +14,64 @@ describe("Icon", () => {
1314
};
1415
});
1516

16-
it("renders glyph icons", () => {
17-
const iconWrapper = render(<Icon {...defaultIconProps} />);
17+
function renderIcon(props: Partial<IconProps> = {}): RenderResult {
18+
return render(<Icon {...defaultIconProps} {...props} />);
19+
}
1820

19-
expect(iconWrapper).toMatchSnapshot();
20-
});
21-
22-
it("renders image icons", () => {
23-
const iconWrapper = render(<Icon {...defaultIconProps} data={{ type: "image", iconUrl: "icon.url" }} />);
24-
25-
expect(iconWrapper).toMatchSnapshot();
26-
});
27-
28-
it("renders a default icon", () => {
29-
const iconWrapper = render(<Icon {...defaultIconProps} data={undefined} />);
21+
describe("Rendering", () => {
22+
it("renders glyph icons", () => {
23+
const icon = renderIcon();
3024

31-
expect(iconWrapper).toMatchSnapshot();
32-
});
25+
expect(icon.asFragment()).toMatchSnapshot();
26+
});
3327

34-
it("doesn't render a default icon while loading", () => {
35-
const iconWrapper = render(<Icon {...defaultIconProps} data={undefined} loading />);
28+
it("renders image icons", () => {
29+
const icon = renderIcon({ data: { type: "image", iconUrl: "icon.url" } });
3630

37-
expect(iconWrapper).toMatchSnapshot();
38-
});
31+
expect(icon.asFragment()).toMatchSnapshot();
32+
});
3933

40-
it("doesn't render an icon with an unknown icon data type", () => {
41-
const iconWrapper = render(<Icon {...defaultIconProps} data={{ type: "unknown" } as any} />);
34+
it("renders a default icon", () => {
35+
const icon = renderIcon({ data: undefined });
4236

43-
expect(iconWrapper).toMatchSnapshot();
44-
});
37+
expect(icon.asFragment()).toMatchSnapshot();
38+
});
4539

46-
it("doesn't apply an animate class to a glyph icon when animate is false", () => {
47-
const iconWrapper = mount(<Icon {...defaultIconProps} animate={false} />);
40+
it("doesn't render a default icon while loading", () => {
41+
const icon = renderIcon({ data: undefined, loading: true });
4842

49-
expect(iconWrapper.find("span").prop("className")).not.toContain("widget-accordion-group-header-icon-animate");
50-
});
43+
expect(icon.asFragment()).toMatchSnapshot();
44+
});
5145

52-
it("doesn't apply an animate class to an image icon when animate is false", () => {
53-
const iconWrapper = mount(
54-
<Icon {...defaultIconProps} data={{ type: "image", iconUrl: "icon.url" }} animate={false} />
55-
);
46+
it("doesn't render an icon with an unknown icon data type", () => {
47+
const icon = renderIcon({ data: { type: "unknown" } as any });
5648

57-
expect(iconWrapper.find("img").prop("className")).not.toContain("widget-accordion-group-header-icon-animate");
49+
expect(icon.asFragment()).toMatchSnapshot();
50+
});
5851
});
5952

60-
it("doesn't apply an animate class to a default icon when animate is false", () => {
61-
const iconWrapper = mount(<Icon {...defaultIconProps} data={undefined} animate={false} />);
62-
63-
expect(iconWrapper.find("svg").prop("className")).not.toContain("widget-accordion-group-header-icon-animate");
53+
describe("Animation Behaviour", () => {
54+
it("doesn't apply an animate class to a glyph icon when animate is false", () => {
55+
const icon = renderIcon({ animate: false });
56+
const spanElement = icon.container.querySelector("span");
57+
58+
expect(spanElement).not.toHaveClass("widget-accordion-group-header-icon-animate");
59+
});
60+
61+
it("doesn't apply an animate class to an image icon when animate is false", () => {
62+
const icon = renderIcon({
63+
data: { type: "image", iconUrl: "icon.url" },
64+
animate: false
65+
});
66+
const imgElement = icon.container.querySelector("img");
67+
68+
expect(imgElement).not.toHaveClass("widget-accordion-group-header-icon-animate");
69+
});
70+
71+
it("doesn't apply an animate class to a default icon when animate is false", () => {
72+
const icon = renderIcon({ data: undefined, animate: false });
73+
const svgElement = icon.container.querySelector("svg");
74+
expect(svgElement).not.toHaveClass("widget-accordion-group-header-icon-animate");
75+
});
6476
});
6577
});

0 commit comments

Comments
 (0)