Skip to content

Instantly share code, notes, and snippets.

@jaketrent
Created June 9, 2021 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaketrent/76962889738dd9221e9788f964a5570f to your computer and use it in GitHub Desktop.
Save jaketrent/76962889738dd9221e9788f964a5570f to your computer and use it in GitHub Desktop.
Jest test dynamic React children
describe('dynamic content', () => {
beforeAll(() => {
jest.useFakeTimers()
})
afterAll(() => {
jest.useRealTimers()
})
it('allows tabbing to dynamic child', async () => {
const DynamicChildren = () => {
const [showButton, setShowButton] = React.useState(false)
React.useEffect(() => {
setTimeout(() => {
setShowButton(true)
}, 1000)
}, [])
return (
<FocusManager trapped={true} autofocus={true}>
<Button>Is tabbable</Button>
{showButton && <Button>Should be tabbable</Button>}
</FocusManager>
)
}
render(<DynamicChildren />)
act(() => jest.advanceTimersByTime(2000))
userEvent.tab()
expect(document.activeElement).toHaveTextContent('Should be tabbable')
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment