From 9504b56c9f2ef926f3fd2fd97f9870357125daaa Mon Sep 17 00:00:00 2001 From: Andrey Medvedev Date: Wed, 17 Jan 2024 11:24:45 +0300 Subject: [PATCH] Fix WriteBarIcon missing label warning logic (#6385) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Предупреждение о том, что у WriteBarIcon отсутствует текстовое описание выводится даже если текстовое описание есть. В v6 эта проблема решена чуть иначе. --- .../WriteBarIcon/WriteBarIcon.test.tsx | 48 +++++++++++++++++++ .../components/WriteBarIcon/WriteBarIcon.tsx | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/packages/vkui/src/components/WriteBarIcon/WriteBarIcon.test.tsx b/packages/vkui/src/components/WriteBarIcon/WriteBarIcon.test.tsx index 24a9c14299..9e367125ae 100644 --- a/packages/vkui/src/components/WriteBarIcon/WriteBarIcon.test.tsx +++ b/packages/vkui/src/components/WriteBarIcon/WriteBarIcon.test.tsx @@ -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) => ); it('a11y: adds default aria-label for assigned mode', () => { @@ -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(); + + 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(); + expect(warnStub).not.toHaveBeenCalled(); + + rerender(); + expect(warnStub).not.toHaveBeenCalled(); + + rerender( + + +
Send
+
, + ); + expect(warnStub).not.toHaveBeenCalled(); + + process.env.NODE_ENV = nodeEnv; + }); + it('shows counter when count={0} is provided', () => { const count = 0; diff --git a/packages/vkui/src/components/WriteBarIcon/WriteBarIcon.tsx b/packages/vkui/src/components/WriteBarIcon/WriteBarIcon.tsx index 352fe46ad8..4de2269d83 100644 --- a/packages/vkui/src/components/WriteBarIcon/WriteBarIcon.tsx +++ b/packages/vkui/src/components/WriteBarIcon/WriteBarIcon.tsx @@ -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');