|
1 | 1 | import * as React from 'react'; |
2 | 2 | import { Popover } from './Popover'; |
3 | | -import { renderHook } from '@testing-library/react-hooks'; |
| 3 | +import { renderHook, act } from '@testing-library/react-hooks'; |
4 | 4 | import { usePopover_unstable } from './usePopover'; |
5 | 5 | import { isConformant } from '../../testing/isConformant'; |
| 6 | +import type { PopoverProps } from './Popover.types'; |
6 | 7 |
|
7 | 8 | describe('Popover', () => { |
8 | 9 | isConformant({ |
@@ -37,4 +38,182 @@ describe('Popover', () => { |
37 | 38 | // Assert |
38 | 39 | expect(result.current.withArrow).toBe(false); |
39 | 40 | }); |
| 41 | + |
| 42 | + describe('close on focus outside', () => { |
| 43 | + it('should close when trapFocus is enabled and focus moves outside', () => { |
| 44 | + const onOpenChange = jest.fn(); |
| 45 | + const outsideButton = document.createElement('button'); |
| 46 | + const popoverContent = document.createElement('div'); |
| 47 | + document.body.appendChild(outsideButton); |
| 48 | + document.body.appendChild(popoverContent); |
| 49 | + |
| 50 | + const { result } = renderHook( |
| 51 | + ({ open }) => |
| 52 | + usePopover_unstable({ |
| 53 | + open, |
| 54 | + trapFocus: true, |
| 55 | + onOpenChange, |
| 56 | + children: <div />, |
| 57 | + }), |
| 58 | + { initialProps: { open: true } }, |
| 59 | + ); |
| 60 | + |
| 61 | + // Set the contentRef to simulate mounted popover content |
| 62 | + act(() => { |
| 63 | + (result.current.contentRef as React.RefObject<HTMLElement | null>).current = popoverContent; |
| 64 | + }); |
| 65 | + |
| 66 | + act(() => { |
| 67 | + outsideButton.dispatchEvent(new FocusEvent('focusin', { bubbles: true })); |
| 68 | + }); |
| 69 | + |
| 70 | + expect(onOpenChange).toHaveBeenCalledWith(expect.anything(), { open: false }); |
| 71 | + |
| 72 | + document.body.removeChild(outsideButton); |
| 73 | + document.body.removeChild(popoverContent); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should not close when trapFocus is not enabled and focus moves outside', () => { |
| 77 | + const onOpenChange = jest.fn(); |
| 78 | + const outsideButton = document.createElement('button'); |
| 79 | + document.body.appendChild(outsideButton); |
| 80 | + |
| 81 | + renderHook(() => |
| 82 | + usePopover_unstable({ |
| 83 | + open: true, |
| 84 | + trapFocus: false, |
| 85 | + onOpenChange, |
| 86 | + children: <div />, |
| 87 | + }), |
| 88 | + ); |
| 89 | + |
| 90 | + act(() => { |
| 91 | + outsideButton.dispatchEvent(new FocusEvent('focusin', { bubbles: true })); |
| 92 | + }); |
| 93 | + |
| 94 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 95 | + |
| 96 | + document.body.removeChild(outsideButton); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should not close when popover is not open', () => { |
| 100 | + const onOpenChange = jest.fn(); |
| 101 | + const outsideButton = document.createElement('button'); |
| 102 | + document.body.appendChild(outsideButton); |
| 103 | + |
| 104 | + renderHook(() => |
| 105 | + usePopover_unstable({ |
| 106 | + open: false, |
| 107 | + trapFocus: true, |
| 108 | + onOpenChange, |
| 109 | + children: <div />, |
| 110 | + }), |
| 111 | + ); |
| 112 | + |
| 113 | + act(() => { |
| 114 | + outsideButton.dispatchEvent(new FocusEvent('focusin', { bubbles: true })); |
| 115 | + }); |
| 116 | + |
| 117 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 118 | + |
| 119 | + document.body.removeChild(outsideButton); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should also close when inertTrapFocus is enabled and focus moves to a page element outside', () => { |
| 123 | + const onOpenChange = jest.fn(); |
| 124 | + const outsideButton = document.createElement('button'); |
| 125 | + const popoverContent = document.createElement('div'); |
| 126 | + document.body.appendChild(outsideButton); |
| 127 | + document.body.appendChild(popoverContent); |
| 128 | + |
| 129 | + const { result } = renderHook(() => |
| 130 | + usePopover_unstable({ |
| 131 | + open: true, |
| 132 | + trapFocus: true, |
| 133 | + inertTrapFocus: true, |
| 134 | + onOpenChange, |
| 135 | + children: <div />, |
| 136 | + }), |
| 137 | + ); |
| 138 | + |
| 139 | + act(() => { |
| 140 | + (result.current.contentRef as React.RefObject<HTMLElement | null>).current = popoverContent; |
| 141 | + }); |
| 142 | + |
| 143 | + act(() => { |
| 144 | + outsideButton.dispatchEvent(new FocusEvent('focusin', { bubbles: true })); |
| 145 | + }); |
| 146 | + |
| 147 | + expect(onOpenChange).toHaveBeenCalledWith(expect.anything(), { open: false }); |
| 148 | + |
| 149 | + document.body.removeChild(outsideButton); |
| 150 | + document.body.removeChild(popoverContent); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should not close when closeOnFocusOutside is false', () => { |
| 154 | + const onOpenChange = jest.fn(); |
| 155 | + const outsideButton = document.createElement('button'); |
| 156 | + const popoverContent = document.createElement('div'); |
| 157 | + document.body.appendChild(outsideButton); |
| 158 | + document.body.appendChild(popoverContent); |
| 159 | + |
| 160 | + const { result } = renderHook( |
| 161 | + ({ open }) => |
| 162 | + usePopover_unstable({ |
| 163 | + open, |
| 164 | + trapFocus: true, |
| 165 | + closeOnFocusOutside: false, |
| 166 | + onOpenChange, |
| 167 | + children: <div />, |
| 168 | + } as unknown as PopoverProps), |
| 169 | + { initialProps: { open: true } }, |
| 170 | + ); |
| 171 | + |
| 172 | + act(() => { |
| 173 | + (result.current.contentRef as React.RefObject<HTMLElement | null>).current = popoverContent; |
| 174 | + }); |
| 175 | + |
| 176 | + act(() => { |
| 177 | + outsideButton.dispatchEvent(new FocusEvent('focusin', { bubbles: true })); |
| 178 | + }); |
| 179 | + |
| 180 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 181 | + |
| 182 | + document.body.removeChild(outsideButton); |
| 183 | + document.body.removeChild(popoverContent); |
| 184 | + }); |
| 185 | + |
| 186 | + it('should not close when focus moves to the trigger element', () => { |
| 187 | + const onOpenChange = jest.fn(); |
| 188 | + const triggerButton = document.createElement('button'); |
| 189 | + const popoverContent = document.createElement('div'); |
| 190 | + document.body.appendChild(triggerButton); |
| 191 | + document.body.appendChild(popoverContent); |
| 192 | + |
| 193 | + const { result } = renderHook( |
| 194 | + ({ open }) => |
| 195 | + usePopover_unstable({ |
| 196 | + open, |
| 197 | + trapFocus: true, |
| 198 | + onOpenChange, |
| 199 | + children: <div />, |
| 200 | + }), |
| 201 | + { initialProps: { open: true } }, |
| 202 | + ); |
| 203 | + |
| 204 | + act(() => { |
| 205 | + (result.current.contentRef as React.RefObject<HTMLElement | null>).current = popoverContent; |
| 206 | + (result.current.triggerRef as React.RefObject<HTMLElement | null>).current = triggerButton; |
| 207 | + }); |
| 208 | + |
| 209 | + act(() => { |
| 210 | + triggerButton.dispatchEvent(new FocusEvent('focusin', { bubbles: true })); |
| 211 | + }); |
| 212 | + |
| 213 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 214 | + |
| 215 | + document.body.removeChild(triggerButton); |
| 216 | + document.body.removeChild(popoverContent); |
| 217 | + }); |
| 218 | + }); |
40 | 219 | }); |
0 commit comments