| 1 |
import { render, screen } from '@testing-library/react'; |
| 2 |
import { IconDefinition, CreateIconProps, createIcon, createIconBase, SVGPathObject } from '../createIcon'; |
| 3 |
|
| 4 |
/** Mirrors the non-exported argument type of {@link createIconBase} for tests. */ |
| 5 |
type CreateIconBaseProps = Parameters<typeof createIconBase>[0]; |
| 6 |
|
| 7 |
const multiPathIcon: IconDefinition = { |
| 8 |
name: 'IconName', |
| 9 |
width: 10, |
| 10 |
height: 20, |
| 11 |
svgPathData: [ |
| 12 |
{ path: 'svgPath1', className: 'class1' }, |
| 13 |
{ path: 'svgPath2', className: 'class2' } |
| 14 |
], |
| 15 |
svgClassName: 'test' |
| 16 |
}; |
| 17 |
|
| 18 |
const singlePathIcon: IconDefinition = { |
| 19 |
name: 'IconName', |
| 20 |
width: 10, |
| 21 |
height: 20, |
| 22 |
svgPathData: 'svgPath', |
| 23 |
svgClassName: 'test' |
| 24 |
}; |
| 25 |
|
| 26 |
const rhStandardIcon: IconDefinition = { |
| 27 |
name: 'IconName', |
| 28 |
width: 10, |
| 29 |
height: 20, |
| 30 |
svgPathData: 'svgPath', |
| 31 |
svgClassName: 'pf-v6-icon-rh-standard' |
| 32 |
}; |
| 33 |
|
| 34 |
const iconDef: CreateIconBaseProps = { |
| 35 |
name: 'SinglePathIconName', |
| 36 |
icon: singlePathIcon |
| 37 |
}; |
| 38 |
|
| 39 |
const iconDefWithArrayPath: CreateIconBaseProps = { |
| 40 |
name: 'MultiPathIconName', |
| 41 |
icon: multiPathIcon |
| 42 |
}; |
| 43 |
|
| 44 |
const iconDefWithRhStandard: CreateIconBaseProps = { |
| 45 |
name: 'RhStandardIconName', |
| 46 |
icon: rhStandardIcon |
| 47 |
}; |
| 48 |
|
| 49 |
const SVGIcon = createIconBase(iconDef); |
| 50 |
const SVGArrayIcon = createIconBase(iconDefWithArrayPath); |
| 51 |
const RhStandardIcon = createIconBase(iconDefWithRhStandard); |
| 52 |
|
| 53 |
test('sets correct viewBox', () => { |
| 54 |
render(<SVGIcon />); |
| 55 |
expect(screen.getByRole('img', { hidden: true })).toHaveAttribute( |
| 56 |
'viewBox', |
| 57 |
`0 0 ${singlePathIcon.width} ${singlePathIcon.height}` |
| 58 |
); |
| 59 |
}); |
| 60 |
|
| 61 |
test('sets correct svgPath if string', () => { |
| 62 |
render(<SVGIcon />); |
| 63 |
expect(screen.getByRole('img', { hidden: true }).querySelector('path')).toHaveAttribute( |
| 64 |
'd', |
| 65 |
singlePathIcon.svgPathData |
| 66 |
); |
| 67 |
}); |
| 68 |
|
| 69 |
test('accepts flat createIcon({ svgPath }) shape', () => { |
| 70 |
const legacyDef: CreateIconProps = { |
| 71 |
name: 'LegacyIcon', |
| 72 |
width: 10, |
| 73 |
height: 20, |
| 74 |
svgPath: 'legacy-path', |
| 75 |
svgClassName: 'legacy-svg' |
| 76 |
}; |
| 77 |
const LegacySVGIcon = createIcon(legacyDef); |
| 78 |
render(<LegacySVGIcon />); |
| 79 |
expect(screen.getByRole('img', { hidden: true }).querySelector('path')).toHaveAttribute('d', 'legacy-path'); |
| 80 |
}); |
| 81 |
|
| 82 |
test('sets correct svgClassName by default', () => { |
| 83 |
render(<RhStandardIcon />); |
| 84 |
expect(screen.getByRole('img', { hidden: true })).toHaveClass('pf-v6-icon-rh-standard'); |
| 85 |
}); |
| 86 |
|
| 87 |
test('sets svgClassName when noDefaultStyle is false', () => { |
| 88 |
render(<RhStandardIcon noDefaultStyle={false} />); |
| 89 |
expect(screen.getByRole('img', { hidden: true })).toHaveClass('pf-v6-icon-rh-standard'); |
| 90 |
}); |
| 91 |
|
| 92 |
test('does not set svgClassName when noDefaultStyle is true', () => { |
| 93 |
render(<RhStandardIcon noDefaultStyle />); |
| 94 |
expect(screen.getByRole('img', { hidden: true })).not.toHaveClass('pf-v6-icon-rh-standard'); |
| 95 |
}); |
| 96 |
|
| 97 |
test('sets correct svgPath if array', () => { |
| 98 |
render(<SVGArrayIcon />); |
| 99 |
const paths = screen.getByRole('img', { hidden: true }).querySelectorAll('path'); |
| 100 |
expect(paths).toHaveLength(2); |
| 101 |
expect(paths[0]).toHaveAttribute('d', (multiPathIcon.svgPathData as SVGPathObject[])[0].path); |
| 102 |
expect(paths[1]).toHaveAttribute('d', (multiPathIcon.svgPathData as SVGPathObject[])[1].path); |
| 103 |
expect(paths[0]).toHaveClass((multiPathIcon.svgPathData as SVGPathObject[])[0].className ?? ''); |
| 104 |
expect(paths[1]).toHaveClass((multiPathIcon.svgPathData as SVGPathObject[])[1].className ?? ''); |
| 105 |
}); |
| 106 |
|
| 107 |
test('sets correct svgClassName', () => { |
| 108 |
render(<SVGArrayIcon />); |
| 109 |
const paths = screen.getByRole('img', { hidden: true }); |
| 110 |
expect(paths).toHaveClass(multiPathIcon.svgClassName ?? ''); |
| 111 |
}); |
| 112 |
|
| 113 |
test('aria-hidden is true if no title is specified', () => { |
| 114 |
render(<SVGIcon />); |
| 115 |
expect(screen.getByRole('img', { hidden: true })).toHaveAttribute('aria-hidden', 'true'); |
| 116 |
}); |
| 117 |
|
| 118 |
test('title is not rendered if a title is not passed', () => { |
| 119 |
render(<SVGIcon />); |
| 120 |
expect(screen.queryByRole('img', { hidden: true })?.querySelector('title')).toBeNull(); |
| 121 |
}); |
| 122 |
|
| 123 |
test('aria-labelledby is null if a title is not passed', () => { |
| 124 |
render(<SVGIcon />); |
| 125 |
expect(screen.getByRole('img', { hidden: true })).not.toHaveAttribute('aria-labelledby'); |
| 126 |
}); |
| 127 |
|
| 128 |
test('title is rendered', () => { |
| 129 |
const title = 'icon title'; |
| 130 |
|
| 131 |
render(<SVGIcon title={title} />); |
| 132 |
expect(screen.getByText(title)).toBeInTheDocument(); |
| 133 |
}); |
| 134 |
|
| 135 |
test('aria-labelledby matches title id', () => { |
| 136 |
render(<SVGIcon title="icon title" />); |
| 137 |
|
| 138 |
const svg = screen.getByRole('img', { hidden: true }); |
| 139 |
const labelledby = svg.getAttribute('aria-labelledby'); |
| 140 |
const titleId = svg.querySelector('title')?.getAttribute('id'); |
| 141 |
|
| 142 |
expect(labelledby).toEqual(titleId); |
| 143 |
}); |
| 144 |
|
| 145 |
test('additional props should be spread to the root svg element', () => { |
| 146 |
render(<SVGIcon data-testid="icon" />); |
| 147 |
expect(screen.getByTestId('icon')).toBeInTheDocument(); |
| 148 |
}); |
| 149 |
|
| 150 |
describe('rh-ui mapping: nested SVGs, set prop, and warnings', () => { |
| 151 |
const defaultPath = 'M0 0-default'; |
| 152 |
const rhUiPath = 'M0 0-rh-ui'; |
| 153 |
|
| 154 |
const defaultIconDef: IconDefinition = { |
| 155 |
name: 'DefaultVariant', |
| 156 |
width: 16, |
| 157 |
height: 16, |
| 158 |
svgPathData: defaultPath |
| 159 |
}; |
| 160 |
|
| 161 |
const rhUiIconDef: IconDefinition = { |
| 162 |
name: 'RhUiVariant', |
| 163 |
width: 16, |
| 164 |
height: 16, |
| 165 |
svgPathData: rhUiPath |
| 166 |
}; |
| 167 |
|
| 168 |
const dualConfig: CreateIconBaseProps = { |
| 169 |
name: 'DualMappedIcon', |
| 170 |
icon: defaultIconDef, |
| 171 |
rhUiIcon: rhUiIconDef |
| 172 |
}; |
| 173 |
|
| 174 |
const DualMappedIcon = createIconBase(dualConfig); |
| 175 |
|
| 176 |
test('renders two nested inner svgs when rhUiIcon is set and `set` is omitted (swap layout)', () => { |
| 177 |
render(<DualMappedIcon />); |
| 178 |
const root = screen.getByRole('img', { hidden: true }); |
| 179 |
expect(root).toHaveClass('pf-v6-svg'); |
| 180 |
const innerSvgs = root.querySelectorAll(':scope > svg'); |
| 181 |
expect(innerSvgs).toHaveLength(2); |
| 182 |
expect(root?.querySelector('.pf-v6-icon-default path')).toHaveAttribute('d', defaultPath); |
| 183 |
expect(root?.querySelector('.pf-v6-icon-rh-ui path')).toHaveAttribute('d', rhUiPath); |
| 184 |
}); |
| 185 |
|
| 186 |
test('set="default" renders a single flat svg using the default icon paths', () => { |
| 187 |
render(<DualMappedIcon set="default" />); |
| 188 |
const root = screen.getByRole('img', { hidden: true }); |
| 189 |
expect(root.querySelectorAll(':scope > svg')).toHaveLength(0); |
| 190 |
expect(root).toHaveAttribute('viewBox', '0 0 16 16'); |
| 191 |
expect(root.querySelector('path')).toHaveAttribute('d', defaultPath); |
| 192 |
expect(root.querySelectorAll('svg')).toHaveLength(0); |
| 193 |
}); |
| 194 |
|
| 195 |
test('set="rh-ui" renders a single flat svg using the rh-ui icon paths', () => { |
| 196 |
render(<DualMappedIcon set="rh-ui" />); |
| 197 |
const root = screen.getByRole('img', { hidden: true }); |
| 198 |
expect(root.querySelectorAll(':scope > svg')).toHaveLength(0); |
| 199 |
expect(root.querySelector('path')).toHaveAttribute('d', rhUiPath); |
| 200 |
expect(root.querySelectorAll('svg')).toHaveLength(0); |
| 201 |
}); |
| 202 |
|
| 203 |
test('set="rh-ui" with no rhUiIcon mapping falls back to default and warns', () => { |
| 204 |
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 205 |
try { |
| 206 |
const IconNoRhMapping = createIconBase({ |
| 207 |
name: 'NoRhMappingIcon', |
| 208 |
icon: defaultIconDef, |
| 209 |
rhUiIcon: null |
| 210 |
}); |
| 211 |
|
| 212 |
render(<IconNoRhMapping set="rh-ui" />); |
| 213 |
|
| 214 |
expect(warnSpy).toHaveBeenCalledWith( |
| 215 |
'Set "rh-ui" was provided for NoRhMappingIcon, but no rh-ui icon data exists for this icon. The default icon will be rendered.' |
| 216 |
); |
| 217 |
const root = screen.getByRole('img', { hidden: true }); |
| 218 |
expect(root.querySelector('path')).toHaveAttribute('d', defaultPath); |
| 219 |
expect(root.querySelectorAll('svg')).toHaveLength(0); |
| 220 |
} finally { |
| 221 |
warnSpy.mockRestore(); |
| 222 |
} |
| 223 |
}); |
| 224 |
|
| 225 |
test('warns when createIconBase omits icon', () => { |
| 226 |
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 227 |
createIconBase({ |
| 228 |
name: 'MissingDefaultIcon', |
| 229 |
rhUiIcon: null |
| 230 |
}); |
| 231 |
expect(warnSpy).toHaveBeenCalledWith( |
| 232 |
'@patternfly/react-icons: createIconBase is missing an `icon` definition (name: MissingDefaultIcon).' |
| 233 |
); |
| 234 |
}); |
| 235 |
}); |