-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test with various wait/act use cases (#7728)
### Summary & Motivation For use as a reference and playground for testing act/waitFor behavior with and without our provider components. ### How I Tested These Changes `yarn jest waitFor`
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
js_modules/dagit/packages/core/src/workspace/waitFor.test.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import {act, render, screen, waitFor} from '@testing-library/react'; | ||
import * as React from 'react'; | ||
|
||
import {TestProvider} from '../testing/TestProvider'; | ||
|
||
describe('waitFor', () => { | ||
describe('With TestProvider', () => { | ||
const Test = () => ( | ||
<TestProvider> | ||
<div>Hello</div> | ||
</TestProvider> | ||
); | ||
|
||
it('does require `act` if not wrapped in `waitFor`', async () => { | ||
await act(async () => { | ||
render(<Test />); | ||
}); | ||
expect(screen.getByText(/hello/i)).toBeVisible(); | ||
}); | ||
|
||
it('does not require `act` if wrapped in `waitFor`', async () => { | ||
render(<Test />); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(/hello/i)).toBeVisible(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Without TestProvider', () => { | ||
it('does not require `act` if your rendered stuff is already there', () => { | ||
const Test = () => <div>Hello</div>; | ||
render(<Test />); | ||
expect(screen.getByText(/hello/i)).toBeVisible(); | ||
}); | ||
|
||
describe('Rendering requires driving forward', () => { | ||
const Test = () => { | ||
const [value, setValue] = React.useState(0); | ||
React.useEffect(() => { | ||
const timer = setTimeout(() => { | ||
setValue((current) => current + 1); | ||
}, 0); | ||
return () => { | ||
clearTimeout(timer); | ||
}; | ||
}, []); | ||
return <div>Hello {value}</div>; | ||
}; | ||
|
||
it('does require `act` if you need to drive rendering forward', async () => { | ||
const {rerender} = render(<Test />); | ||
expect(screen.getByText(/hello 0/i)).toBeVisible(); | ||
|
||
await act(async () => rerender(<Test />)); | ||
expect(screen.getByText(/hello 1/i)).toBeVisible(); | ||
}); | ||
|
||
it('or it requires a `wait` utility call if you need to drive rendering forward', async () => { | ||
render(<Test />); | ||
expect(screen.getByText(/hello 0/i)).toBeVisible(); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(/hello 1/i)).toBeVisible(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |