From 615b1fa5ce930f369110e1fae3ce75617ce334dc Mon Sep 17 00:00:00 2001 From: adamviktora <84135613+adamviktora@users.noreply.github.com> Date: Fri, 19 Apr 2024 15:44:32 +0200 Subject: [PATCH] test(Label): add tests for disabled labels (#10254) --- .../react-core/src/components/Label/Label.tsx | 2 +- .../components/Label/__tests__/Label.test.tsx | 89 ++++++++++++++++++- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/packages/react-core/src/components/Label/Label.tsx b/packages/react-core/src/components/Label/Label.tsx index 8c827374510..9ce1fbb57d0 100644 --- a/packages/react-core/src/components/Label/Label.tsx +++ b/packages/react-core/src/components/Label/Label.tsx @@ -284,7 +284,7 @@ export const Label: React.FunctionComponent = ({ ...editableProps }), ...(isClickableDisabled && isButton && { disabled: true }), - ...(isClickableDisabled && href && { tabindex: -1, 'aria-disabled': true }) + ...(isClickableDisabled && href && { tabIndex: -1, 'aria-disabled': true }) }; let labelComponentChild = ( diff --git a/packages/react-core/src/components/Label/__tests__/Label.test.tsx b/packages/react-core/src/components/Label/__tests__/Label.test.tsx index 519d99969cd..57600d48a5c 100644 --- a/packages/react-core/src/components/Label/__tests__/Label.test.tsx +++ b/packages/react-core/src/components/Label/__tests__/Label.test.tsx @@ -142,6 +142,93 @@ describe('Label', () => { await user.click(label); - expect(mockCallback).toBeCalledTimes(1); + expect(mockCallback).toHaveBeenCalledTimes(1); + }); + + test('disabled clickable label does not call passed callback on click', async () => { + const mockCallback = jest.fn(); + const user = userEvent.setup(); + + render( + + ); + + const label = screen.getByText('Click me'); + + await user.click(label); + + expect(mockCallback).not.toHaveBeenCalled(); + }); + + test('disabled clickable label is a disabled button', async () => { + const mockCallback = jest.fn(); + + render( + + ); + + const labelButton = screen.getByRole('button'); + + expect(labelButton).toHaveAttribute('disabled'); + }); + + test('link label is an anchor', () => { + const href = '#example'; + + render(); + + const anchor = screen.getByRole('link', { name: 'Click me' }); + + expect(anchor).toBeInTheDocument(); + expect(anchor).toHaveAttribute('href', href); + }); + + test('disabled link label is an anchor with aria-disabled attribute', () => { + const href = '#example'; + + render( + + ); + + const anchor = screen.getByRole('link', { name: 'Click me' }); + + expect(anchor).toBeInTheDocument(); + expect(anchor).toHaveAttribute('href', href); + expect(anchor).toHaveAttribute('tabIndex', '-1'); + expect(anchor).toHaveAttribute('aria-disabled', 'true'); + }); + + test('disabled removable clickable label has a disabled close button', async () => { + const mockCallback = jest.fn(); + + render( + + ); + + const closeButton = screen.getByLabelText('Close Click me'); + + expect(closeButton).toBeDisabled(); + }); + + test('disabled removable link label has a disabled close button', async () => { + const mockCallback = jest.fn(); + + render( + + ); + + const closeButton = screen.getByLabelText('Close Click me'); + + expect(closeButton).toBeDisabled(); }); });