Skip to content

Commit 669a9a4

Browse files
committed
chore: test @shared/UsaIcon.jsx #4668
1 parent f78a95f commit 669a9a4

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

frontend/shared/UsaIcon.jsx

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33

4-
function UsaIcon({ focusable = false, role = 'img', name, size = null }) {
4+
function UsaIcon({ role = 'img', name, size = null }) {
55
return (
66
<svg
77
className={size ? `usa-icon usa-icon--size-${size}` : 'usa-icon'}
8-
aria-hidden="true"
9-
focusable={focusable}
8+
focusable={role === 'button' || role === 'link'}
109
role={role}
1110
>
12-
<use xlinkHref={`/img/sprite.svg#${name}`} />
11+
<use href={`/img/sprite.svg#${name}`} />
1312
</svg>
1413
);
1514
}
1615

1716
UsaIcon.propTypes = {
18-
role: PropTypes.string,
19-
focusable: PropTypes.bool,
17+
role: PropTypes.oneOf(['img', 'presentation', 'button', 'link', 'status', 'none']),
2018
name: PropTypes.string.isRequired,
21-
size: PropTypes.number,
22-
};
23-
24-
UsaIcon.defaultProps = {
25-
role: 'img',
26-
focusable: false,
27-
size: null,
19+
size: PropTypes.oneOf([3, 4, 5, 6, 7, 8, 9]),
2820
};
2921

3022
export default UsaIcon;

frontend/shared/UsaIcon.test.jsx

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import UsaIcon from './UsaIcon';
5+
6+
describe('<UsaIcon />', () => {
7+
it('renders an SVG icon with USDWDS classes', () => {
8+
render(<UsaIcon name="check" />);
9+
const svgElement = screen.getByRole('img');
10+
expect(svgElement).toBeInstanceOf(SVGSVGElement);
11+
expect(svgElement).toHaveClass('usa-icon');
12+
});
13+
14+
it('renders the icon as unfocusable img role', () => {
15+
render(<UsaIcon name="check" />);
16+
const svgElement = screen.getByRole('img');
17+
expect(svgElement).toHaveAttribute('focusable', 'false');
18+
});
19+
20+
it('renders the icon as focusable when the role is button', () => {
21+
render(<UsaIcon name="check" role="button" />);
22+
const svgElement = screen.getByRole('button');
23+
expect(svgElement).toHaveAttribute('focusable', 'true');
24+
});
25+
26+
it('renders the icon as focusable when the role is link', () => {
27+
render(<UsaIcon name="check" role="link" />);
28+
const svgElement = screen.getByRole('link');
29+
expect(svgElement).toHaveAttribute('focusable', 'true');
30+
});
31+
32+
it('renders the icon with a size class when size is set', () => {
33+
render(<UsaIcon name="check" size={4} />);
34+
const svgElement = screen.getByRole('img');
35+
expect(svgElement).toHaveClass('usa-icon--size-4');
36+
});
37+
38+
it('uses a role attribute when provided', () => {
39+
render(<UsaIcon name="check" role="presentation" />);
40+
const svgElement = screen.getByRole('presentation');
41+
expect(svgElement).toBeInTheDocument();
42+
});
43+
});

0 commit comments

Comments
 (0)