Skip to content

Commit 8032fef

Browse files
mattxwangsmalex-z
andauthored
tests: adds aXe test for <Officers />; resolves key bug (#507)
* tests: adds aXe test for `<Officers />`; resolves `key` bug * lint fixes and remove next image req. in eslint --------- Co-authored-by: Alex Zheng <[email protected]> Co-authored-by: smalex-z <[email protected]>
1 parent ab64f95 commit 8032fef

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = {
3131
'react/prop-types': 'off',
3232
'react/no-unknown-property': 'off',
3333
'react/react-in-jsx-scope': 'off',
34+
'@next/next/no-img-element': 'off',
3435

3536
// many of these rules are taken from our friends at Creative Labs;
3637
// see their config here: https://github.com/UCLA-Creative-Labs/sunshine/blob/master/.eslintrc.js
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { render } from '@testing-library/react';
2+
import { axe, toHaveNoViolations } from 'jest-axe';
3+
import React from 'react';
4+
import { act } from 'react-dom/test-utils';
5+
import Officers from '../../components/LeadershipOfficers';
6+
import data from '../../data';
7+
8+
expect.extend(toHaveNoViolations);
9+
10+
// TODO: consider lifting this mock globally
11+
jest.mock('next/image', () => ({
12+
__esModule: true,
13+
default: (props) => {
14+
// eslint-disable-next-line @next/next/no-img-element
15+
return <img alt={props.alt} {...props} />;
16+
},
17+
}));
18+
19+
it('has no axe violations', async () => {
20+
const { leadership } = data;
21+
22+
// this let / async construct is required since next/image rendering
23+
// is a side effect that needs to be captured with async act
24+
// see https://reactjs.org/docs/test-utils.html#act
25+
let results;
26+
await act(async () => {
27+
const { container } = render(<Officers officers={leadership} />);
28+
results = await axe(container);
29+
});
30+
31+
expect(results).toHaveNoViolations();
32+
});

components/LeadershipOfficers.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ function Officer(props) {
2323
function Officers(props) {
2424
return (
2525
// TODO: more flexible mobile views
26-
(<div className="grid-desktop-3">
27-
{props.officers.map((officer) => (
28-
<Officer officer={officer} key={officer.image} />
29-
))}
30-
</div>)
26+
<div className="grid-desktop-3">
27+
{
28+
props.officers.map(
29+
officer => <Officer officer={officer} key={officer.name + officer.position} />,
30+
)
31+
}
32+
</div>
3133
);
3234
}
3335

0 commit comments

Comments
 (0)