-
Notifications
You must be signed in to change notification settings - Fork 48.4k
Reimplement act
without mock Scheduler
#21714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,32 +354,6 @@ function runActTests(label, render, unmount, rerender) { | |
expect(container.innerHTML).toBe('2'); | ||
}); | ||
}); | ||
|
||
// @gate __DEV__ | ||
it('warns if you return a value inside act', () => { | ||
expect(() => act(() => null)).toErrorDev( | ||
[ | ||
'The callback passed to act(...) function must return undefined, or a Promise.', | ||
], | ||
{withoutStack: true}, | ||
); | ||
expect(() => act(() => 123)).toErrorDev( | ||
[ | ||
'The callback passed to act(...) function must return undefined, or a Promise.', | ||
], | ||
{withoutStack: true}, | ||
); | ||
}); | ||
|
||
// @gate __DEV__ | ||
it('warns if you try to await a sync .act call', () => { | ||
expect(() => act(() => {}).then(() => {})).toErrorDev( | ||
[ | ||
'Do not await the result of calling act(...) with sync logic, it is not a Promise.', | ||
], | ||
{withoutStack: true}, | ||
); | ||
}); | ||
}); | ||
|
||
describe('asynchronous tests', () => { | ||
|
@@ -401,15 +375,17 @@ function runActTests(label, render, unmount, rerender) { | |
|
||
await act(async () => { | ||
render(<App />, container); | ||
// flush a little to start the timer | ||
expect(Scheduler).toFlushAndYield([]); | ||
}); | ||
expect(container.innerHTML).toBe('0'); | ||
// Flush the pending timers | ||
await act(async () => { | ||
await sleep(100); | ||
}); | ||
expect(container.innerHTML).toBe('1'); | ||
}); | ||
|
||
// @gate __DEV__ | ||
it('flushes microtasks before exiting', async () => { | ||
it('flushes microtasks before exiting (async function)', async () => { | ||
function App() { | ||
const [ctr, setCtr] = React.useState(0); | ||
async function someAsyncFunction() { | ||
|
@@ -431,6 +407,31 @@ function runActTests(label, render, unmount, rerender) { | |
expect(container.innerHTML).toEqual('1'); | ||
}); | ||
|
||
// @gate __DEV__ | ||
it('flushes microtasks before exiting (sync function)', async () => { | ||
// Same as previous test, but the callback passed to `act` is not itself | ||
// an async function. | ||
function App() { | ||
const [ctr, setCtr] = React.useState(0); | ||
async function someAsyncFunction() { | ||
// queue a bunch of promises to be sure they all flush | ||
await null; | ||
await null; | ||
await null; | ||
setCtr(1); | ||
} | ||
React.useEffect(() => { | ||
someAsyncFunction(); | ||
}, []); | ||
return ctr; | ||
} | ||
|
||
await act(() => { | ||
render(<App />, container); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually a new feature. There was no inherent reason this wasn't already supported. This is nice because usually an |
||
}); | ||
expect(container.innerHTML).toEqual('1'); | ||
}); | ||
|
||
// @gate __DEV__ | ||
it('warns if you do not await an act call', async () => { | ||
spyOnDevAndProd(console, 'error'); | ||
|
@@ -461,7 +462,13 @@ function runActTests(label, render, unmount, rerender) { | |
|
||
await sleep(150); | ||
if (__DEV__) { | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
expect(console.error).toHaveBeenCalledTimes(2); | ||
expect(console.error.calls.argsFor(0)[0]).toMatch( | ||
'You seem to have overlapping act() calls', | ||
); | ||
expect(console.error.calls.argsFor(1)[0]).toMatch( | ||
'You seem to have overlapping act() calls', | ||
); | ||
} | ||
}); | ||
|
||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rewrote to not use a mock scheduler API