Created
June 9, 2021 20:18
-
-
Save jaketrent/76962889738dd9221e9788f964a5570f to your computer and use it in GitHub Desktop.
Jest test dynamic React children
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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