|
1 | 1 | import * as React from 'react'
|
2 | 2 | import * as ReactDOM from 'react-dom'
|
3 |
| -import {render, fireEvent} from '../' |
| 3 | +import {render, createEvent, fireEvent} from '../' |
4 | 4 |
|
5 | 5 | const eventTypes = [
|
6 | 6 | {
|
@@ -256,49 +256,55 @@ test('blur/focus bubbles in react', () => {
|
256 | 256 | expect(handleBubbledFocus).toHaveBeenCalledTimes(1)
|
257 | 257 | })
|
258 | 258 |
|
259 |
| -test('discrete events are not wrapped in act', () => { |
260 |
| - function AddDocumentClickListener({onClick}) { |
261 |
| - React.useEffect(() => { |
262 |
| - document.addEventListener('click', onClick) |
263 |
| - return () => { |
264 |
| - document.removeEventListener('click', onClick) |
265 |
| - } |
266 |
| - }, [onClick]) |
267 |
| - return null |
268 |
| - } |
269 |
| - function Component({onDocumentClick}) { |
270 |
| - const [open, setOpen] = React.useState(false) |
| 259 | +test.each([ |
| 260 | + ['fireEvent.click', element => fireEvent.click(element)], |
| 261 | + ['fireEvent()', element => fireEvent(element, createEvent.click(element))], |
| 262 | +])( |
| 263 | + 'discrete events are not wrapped in act when using %s', |
| 264 | + (_, dispatchClick) => { |
| 265 | + function AddDocumentClickListener({onClick}) { |
| 266 | + React.useEffect(() => { |
| 267 | + document.addEventListener('click', onClick) |
| 268 | + return () => { |
| 269 | + document.removeEventListener('click', onClick) |
| 270 | + } |
| 271 | + }, [onClick]) |
| 272 | + return null |
| 273 | + } |
| 274 | + function Component({onDocumentClick}) { |
| 275 | + const [open, setOpen] = React.useState(false) |
271 | 276 |
|
272 |
| - return ( |
273 |
| - <React.Fragment> |
274 |
| - <button onClick={() => setOpen(true)} /> |
275 |
| - {open && |
276 |
| - ReactDOM.createPortal( |
277 |
| - <AddDocumentClickListener onClick={onDocumentClick} />, |
278 |
| - document.body, |
279 |
| - )} |
280 |
| - </React.Fragment> |
281 |
| - ) |
282 |
| - } |
283 |
| - const onDocumentClick = jest.fn() |
284 |
| - render(<Component onDocumentClick={onDocumentClick} />) |
| 277 | + return ( |
| 278 | + <React.Fragment> |
| 279 | + <button onClick={() => setOpen(true)} /> |
| 280 | + {open && |
| 281 | + ReactDOM.createPortal( |
| 282 | + <AddDocumentClickListener onClick={onDocumentClick} />, |
| 283 | + document.body, |
| 284 | + )} |
| 285 | + </React.Fragment> |
| 286 | + ) |
| 287 | + } |
| 288 | + const onDocumentClick = jest.fn() |
| 289 | + render(<Component onDocumentClick={onDocumentClick} />) |
285 | 290 |
|
286 |
| - const button = document.querySelector('button') |
287 |
| - fireEvent.click(button) |
| 291 | + const button = document.querySelector('button') |
| 292 | + dispatchClick(button) |
288 | 293 |
|
289 |
| - // We added a native click listener from an effect. |
290 |
| - // There are two possible scenarios: |
291 |
| - // 1. If that effect is flushed during the click the native click listener would still receive the event that caused the native listener to be added. |
292 |
| - // 2. If that effect is flushed before we return from fireEvent.click the native click listener would not receive the event that caused the native listener to be added. |
293 |
| - // React flushes effects scheduled from an update by a "discrete" event immediately. |
294 |
| - // but not effects in a batched context (e.g. act(() => {})) |
295 |
| - // So if we were in act(() => {}), we would see scenario 2 i.e. `onDocumentClick` would not be called |
296 |
| - // If we were not in `act(() => {})`, we would see scenario 1 i.e. `onDocumentClick` would already be called |
297 |
| - expect(onDocumentClick).toHaveBeenCalledTimes(1) |
| 294 | + // We added a native click listener from an effect. |
| 295 | + // There are two possible scenarios: |
| 296 | + // 1. If that effect is flushed during the click the native click listener would still receive the event that caused the native listener to be added. |
| 297 | + // 2. If that effect is flushed before we return from fireEvent.click the native click listener would not receive the event that caused the native listener to be added. |
| 298 | + // React flushes effects scheduled from an update by a "discrete" event immediately. |
| 299 | + // but not effects in a batched context (e.g. act(() => {})) |
| 300 | + // So if we were in act(() => {}), we would see scenario 2 i.e. `onDocumentClick` would not be called |
| 301 | + // If we were not in `act(() => {})`, we would see scenario 1 i.e. `onDocumentClick` would already be called |
| 302 | + expect(onDocumentClick).toHaveBeenCalledTimes(1) |
298 | 303 |
|
299 |
| - // verify we did actually flush the effect before we returned from `fireEvent.click` i.e. the native click listener is mounted. |
300 |
| - document.dispatchEvent( |
301 |
| - new MouseEvent('click', {bubbles: true, cancelable: true}), |
302 |
| - ) |
303 |
| - expect(onDocumentClick).toHaveBeenCalledTimes(2) |
304 |
| -}) |
| 304 | + // verify we did actually flush the effect before we returned from `fireEvent.click` i.e. the native click listener is mounted. |
| 305 | + document.dispatchEvent( |
| 306 | + new MouseEvent('click', {bubbles: true, cancelable: true}), |
| 307 | + ) |
| 308 | + expect(onDocumentClick).toHaveBeenCalledTimes(2) |
| 309 | + }, |
| 310 | +) |
0 commit comments