Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Prevent tooltip from rendering with empty label #1834

Merged
merged 1 commit into from
Jan 13, 2025
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
5 changes: 5 additions & 0 deletions .changeset/three-eels-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@strapi/design-system': patch
---

Prevent tooltip from rendering when the label property is not passed
46 changes: 46 additions & 0 deletions packages/design-system/src/components/Tooltip/Tooltip.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { render as renderHarness } from '@test/utils';

import { Button } from '../Button';

import { Tooltip, type TooltipProps } from './Tooltip';

const render = (props: Partial<TooltipProps> = {}) =>
renderHarness(
<Tooltip {...props}>
<Button>My button</Button>
</Tooltip>,
);

describe('Tooltip', () => {
it('should render and be accessible with a label', async () => {
const { user, getByRole, findByRole } = render({
label: 'My tooltip',
});

await user.hover(getByRole('button'));

expect(await findByRole('tooltip', { name: 'My tooltip' })).toBeInTheDocument();
});

it('should render the label and not the description if both are provided', async () => {
const { user, getByRole, findByRole, queryByRole } = render({
label: 'My tooltip label',
description: 'My tooltip description',
});

await user.hover(getByRole('button'));

expect(await findByRole('tooltip', { name: 'My tooltip label' })).toBeInTheDocument();
expect(queryByRole('tooltip', { name: 'My tooltip description' })).not.toBeInTheDocument();
});

it('should not render when the label is empty', async () => {
const { user, getByRole, queryByRole } = render({
label: '',
});

await user.hover(getByRole('button'));

expect(queryByRole('tooltip', { name: 'My tooltip' })).not.toBeInTheDocument();
});
});
2 changes: 2 additions & 0 deletions packages/design-system/src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const TooltipImpl = React.forwardRef<TooltipElement, TooltipProps>(
},
forwardedRef,
) => {
if (!label && !description) return children;

return (
<Tooltip.Root
defaultOpen={defaultOpen}
Expand Down
Loading