Skip to content

Commit

Permalink
fix(TabList): add console warn if missing aria-label
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiccarrington committed Nov 7, 2024
1 parent dcd782c commit 6df8005
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/components/TabList/TabList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const TabList: FC<Props> = (props: Props) => {
? (_children as ReactElement<TabProps>[])
: [_children];

if (otherProps['aria-labelledby'] === undefined && otherProps['aria-label'] === undefined) {
console.warn('MRV2: TabList requires aria-labelledby or aria-label.');
}

const buttonGroupProps = defaultsDeep({}, otherProps, {
round: true,
spaced: true,
Expand Down
47 changes: 47 additions & 0 deletions src/components/TabList/TabList.unit.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */

import { mount } from 'enzyme';
import React from 'react';

Expand Down Expand Up @@ -178,6 +180,51 @@ describe('<TabList />', () => {
});
});
});

it('requires an label', () => {
const consoleWarnSpy = jest.spyOn(console, 'warn');

const wrapper = mount(
// @ts-expect-error
<TabList>
<Tab key="one">Hello</Tab>
</TabList>
);

expect(consoleWarnSpy).toHaveBeenCalledWith(
'MRV2: TabList requires aria-labelledby or aria-label.'
);

consoleWarnSpy.mockReset();
});

it('does not warn if aria-label defined', () => {
const consoleWarnSpy = jest.spyOn(console, 'warn');

mount(
<TabList aria-label="Hello">
<Tab key="one">Hello</Tab>
</TabList>
);

expect(consoleWarnSpy).not.toHaveBeenCalled();

consoleWarnSpy.mockReset();
});

it('does not warn if aria-labelledby defined', () => {
const consoleWarnSpy = jest.spyOn(console, 'warn');

mount(
<TabList aria-labelledby="Hello">
<Tab key="one">Hello</Tab>
</TabList>
);

expect(consoleWarnSpy).not.toHaveBeenCalled();

consoleWarnSpy.mockReset();
});
});

describe('actions', () => {
Expand Down

0 comments on commit 6df8005

Please sign in to comment.