|
| 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