Skip to content

Commit c5be1a1

Browse files
committed
Tests for icon atom component
1 parent 9377511 commit c5be1a1

3 files changed

Lines changed: 73 additions & 4 deletions

File tree

src/atoms/anchors/anchor.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Anchor } from "./anchor";
44
import faker from "faker";
55
import { MemoryRouter } from "react-router-dom";
66

7-
describe("anchor", () => {
7+
describe("Anchor", () => {
88
it("when default props, renders link with correct url", async () => {
99
// Arrange
1010
const expected = `/some/random/path/${faker.random.word()}`;

src/atoms/buttons/button.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import React from "react";
2-
import { act, render, wait, fireEvent } from "@testing-library/react";
2+
import { render, wait } from "@testing-library/react";
33
import { Button } from "./button";
44
import faker from "faker";
5-
import { MemoryRouter } from "react-router-dom";
65
import { ButtonSizes } from "../constants/button-sizes";
76
import { ButtonStyles } from "../constants/button-styles";
87

9-
describe("button", () => {
8+
describe("Button", () => {
109
it("when default props, renders button children", async () => {
1110
// Arrange
1211
const expected = faker.random.words();

src/atoms/icons/icon.test.tsx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import React from "react";
2+
import { render, wait } from "@testing-library/react";
3+
import { Icon } from "./icon";
4+
import faker from "faker";
5+
import { Icons } from "../constants/icons";
6+
import { IconUtils } from "../../utilities/icon-utils";
7+
import { getSvgIconByType } from "../constants/svg-icons";
8+
import { IconSizes } from "../constants/icon-sizes";
9+
import { SvgIcon } from "../interfaces/svg-icon";
10+
11+
describe("Icon", () => {
12+
test("given icon type not found, when default props, renders bare icon <i>", async () => {
13+
// Arrange & Act
14+
const { container } = render(<Icon type={Icons.Checkmark} />);
15+
16+
// Assert
17+
expect(container.firstChild.nodeName).toBe("I");
18+
});
19+
20+
describe("given icon type exists", () => {
21+
let registeredIcon: SvgIcon;
22+
23+
beforeEach(() => {
24+
IconUtils.clearRegistry();
25+
registeredIcon = getSvgIconByType(Icons.ChevronUp);
26+
IconUtils.registerSvgIcon(registeredIcon);
27+
});
28+
29+
test("when default props, renders matching base icon", async () => {
30+
// Arrange & Act
31+
const { container } = render(<Icon type={registeredIcon.type} />);
32+
33+
// Assert
34+
expect(container.firstChild.nodeName).toBe("svg");
35+
});
36+
37+
test.each`
38+
size
39+
${IconSizes.Base}
40+
${IconSizes.Large}
41+
`("when size of $size, renders icon", async ({ size }) => {
42+
// Arrange & Act
43+
const { container } = render(
44+
<Icon size={size} type={registeredIcon.type} />
45+
);
46+
47+
// Assert
48+
expect(container.firstChild.nodeName).toBe("svg");
49+
expect(container.firstChild.getAttribute("class")).toContain(
50+
`-${size}`
51+
);
52+
});
53+
54+
test("when cssClassName provided, renders with className set", async () => {
55+
// Arrange
56+
const expected = faker.random.word();
57+
58+
// Act
59+
const { container } = render(
60+
<Icon cssClassName={expected} type={registeredIcon.type} />
61+
);
62+
63+
// Assert
64+
expect(container.firstChild.nodeName).toBe("svg");
65+
expect(container.firstChild.getAttribute("class")).toContain(
66+
expected
67+
);
68+
});
69+
}); // end 'given icon type exists'
70+
});

0 commit comments

Comments
 (0)