Skip to content
New issue

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

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

Already on GitHub? # to your account

test(Label): add tests for disabled labels #10254

Merged
merged 1 commit into from
Apr 19, 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
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();
});
});
Loading