|
| 1 | +import "@testing-library/jest-dom"; |
| 2 | +import { render, RenderResult } from "@testing-library/react"; |
1 | 3 | import { createElement } from "react"; |
2 | | -import { mount, render } from "enzyme"; |
3 | 4 | import { Icon, IconProps } from "../Icon"; |
4 | 5 |
|
5 | 6 | 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 | + }; |
15 | 12 |
|
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 | + } |
18 | 16 |
|
19 | | - expect(iconWrapper).toMatchSnapshot(); |
20 | | - }); |
| 17 | + describe("Rendering", () => { |
| 18 | + it("renders glyph icons", () => { |
| 19 | + const icon = renderIcon(); |
21 | 20 |
|
22 | | - it("renders image icons", () => { |
23 | | - const iconWrapper = render(<Icon {...defaultIconProps} data={{ type: "image", iconUrl: "icon.url" }} />); |
| 21 | + expect(icon.container).toMatchSnapshot(); |
| 22 | + }); |
24 | 23 |
|
25 | | - expect(iconWrapper).toMatchSnapshot(); |
26 | | - }); |
| 24 | + it("renders image icons", () => { |
| 25 | + const icon = renderIcon({ data: { type: "image", iconUrl: "icon.url" } }); |
27 | 26 |
|
28 | | - it("renders a default icon", () => { |
29 | | - const iconWrapper = render(<Icon {...defaultIconProps} data={undefined} />); |
| 27 | + expect(icon.container).toMatchSnapshot(); |
| 28 | + }); |
30 | 29 |
|
31 | | - expect(iconWrapper).toMatchSnapshot(); |
32 | | - }); |
| 30 | + it("renders a default icon", () => { |
| 31 | + const icon = renderIcon({ data: undefined }); |
33 | 32 |
|
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 | + }); |
36 | 35 |
|
37 | | - expect(iconWrapper).toMatchSnapshot(); |
38 | | - }); |
| 36 | + it("doesn't render a default icon while loading", () => { |
| 37 | + const icon = renderIcon({ data: undefined, loading: true }); |
39 | 38 |
|
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 | + }); |
42 | 41 |
|
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 }); |
45 | 44 |
|
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 | + }); |
50 | 47 | }); |
51 | 48 |
|
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 | + }); |
64 | 72 | }); |
65 | 73 | }); |
0 commit comments