Skip to content

Commit

Permalink
fix(tooltip): solve failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
alaa-yahia committed Mar 19, 2024
1 parent 72461f2 commit eeafeef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions components/tooltip/src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const Tooltip = ({
modifiers={[offsetModifier, flipModifier, hideModifier]}
>
<div
className={className}
id="tooltipContenDhis2Ui"
onMouseOver={onMouseOver}
onMouseOut={onMouseOut}
Expand Down
28 changes: 21 additions & 7 deletions components/tooltip/src/tooltip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,20 @@ describe('Keyboard interaction', () => {

expect(document.querySelector(tooltipContentSelector)).toBe(null)

// wait for 'open delay' to elapse to open tooltip
act(() => {
jest.advanceTimersByTime(200)
})

// expect tooltip to be open after delay
const res = document.querySelector(tooltipContentSelector)
expect(res).not.toBe(null)
expect(res.textContent).toBe(tooltipContent)

wrapper.simulate('blur')
act(() => {
jest.advanceTimersByTime(200) // Assuming default closeDelay is 200ms
jest.advanceTimersByTime(200)
})
expect(document.querySelector(tooltipContentSelector)).toBe(null)
// this last part clears a warning about "code should be wrapped in `act(...)`"
// and clears the tooltip

act(() => {
jest.runAllTimers()
})
Expand All @@ -187,10 +184,27 @@ describe('Keyboard interaction', () => {

// open tooltip
wrapper.simulate('mouseover')

act(() => {
jest.advanceTimersByTime(200) // open the tooltip
jest.advanceTimersByTime(200)
})
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))

// verify tooltip is open
expect(document.querySelector(tooltipContentSelector)).not.toBe(null)

//Press the Escape key
act(() => {
document.dispatchEvent(
new KeyboardEvent('keydown', { key: 'Escape' })
)
})

// wait for close delay
act(() => {
jest.advanceTimersByTime(200)
})

// expect tooltip to be closed
expect(document.querySelector(tooltipContentSelector)).toBe(null)
})
})

0 comments on commit eeafeef

Please sign in to comment.