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