Skip to content

Commit ba66a34

Browse files
authored
fix(sub-header): add accessibility label to SortButton (#3178)
* fix(sub-header): add accessibility label to SortButton * fix(sub-header): add IntlShape type * fix(sub-header): add tests * fix(sub-header): adjust test names
1 parent 2553713 commit ba66a34

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

src/elements/common/sub-header/SortButton.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,31 @@
55
*/
66

77
import React from 'react';
8-
import { FormattedMessage } from 'react-intl';
8+
import { injectIntl } from 'react-intl';
9+
import type { IntlShape } from 'react-intl';
10+
911
import Button from '../../../components/button';
1012
import IconSort from '../../../icons/general/IconSort';
11-
import messages from '../messages';
1213
import Tooltip from '../Tooltip';
14+
15+
import messages from '../messages';
16+
1317
import './SortButton.scss';
1418

15-
const SortButton = (props: ?Object) => (
16-
<Tooltip text={<FormattedMessage {...messages.sort} />}>
17-
<Button className="be-btn-sort" type="button" {...props}>
18-
<IconSort />
19-
</Button>
20-
</Tooltip>
21-
);
19+
type Props = {
20+
intl: IntlShape,
21+
};
22+
23+
const SortButton = ({ intl, ...rest }: Props) => {
24+
const sortMessage = intl.formatMessage(messages.sort);
25+
return (
26+
<Tooltip text={sortMessage}>
27+
<Button aria-label={sortMessage} className="be-btn-sort" type="button" {...rest}>
28+
<IconSort />
29+
</Button>
30+
</Tooltip>
31+
);
32+
};
2233

23-
export default SortButton;
34+
export { SortButton as SortButtonBase };
35+
export default injectIntl(SortButton);

src/elements/common/sub-header/__tests__/Sort.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import SortButton from '../SortButton';
77
import messages from '../../messages';
88
import { SORT_ASC, SORT_DESC } from '../../../../constants';
99

10-
describe('elements/SubHeader/Sort', () => {
10+
describe('elements/common/sub-header/Sort', () => {
1111
test('should render a button and menu with 4 menu items', () => {
1212
const wrapper = shallow(<Sort onSortChange={jest.fn()} sortBy="name" sortDirection={SORT_ASC} />);
1313

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import Button from '../../../../components/button';
3+
import IconSort from '../../../../icons/general/IconSort';
4+
import { SortButtonBase } from '../SortButton';
5+
6+
const intlMock = {
7+
formatMessage: jest.fn().mockReturnValue('Sort'),
8+
};
9+
10+
describe('elements/common/sub-header/SortButton', () => {
11+
const getWrapper = () => shallow(<SortButtonBase intl={intlMock} />);
12+
13+
test('should render IconSort', () => {
14+
const wrapper = getWrapper();
15+
expect(wrapper.exists(IconSort)).toBe(true);
16+
});
17+
18+
test('should have aria-label "Sort"', () => {
19+
const wrapper = getWrapper();
20+
const button = wrapper.find(Button);
21+
expect(button.prop('aria-label')).toBe('Sort');
22+
expect(button.prop('aria-describedby')).toBeFalsy();
23+
});
24+
});

src/elements/common/sub-header/__tests__/SubHeaderRight.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { VIEW_FOLDER, VIEW_MODE_GRID } from '../../../../constants';
88

99
const getWrapper = props => shallow(<SubHeaderRight {...props} />);
1010

11-
describe('Elements/SubHeader/SubHeaderRight', () => {
11+
describe('elements/common/sub-header/SubHeaderRight', () => {
1212
const currentCollection = {
1313
sortBy: '',
1414
sortDirection: '',

0 commit comments

Comments
 (0)