/
SvgIcon.test.js
146 lines (121 loc) · 4.74 KB
/
SvgIcon.test.js
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
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer, describeConformance } from 'test/utils';
import SvgIcon, { svgIconClasses as classes } from '@mui/joy/SvgIcon';
import { ThemeProvider } from '@mui/joy/styles';
import { unstable_capitalize as capitalize } from '@mui/utils';
describe('<SvgIcon />', () => {
const { render } = createRenderer();
let path;
before(() => {
path = <path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" data-testid="test-path" />;
});
describeConformance(
<SvgIcon>
<path />
</SvgIcon>,
() => ({
classes,
inheritComponent: 'svg',
ThemeProvider,
render,
muiName: 'JoySvgIcon',
refInstanceof: window.SVGSVGElement,
testComponentPropWith: (props) => (
<svg {...props}>
<defs>
<linearGradient id="gradient1">
<stop offset="20%" stopColor="#39F" />
<stop offset="90%" stopColor="#F3F" />
</linearGradient>
</defs>
{props.children}
</svg>
),
slots: {
root: { expectedClassName: classes.root, testWithComponent: 'svg' },
},
skip: ['themeVariants', 'classesRoot', 'componentsProp'],
}),
);
it('renders children by default', () => {
const { container, queryByTestId } = render(<SvgIcon>{path}</SvgIcon>);
expect(queryByTestId('test-path')).not.to.equal(null);
expect(container.firstChild).to.have.attribute('aria-hidden', 'true');
});
describe('prop: titleAccess', () => {
it('should be able to make an icon accessible', () => {
const { container, queryByText } = render(
<SvgIcon title="Go to link" titleAccess="Network">
{path}
</SvgIcon>,
);
expect(queryByText('Network')).not.to.equal(null);
expect(container.firstChild).not.to.have.attribute('aria-hidden');
});
});
describe('prop: color', () => {
it('should render with the user and SvgIcon classes', () => {
const { container } = render(<SvgIcon className="meow">{path}</SvgIcon>);
expect(container.firstChild).to.have.class('meow');
});
it('should render with `inherit` by default', () => {
const { container } = render(<SvgIcon>{path}</SvgIcon>);
expect(container.firstChild).to.have.class(classes.colorInherit);
});
['inherit', 'primary', 'success', 'info', 'danger', 'neutral', 'warning'].forEach((color) => {
it(`should render ${color}`, () => {
const { container } = render(<SvgIcon color={color}>{path}</SvgIcon>);
expect(container.firstChild).to.have.class(classes[`color${capitalize(color)}`]);
});
});
});
describe('prop: fontSize', function test() {
before(function beforeHook() {
// Only run the test on node.
if (!/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
});
it('should render with `xl` by default', () => {
const { container } = render(<SvgIcon>{path}</SvgIcon>);
expect(container.firstChild).to.have.class(classes.fontSizeXl);
});
['inherit', 'xs', 'sm', 'md', 'lg', 'xl', 'xl2', 'xl3', 'xl4', 'xl5', 'xl6'].forEach(
(fontSize) => {
it(`should render ${fontSize}`, () => {
const { container } = render(<SvgIcon fontSize={fontSize}>{path}</SvgIcon>);
expect(container.firstChild).to.have.class(classes[`fontSize${capitalize(fontSize)}`]);
});
},
);
});
describe('prop: inheritViewBox', () => {
function CustomSvg(props) {
return (
<svg viewBox="-4 -4 24 24" {...props}>
{path}
</svg>
);
}
it('should render with the default viewBox if neither inheritViewBox nor viewBox are provided', () => {
const { container } = render(<SvgIcon component={CustomSvg} />);
expect(container.firstChild).to.have.attribute('viewBox', '0 0 24 24');
});
it('should render with given viewBox if inheritViewBox is not provided', () => {
const { container } = render(<SvgIcon component={CustomSvg} viewBox="0 0 30 30" />);
expect(container.firstChild).to.have.attribute('viewBox', '0 0 30 30');
});
it("should use the custom component's viewBox if true", () => {
const { container } = render(<SvgIcon component={CustomSvg} inheritViewBox />);
expect(container.firstChild).to.have.attribute('viewBox', '-4 -4 24 24');
});
});
it('should not override internal ownerState with the ownerState passed to the icon', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
const { container } = render(<SvgIcon ownerState={{ fontSize: 'sm' }}>{path}</SvgIcon>);
expect(container.firstChild).toHaveComputedStyle({ fontSize: '20px' }); // fontSize: xl -> 1.25rem = 20px
});
});