Skip to content

Commit

Permalink
test: use waitFor only when relevant
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieljablonski committed Feb 18, 2024
1 parent 890a7a7 commit 2afd043
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 76 deletions.
24 changes: 6 additions & 18 deletions src/test/tooltip-attributes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ describe('tooltip attributes', () => {

await userEvent.hover(anchorElement)

let tooltip = null

await waitFor(() => {
tooltip = screen.getByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
})
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')

expect(anchorElement).toHaveAttribute('data-tooltip-content')
expect(tooltip).toBeInTheDocument()
Expand All @@ -63,12 +59,8 @@ describe('tooltip attributes', () => {

await userEvent.hover(anchorElement)

let tooltip = null

await waitFor(() => {
tooltip = screen.getByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
})
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')

expect(anchorElement).toHaveAttribute('data-tooltip-place')
expect(anchorElement).toHaveAttribute('data-tooltip-content')
Expand All @@ -88,12 +80,8 @@ describe('tooltip attributes', () => {

await userEvent.hover(anchorElement)

let tooltip = null

await waitFor(() => {
tooltip = screen.getByRole('tooltip')
expect(tooltip).toHaveClass('tooltip-class-name')
})
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveClass('tooltip-class-name')

expect(anchorElement).toHaveAttribute('data-tooltip-class-name')
expect(tooltip).toBeInTheDocument()
Expand Down
78 changes: 23 additions & 55 deletions src/test/tooltip-props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ describe('tooltip props', () => {

await userEvent.hover(anchorElement)

let tooltip = null

await waitFor(() => {
tooltip = screen.getByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
})
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')

expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
Expand All @@ -54,12 +50,8 @@ describe('tooltip props', () => {

await userEvent.hover(anchorElement)

let tooltip = null

await waitFor(() => {
tooltip = screen.getByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
})
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')

expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
Expand All @@ -71,12 +63,8 @@ describe('tooltip props', () => {

await userEvent.hover(anchorElement)

let tooltip = null

await waitFor(() => {
tooltip = screen.getByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
})
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')

expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
Expand All @@ -98,14 +86,10 @@ describe('tooltip props', () => {

await userEvent.hover(anchorElement)

let tooltip = null
let button = null
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')

await waitFor(() => {
tooltip = screen.getByRole('tooltip')
button = screen.getByRole('button')
expect(tooltip).toHaveAttribute('style')
})
const button = await screen.findByRole('button')

await userEvent.click(button)

Expand All @@ -123,22 +107,13 @@ describe('tooltip props', () => {

await userEvent.hover(anchorElement)

let tooltip = null

await waitFor(
() => {
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
},
{
timeout: 250,
},
)

await waitFor(() => {
tooltip = screen.getByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
})

const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')

expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
})
Expand All @@ -151,15 +126,16 @@ describe('tooltip props', () => {

await userEvent.hover(anchorElement)

await waitFor(() => {
expect(screen.queryByRole('tooltip')).toBeInTheDocument()
})
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')

expect(tooltip).toBeInTheDocument()

await userEvent.unhover(anchorElement)

await waitFor(
() => {
expect(screen.queryByRole('tooltip')).toBeInTheDocument()
expect(tooltip).toBeInTheDocument()
},
{
timeout: 200,
Expand All @@ -168,7 +144,7 @@ describe('tooltip props', () => {

await waitFor(
() => {
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
expect(tooltip).not.toBeInTheDocument()
},
{
timeout: 500,
Expand All @@ -186,12 +162,8 @@ describe('tooltip props', () => {

await userEvent.hover(anchorElement)

let tooltip = null

await waitFor(() => {
tooltip = screen.getByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
})
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')

expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
Expand All @@ -203,12 +175,8 @@ describe('tooltip props', () => {

await userEvent.hover(anchorElement)

let tooltip = null

await waitFor(() => {
tooltip = screen.getByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
})
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')

expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
Expand Down
3 changes: 0 additions & 3 deletions src/test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { debounce, computeTooltipPosition, cssTimeToMs } from 'utils'

// Tell Jest to mock all timeout functions
jest.useRealTimers()

describe('compute positions', () => {
test('empty reference elements', async () => {
const value = await computeTooltipPosition({
Expand Down

0 comments on commit 2afd043

Please sign in to comment.