Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(WriteBarIcon): update missing label warning logic #6385

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions packages/vkui/src/components/WriteBarIcon/WriteBarIcon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@ import { render, screen } from '@testing-library/react';
import { baselineComponent } from '../../testing/utils';
import { WriteBarIcon } from './WriteBarIcon';

const warnStub = jest.fn();
jest.mock('../../lib/warnOnce', () => {
const originalModule = jest.requireActual('../../lib/warnOnce');

return {
__esModule: true,
...originalModule,
warnOnce: () => {
return () => {
warnStub();
};
},
};
});

describe('WriteBarIcon', () => {
beforeEach(() => warnStub.mockReset());
baselineComponent((props) => <WriteBarIcon aria-label="WriteBarIcon" {...props} />);

it('a11y: adds default aria-label for assigned mode', () => {
Expand All @@ -12,6 +28,38 @@ describe('WriteBarIcon', () => {
expect(screen.getByTestId('button')).toHaveAttribute('aria-label', 'Отправить');
});

it('a11y: warns when there is no label text', () => {
const nodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';

render(<WriteBarIcon data-testid="button" mode={undefined} />);

expect(warnStub).toHaveBeenCalledTimes(1);

process.env.NODE_ENV = nodeEnv;
});

it('a11y: does not warn when there is label text', () => {
const nodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';

const { rerender } = render(<WriteBarIcon data-testid="button" mode="send" />);
expect(warnStub).not.toHaveBeenCalled();

rerender(<WriteBarIcon aria-label="send" />);
expect(warnStub).not.toHaveBeenCalled();

rerender(
<React.Fragment>
<WriteBarIcon aria-labelledby="send" />
<div id="send">Send</div>
</React.Fragment>,
);
expect(warnStub).not.toHaveBeenCalled();

process.env.NODE_ENV = nodeEnv;
});

it('shows counter when count={0} is provided', () => {
const count = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const WriteBarIcon = ({
}

if (process.env.NODE_ENV === 'development') {
const isAccessible = !modeLabel && (!restProps['aria-label'] || restProps['aria-labelledby']);
const isAccessible = modeLabel || restProps['aria-label'] || restProps['aria-labelledby'];

if (!isAccessible) {
warn(COMMON_WARNINGS.a11y['button-name'], 'error');
Expand Down
Loading