Skip to content

Commit

Permalink
add tests to verify that actions in Portal components won't close t…
Browse files Browse the repository at this point in the history
…he `Popover`
  • Loading branch information
RobinMalfait committed May 12, 2023
1 parent 04e8482 commit 8d1fda3
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions packages/@headlessui-react/src/components/popover/popover.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2623,6 +2623,80 @@ describe('Mouse interactions', () => {
assertActiveElement(getPopoverButton())
})
)

it(
'should not close the Popover if the focus is moved outside of the Popover but still in the same React tree using Portals',
suppressConsoleLogs(async () => {
let clickFn = jest.fn()
render(
<Popover>
<Popover.Button>Toggle</Popover.Button>
<Popover.Panel>
<Portal>
<button onClick={clickFn}>foo</button>
</Portal>
</Popover.Panel>
</Popover>
)

// Open the popover
await click(getPopoverButton())

// Verify it is open
assertPopoverPanel({ state: PopoverState.Visible })

// Click the button outside the Popover (DOM) but inside (Portal / React tree)
await click(getByText('foo'))

// Verify it is still open
assertPopoverPanel({ state: PopoverState.Visible })

// Verify the button was clicked
expect(clickFn).toHaveBeenCalled()
})
)

it(
'should not close the Popover if the focus is moved outside of the Popover but still in the same React tree using nested Portals',
suppressConsoleLogs(async () => {
let clickFn = jest.fn()
render(
<Popover>
<Popover.Button>Toggle</Popover.Button>
<Popover.Panel>
Level 0
<Portal>
Level 1
<Portal>
Level 2
<Portal>
Level 3
<Portal>
Level 4<button onClick={clickFn}>foo</button>
</Portal>
</Portal>
</Portal>
</Portal>
</Popover.Panel>
</Popover>
)

// Open the popover
await click(getPopoverButton())

// Verify it is open
assertPopoverPanel({ state: PopoverState.Visible })

// Click the button outside the Popover (DOM) but inside (Portal / React tree)
await click(getByText('foo'))

// Verify it is still open
assertPopoverPanel({ state: PopoverState.Visible })

// Verify the button was clicked
expect(clickFn).toHaveBeenCalled()
})
)
})

describe('Nested popovers', () => {
Expand Down

0 comments on commit 8d1fda3

Please sign in to comment.