|
| 1 | +import { fireEvent, render, screen } from '@testing-library/react'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { Popover } from './Popover'; |
| 4 | + |
| 5 | +vi.mock('react-dom', () => ({ |
| 6 | + createPortal: (node: React.ReactNode) => node, |
| 7 | +})); |
| 8 | + |
| 9 | +describe('Popover', () => { |
| 10 | + const onClose = vi.fn(); |
| 11 | + let anchorEl: HTMLElement; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + vi.clearAllMocks(); |
| 15 | + anchorEl = document.createElement('button'); |
| 16 | + anchorEl.setAttribute('data-testid', 'anchor'); |
| 17 | + document.body.appendChild(anchorEl); |
| 18 | + vi.spyOn(anchorEl, 'getBoundingClientRect').mockReturnValue({ |
| 19 | + x: 100, |
| 20 | + y: 100, |
| 21 | + top: 100, |
| 22 | + right: 200, |
| 23 | + bottom: 200, |
| 24 | + left: 100, |
| 25 | + width: 100, |
| 26 | + height: 100, |
| 27 | + toJSON: () => {}, |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('rendering', () => { |
| 32 | + it('renders nothing when isOpen is false', () => { |
| 33 | + render( |
| 34 | + <Popover isOpen={false} anchorEl={anchorEl} onClose={onClose}> |
| 35 | + <div>Content</div> |
| 36 | + </Popover>, |
| 37 | + ); |
| 38 | + expect(screen.queryByTestId('ockPopover')).not.toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('renders content when isOpen is true', () => { |
| 42 | + render( |
| 43 | + <Popover isOpen={true} anchorEl={anchorEl} onClose={onClose}> |
| 44 | + <div data-testid="content">Content</div> |
| 45 | + </Popover>, |
| 46 | + ); |
| 47 | + expect(screen.getByTestId('ockPopover')).toBeInTheDocument(); |
| 48 | + expect(screen.getByTestId('content')).toBeInTheDocument(); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('accessibility', () => { |
| 53 | + it('sets all ARIA attributes correctly', () => { |
| 54 | + render( |
| 55 | + <Popover |
| 56 | + isOpen={true} |
| 57 | + anchorEl={anchorEl} |
| 58 | + aria-label="Test Popover" |
| 59 | + aria-describedby="desc" |
| 60 | + aria-labelledby="title" |
| 61 | + onClose={onClose} |
| 62 | + > |
| 63 | + <div>Content</div> |
| 64 | + </Popover>, |
| 65 | + ); |
| 66 | + |
| 67 | + const popover = screen.getByTestId('ockPopover'); |
| 68 | + expect(popover).toHaveAttribute('role', 'dialog'); |
| 69 | + expect(popover).toHaveAttribute('aria-label', 'Test Popover'); |
| 70 | + expect(popover).toHaveAttribute('aria-describedby', 'desc'); |
| 71 | + expect(popover).toHaveAttribute('aria-labelledby', 'title'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('positioning', () => { |
| 76 | + const testCases = [ |
| 77 | + { |
| 78 | + position: 'top', |
| 79 | + align: 'start', |
| 80 | + expectedTop: -8, |
| 81 | + expectedLeft: 100, |
| 82 | + }, |
| 83 | + { |
| 84 | + position: 'bottom', |
| 85 | + align: 'center', |
| 86 | + expectedTop: 208, |
| 87 | + expectedLeft: 100, |
| 88 | + }, |
| 89 | + { |
| 90 | + position: 'left', |
| 91 | + align: 'end', |
| 92 | + expectedTop: 200, |
| 93 | + expectedLeft: -8, |
| 94 | + }, |
| 95 | + { |
| 96 | + position: 'right', |
| 97 | + align: 'center', |
| 98 | + expectedTop: 150, |
| 99 | + expectedLeft: 208, |
| 100 | + }, |
| 101 | + ] as const; |
| 102 | + |
| 103 | + for (const { position, align, expectedTop, expectedLeft } of testCases) { |
| 104 | + it(`positions correctly with position=${position} and align=${align}`, () => { |
| 105 | + render( |
| 106 | + <Popover |
| 107 | + isOpen={true} |
| 108 | + anchorEl={anchorEl} |
| 109 | + position={position} |
| 110 | + align={align} |
| 111 | + offset={8} |
| 112 | + onClose={onClose} |
| 113 | + > |
| 114 | + <div style={{ width: '100px', height: '100px' }}>Content</div> |
| 115 | + </Popover>, |
| 116 | + ); |
| 117 | + |
| 118 | + const popover = screen.getByTestId('ockPopover'); |
| 119 | + vi.spyOn(popover, 'getBoundingClientRect').mockReturnValue({ |
| 120 | + width: 100, |
| 121 | + height: 100, |
| 122 | + } as DOMRect); |
| 123 | + |
| 124 | + fireEvent(window, new Event('resize')); |
| 125 | + |
| 126 | + expect(popover.style.top).toBe(`${expectedTop}px`); |
| 127 | + expect(popover.style.left).toBe(`${expectedLeft}px`); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + it('updates position on scroll', () => { |
| 132 | + render( |
| 133 | + <Popover isOpen={true} anchorEl={anchorEl} onClose={onClose}> |
| 134 | + <div>Content</div> |
| 135 | + </Popover>, |
| 136 | + ); |
| 137 | + |
| 138 | + fireEvent.scroll(window); |
| 139 | + expect(anchorEl.getBoundingClientRect).toHaveBeenCalled(); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('dismissal behavior', () => { |
| 144 | + it('calls onClose when clicking outside', () => { |
| 145 | + render( |
| 146 | + <Popover isOpen={true} anchorEl={anchorEl} onClose={onClose}> |
| 147 | + <div>Content</div> |
| 148 | + </Popover>, |
| 149 | + ); |
| 150 | + |
| 151 | + fireEvent.pointerDown(document.body); |
| 152 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 153 | + }); |
| 154 | + |
| 155 | + it('calls onClose when pressing Escape', () => { |
| 156 | + render( |
| 157 | + <Popover isOpen={true} anchorEl={anchorEl} onClose={onClose}> |
| 158 | + <div>Content</div> |
| 159 | + </Popover>, |
| 160 | + ); |
| 161 | + |
| 162 | + fireEvent.keyDown(document, { key: 'Escape' }); |
| 163 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 164 | + }); |
| 165 | + |
| 166 | + it('handles undefined onClose prop gracefully', () => { |
| 167 | + render( |
| 168 | + <Popover isOpen={true} anchorEl={anchorEl}> |
| 169 | + <div>Content</div> |
| 170 | + </Popover>, |
| 171 | + ); |
| 172 | + |
| 173 | + fireEvent.pointerDown(document.body); |
| 174 | + fireEvent.keyDown(document, { key: 'Escape' }); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + describe('cleanup', () => { |
| 179 | + it('removes event listeners on unmount', () => { |
| 180 | + const { unmount } = render( |
| 181 | + <Popover isOpen={true} anchorEl={anchorEl} onClose={onClose}> |
| 182 | + <div>Content</div> |
| 183 | + </Popover>, |
| 184 | + ); |
| 185 | + |
| 186 | + const removeEventListenerSpy = vi.spyOn(window, 'removeEventListener'); |
| 187 | + unmount(); |
| 188 | + |
| 189 | + expect(removeEventListenerSpy).toHaveBeenCalledWith( |
| 190 | + 'resize', |
| 191 | + expect.any(Function), |
| 192 | + ); |
| 193 | + expect(removeEventListenerSpy).toHaveBeenCalledWith( |
| 194 | + 'scroll', |
| 195 | + expect.any(Function), |
| 196 | + ); |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + describe('portal rendering', () => { |
| 201 | + it('renders in portal', () => { |
| 202 | + const { baseElement } = render( |
| 203 | + <Popover isOpen={true} anchorEl={anchorEl} onClose={onClose}> |
| 204 | + <div>Content</div> |
| 205 | + </Popover>, |
| 206 | + ); |
| 207 | + |
| 208 | + expect(baseElement.contains(screen.getByTestId('ockPopover'))).toBe(true); |
| 209 | + }); |
| 210 | + }); |
| 211 | + |
| 212 | + describe('edge cases', () => { |
| 213 | + it('handles null anchorEl gracefully', () => { |
| 214 | + render( |
| 215 | + <Popover isOpen={true} anchorEl={null} onClose={onClose}> |
| 216 | + <div>Content</div> |
| 217 | + </Popover>, |
| 218 | + ); |
| 219 | + expect(screen.getByTestId('ockPopover')).toBeInTheDocument(); |
| 220 | + }); |
| 221 | + |
| 222 | + it('handles missing getBoundingClientRect gracefully', () => { |
| 223 | + const brokenAnchorEl = document.createElement('button'); |
| 224 | + vi.spyOn(brokenAnchorEl, 'getBoundingClientRect').mockReturnValue( |
| 225 | + null as unknown as DOMRect, |
| 226 | + ); |
| 227 | + |
| 228 | + render( |
| 229 | + <Popover isOpen={true} anchorEl={brokenAnchorEl} onClose={onClose}> |
| 230 | + <div>Content</div> |
| 231 | + </Popover>, |
| 232 | + ); |
| 233 | + expect(screen.getByTestId('ockPopover')).toBeInTheDocument(); |
| 234 | + }); |
| 235 | + }); |
| 236 | + |
| 237 | + describe('focus management', () => { |
| 238 | + it('traps focus when open', () => { |
| 239 | + render( |
| 240 | + <Popover isOpen={true} anchorEl={anchorEl} onClose={onClose}> |
| 241 | + <button type="button">First</button> |
| 242 | + <button type="button">Second</button> |
| 243 | + </Popover>, |
| 244 | + ); |
| 245 | + |
| 246 | + const firstButton = screen.getByText('First'); |
| 247 | + const secondButton = screen.getByText('Second'); |
| 248 | + |
| 249 | + firstButton.focus(); |
| 250 | + expect(document.activeElement).toBe(firstButton); |
| 251 | + |
| 252 | + fireEvent.keyDown(firstButton, { key: 'Tab' }); |
| 253 | + expect(document.activeElement).toBe(secondButton); |
| 254 | + }); |
| 255 | + }); |
| 256 | +}); |
0 commit comments