Skip to content

Commit 708f801

Browse files
test(accordion-web): update test cases to use only react-testing-library
1 parent 75069e5 commit 708f801

File tree

8 files changed

+1819
-5389
lines changed

8 files changed

+1819
-5389
lines changed

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

Lines changed: 135 additions & 174 deletions
Large diffs are not rendered by default.

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

Lines changed: 211 additions & 188 deletions
Large diffs are not rendered by default.
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
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", () => {
7-
let defaultHeaderProps: PropsWithChildren<HeaderProps>;
7+
const defaultHeaderProps: PropsWithChildren<HeaderProps> = {
8+
heading: "headingThree",
9+
children: "Text"
10+
};
811

9-
beforeEach(() => {
10-
defaultHeaderProps = {
11-
heading: "headingThree",
12-
children: "Text"
13-
};
14-
});
12+
function renderHeader(props: Partial<HeaderProps> = {}): RenderResult {
13+
return render(<Header {...defaultHeaderProps} {...props} />);
14+
}
1515

1616
it("renders h1", () => {
17-
const headerWrapper = shallow(<Header {...defaultHeaderProps} heading="headingOne" />);
17+
const header = renderHeader({ heading: "headingOne" });
1818

19-
expect(headerWrapper).toMatchSnapshot();
19+
expect(header.container).toMatchSnapshot();
2020
});
2121

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

25-
expect(headerWrapper).toMatchSnapshot();
25+
expect(header.container).toMatchSnapshot();
2626
});
2727

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

31-
expect(headerWrapper).toMatchSnapshot();
31+
expect(header.container).toMatchSnapshot();
3232
});
3333

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

37-
expect(headerWrapper).toMatchSnapshot();
37+
expect(header.container).toMatchSnapshot();
3838
});
3939

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

43-
expect(headerWrapper).toMatchSnapshot();
43+
expect(header.container).toMatchSnapshot();
4444
});
4545

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

49-
expect(headerWrapper).toMatchSnapshot();
49+
expect(header.container).toMatchSnapshot();
5050
});
5151
});
Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,73 @@
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", () => {
6-
let defaultIconProps: IconProps;
7-
8-
beforeEach(() => {
9-
defaultIconProps = {
10-
data: { type: "glyph", iconClass: "icon-class" },
11-
loading: false,
12-
animate: true
13-
};
14-
});
7+
const defaultIconProps: IconProps = {
8+
data: { type: "glyph", iconClass: "icon-class" },
9+
loading: false,
10+
animate: true
11+
};
1512

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

19-
expect(iconWrapper).toMatchSnapshot();
20-
});
17+
describe("Rendering", () => {
18+
it("renders glyph icons", () => {
19+
const icon = renderIcon();
2120

22-
it("renders image icons", () => {
23-
const iconWrapper = render(<Icon {...defaultIconProps} data={{ type: "image", iconUrl: "icon.url" }} />);
21+
expect(icon.container).toMatchSnapshot();
22+
});
2423

25-
expect(iconWrapper).toMatchSnapshot();
26-
});
24+
it("renders image icons", () => {
25+
const icon = renderIcon({ data: { type: "image", iconUrl: "icon.url" } });
2726

28-
it("renders a default icon", () => {
29-
const iconWrapper = render(<Icon {...defaultIconProps} data={undefined} />);
27+
expect(icon.container).toMatchSnapshot();
28+
});
3029

31-
expect(iconWrapper).toMatchSnapshot();
32-
});
30+
it("renders a default icon", () => {
31+
const icon = renderIcon({ data: undefined });
3332

34-
it("doesn't render a default icon while loading", () => {
35-
const iconWrapper = render(<Icon {...defaultIconProps} data={undefined} loading />);
33+
expect(icon.container).toMatchSnapshot();
34+
});
3635

37-
expect(iconWrapper).toMatchSnapshot();
38-
});
36+
it("doesn't render a default icon while loading", () => {
37+
const icon = renderIcon({ data: undefined, loading: true });
3938

40-
it("doesn't render an icon with an unknown icon data type", () => {
41-
const iconWrapper = render(<Icon {...defaultIconProps} data={{ type: "unknown" } as any} />);
39+
expect(icon.container).toMatchSnapshot();
40+
});
4241

43-
expect(iconWrapper).toMatchSnapshot();
44-
});
42+
it("doesn't render an icon with an unknown icon data type", () => {
43+
const icon = renderIcon({ data: { type: "unknown" } as any });
4544

46-
it("doesn't apply an animate class to a glyph icon when animate is false", () => {
47-
const iconWrapper = mount(<Icon {...defaultIconProps} animate={false} />);
48-
49-
expect(iconWrapper.find("span").prop("className")).not.toContain("widget-accordion-group-header-icon-animate");
45+
expect(icon.container).toMatchSnapshot();
46+
});
5047
});
5148

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-
);
56-
57-
expect(iconWrapper.find("img").prop("className")).not.toContain("widget-accordion-group-header-icon-animate");
58-
});
59-
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");
49+
describe("Animation Behaviour", () => {
50+
it("doesn't apply an animate class to a glyph icon when animate is false", () => {
51+
const icon = renderIcon({ animate: false });
52+
const spanElement = icon.container.querySelector("span");
53+
54+
expect(spanElement).not.toHaveClass("widget-accordion-group-header-icon-animate");
55+
});
56+
57+
it("doesn't apply an animate class to an image icon when animate is false", () => {
58+
const icon = renderIcon({
59+
data: { type: "image", iconUrl: "icon.url" },
60+
animate: false
61+
});
62+
const imgElement = icon.container.querySelector("img");
63+
64+
expect(imgElement).not.toHaveClass("widget-accordion-group-header-icon-animate");
65+
});
66+
67+
it("doesn't apply an animate class to a default icon when animate is false", () => {
68+
const icon = renderIcon({ data: undefined, animate: false });
69+
const svgElement = icon.container.querySelector("svg");
70+
expect(svgElement).not.toHaveClass("widget-accordion-group-header-icon-animate");
71+
});
6472
});
6573
});

0 commit comments

Comments
 (0)