Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/__tests__/event-wrapper-nesting.js

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write a test showing the underlying issue instead of testing implementation details (nesting depth)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry about that, I was stumped on it for a little bit
I got there today though, just needed to sleep on it. done.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as React from 'react'
import {render, screen, fireEvent} from '../'

let mockActDepth = 0
let mockMaxActDepth = 0
jest.mock('react', () => {
const actual = jest.requireActual('react')
return {
...actual,
act: jest.fn(cb => {
mockActDepth++
mockMaxActDepth = Math.max(mockMaxActDepth, mockActDepth)
try {
return actual.act(cb)
} finally {
mockActDepth--
}
}),
}
})

function Nested() {
return (
<>
<input
aria-label="outer"
onFocus={() =>
fireEvent.change(screen.getByLabelText('inner'), {
target: {value: 'changed'},
})
}
/>
<input aria-label="inner" />
</>
)
}

test('eventWrapper does not nest `act` for a re-entrant event dispatch', () => {
render(<Nested />)
expect(screen.getByLabelText('inner').value).toBe('')

mockMaxActDepth = 0
fireEvent.focus(screen.getByLabelText('outer'))

expect(screen.getByLabelText('inner').value).toBe('changed')
expect(mockMaxActDepth).toBe(1)
})
20 changes: 15 additions & 5 deletions src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ function jestFakeTimersAreEnabled() {
return false
}

let inEventWrapper = false

configureDTL({
unstable_advanceTimersWrapper: cb => {
return act(cb)
Expand Down Expand Up @@ -58,11 +60,19 @@ configureDTL({
}
},
eventWrapper: cb => {
let result
act(() => {
result = cb()
})
return result
if (inEventWrapper) {
return cb()
}
inEventWrapper = true
try {
let result
act(() => {
result = cb()
})
return result
} finally {
inEventWrapper = false
}
},
})

Expand Down
Loading