Skip to content

Commit

Permalink
test(Label): add tests for disabled labels (#10254)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamviktora authored Apr 19, 2024
1 parent 0a64acd commit 615b1fa
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/react-core/src/components/Label/Label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export const Label: React.FunctionComponent<LabelProps> = ({
...editableProps
}),
...(isClickableDisabled && isButton && { disabled: true }),
...(isClickableDisabled && href && { tabindex: -1, 'aria-disabled': true })
...(isClickableDisabled && href && { tabIndex: -1, 'aria-disabled': true })
};

let labelComponentChild = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Label isDisabled onClick={mockCallback}>
Click me
</Label>
);

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(
<Label isDisabled onClick={mockCallback}>
Click me
</Label>
);

const labelButton = screen.getByRole('button');

expect(labelButton).toHaveAttribute('disabled');
});

test('link label is an anchor', () => {
const href = '#example';

render(<Label href={href}>Click me</Label>);

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(
<Label isDisabled href={href}>
Click me
</Label>
);

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(
<Label isDisabled onClick={mockCallback} onClose={mockCallback}>
Click me
</Label>
);

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(
<Label isDisabled href="#" onClose={mockCallback}>
Click me
</Label>
);

const closeButton = screen.getByLabelText('Close Click me');

expect(closeButton).toBeDisabled();
});
});

0 comments on commit 615b1fa

Please sign in to comment.