-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: (@radix-ui/react-tooltip) add unit tests for tooltip component (#…
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from 'react'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import * as Tooltip from '@radix-ui/react-tooltip'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
describe('Tooltip', () => { | ||
it('renders tooltip trigger', () => { | ||
render( | ||
<Tooltip.Provider> | ||
<Tooltip.Root> | ||
<Tooltip.Trigger>Tooltip Trigger</Tooltip.Trigger> | ||
<Tooltip.Portal> | ||
<Tooltip.Content> | ||
Tooltip Content | ||
<Tooltip.Arrow /> | ||
</Tooltip.Content> | ||
</Tooltip.Portal> | ||
</Tooltip.Root> | ||
</Tooltip.Provider> | ||
); | ||
|
||
expect(screen.getByText('Tooltip Trigger')).toBeInTheDocument(); | ||
expect(screen.queryByText('Tooltip Content')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('renders tooltip content when trigger is hovered', async () => { | ||
render( | ||
<Tooltip.Provider> | ||
<Tooltip.Root delayDuration={0}> | ||
<Tooltip.Trigger>Tooltip Trigger</Tooltip.Trigger> | ||
<Tooltip.Portal> | ||
<Tooltip.Content> | ||
Tooltip Content | ||
<Tooltip.Arrow /> | ||
</Tooltip.Content> | ||
</Tooltip.Portal> | ||
</Tooltip.Root> | ||
</Tooltip.Provider> | ||
); | ||
|
||
const trigger = screen.getByText('Tooltip Trigger'); | ||
expect(screen.queryByText('Tooltip Content')).not.toBeInTheDocument(); | ||
|
||
userEvent.hover(trigger); | ||
await waitFor(() => { | ||
// Get the first instance of the tooltip content because the second is | ||
// the visually hidden primitive. | ||
expect(screen.queryAllByText('Tooltip Content')[0]).toBeVisible(); | ||
}); | ||
}); | ||
|
||
it('renders tooltip content is dismissed when trigger is clicked', async () => { | ||
render( | ||
<Tooltip.Provider> | ||
<Tooltip.Root delayDuration={0}> | ||
<Tooltip.Trigger>Tooltip Trigger</Tooltip.Trigger> | ||
<Tooltip.Portal> | ||
<Tooltip.Content> | ||
Tooltip Content | ||
<Tooltip.Arrow /> | ||
</Tooltip.Content> | ||
</Tooltip.Portal> | ||
</Tooltip.Root> | ||
</Tooltip.Provider> | ||
); | ||
|
||
const trigger = screen.getByText('Tooltip Trigger'); | ||
expect(screen.queryByText('Tooltip Content')).not.toBeInTheDocument(); | ||
|
||
userEvent.hover(trigger); | ||
await waitFor(() => { | ||
// Get the first instance of the tooltip content because the second is | ||
// the visually hidden primitive. | ||
expect(screen.queryAllByText('Tooltip Content')[0]).toBeVisible(); | ||
}); | ||
|
||
userEvent.click(trigger); | ||
await waitFor(() => { | ||
expect(screen.queryByText('Tooltip Content')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |