@@ -3,18 +3,60 @@ import faker from "faker";
33import { HeadingIcon } from "./heading-icon" ;
44import { render } from "@testing-library/react" ;
55import { Icons } from "../constants/icons" ;
6+ import { IconUtils } from "../../utilities/icon-utils" ;
7+ import { SvgIcon } from "../interfaces/svg-icon" ;
8+ import { getSvgIconByType } from "../constants/svg-icons" ;
9+ import { Icon } from "../icons/icon" ;
610
711describe ( "HeadingIcon" , ( ) => {
8- test ( "when default props, renders Heading with Icon" , ( ) => {
12+ let registeredIcon : SvgIcon ;
13+
14+ beforeEach ( ( ) => {
15+ IconUtils . clearRegistry ( ) ;
16+ registeredIcon = getSvgIconByType ( Icons . ChevronUp ) ;
17+ IconUtils . registerSvgIcon ( registeredIcon ) ;
18+ } ) ;
19+
20+ test ( "when default props, renders Heading with icon" , ( ) => {
921 // Arrange
1022 const expected = faker . random . words ( ) ;
1123
1224 // Act
1325 const { getByText } = render (
14- < HeadingIcon type = { Icons . Checkmark } > { expected } </ HeadingIcon >
26+ < HeadingIcon type = { registeredIcon . type } > { expected } </ HeadingIcon >
1527 ) ;
1628
1729 // Assert
1830 expect ( getByText ( expected ) ) . not . toBeNil ( ) ;
1931 } ) ;
32+
33+ test ( "when type prop set and icon exists, renders Heading with set icon" , ( ) => {
34+ // Arrange
35+ const testHeading = faker . random . words ( ) ;
36+
37+ // Act
38+ const { container } = render (
39+ < HeadingIcon type = { registeredIcon . type } > { testHeading } </ HeadingIcon >
40+ ) ;
41+ const svgIcon = container . getElementsByTagName ( "svg" ) ;
42+
43+ // Assert
44+ expect ( svgIcon ) . toHaveLength ( 1 ) ;
45+ } ) ;
46+
47+ test ( "when type prop set and icon does not exist, renders Heading with bare icon <i>" , ( ) => {
48+ // Arrange
49+ const expected = faker . random . words ( ) ;
50+
51+ // Act
52+ const { container } = render (
53+ < HeadingIcon type = { Icons . Lightbulb } > { expected } </ HeadingIcon >
54+ ) ;
55+ const svgIcon = container . getElementsByTagName ( "svg" ) ;
56+ const bareIcon = container . getElementsByTagName ( "i" ) ;
57+
58+ // Assert
59+ expect ( svgIcon ) . toHaveLength ( 0 ) ;
60+ expect ( bareIcon ) . toHaveLength ( 1 ) ;
61+ } ) ;
2062} ) ;
0 commit comments