Skip to content

Commit

Permalink
Fix WriteBarIcon missing label warning logic (#6385)
Browse files Browse the repository at this point in the history
Предупреждение о том, что у WriteBarIcon отсутствует текстовое описание выводится даже если текстовое описание есть.

В v6 эта проблема решена чуть иначе.
  • Loading branch information
mendrew authored and actions-user committed Jan 17, 2024
1 parent 7f1888d commit 9504b56
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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
2 changes: 1 addition & 1 deletion packages/vkui/src/components/WriteBarIcon/WriteBarIcon.tsx
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

0 comments on commit 9504b56

Please sign in to comment.