-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: switch IS_REACT_ACT_ENVIRONMENT in userEvent (#1491)
* fix: use act in wait util * refactor: remove useless usage of act in press implem * refactor: make test fail by checking console.error is not called * refactor: extract async wrapper from waitFor implem * feat: use asyncWrapper for userEvent to prevent act warnings * refactor: add comment making test on act environment more explicit * refactor: move asyncWrapper to helper folder cause its not direclty tied to userevent * refactor: add comment in asyncWrapper * refactor: move UE `act` test to UE `press` tests * refactor: tweaks * refactor: naming tweaks * chore: revert unnecessary filename change * chore: exclude code cov for React <= 17 * chore: disable codecov for wrap-async --------- Co-authored-by: pierrezimmermann <pierrez@nam.tech> Co-authored-by: Maciej Jastrzebski <mdjastrzebski@gmail.com>
- Loading branch information
1 parent
3be9f3b
commit 328466d
Showing
6 changed files
with
138 additions
and
67 deletions.
There are no files selected for viewing
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
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,43 @@ | ||
/* istanbul ignore file */ | ||
|
||
import { act } from 'react-test-renderer'; | ||
import { getIsReactActEnvironment, setReactActEnvironment } from '../act'; | ||
import { flushMicroTasksLegacy } from '../flush-micro-tasks'; | ||
import { checkReactVersionAtLeast } from '../react-versions'; | ||
|
||
/** | ||
* Run given async callback with temporarily disabled `act` environment and flushes microtasks queue. | ||
* | ||
* @param callback Async callback to run | ||
* @returns Result of the callback | ||
*/ | ||
export async function wrapAsync<Result>( | ||
callback: () => Promise<Result> | ||
): Promise<Result> { | ||
if (checkReactVersionAtLeast(18, 0)) { | ||
const previousActEnvironment = getIsReactActEnvironment(); | ||
setReactActEnvironment(false); | ||
|
||
try { | ||
const result = await callback(); | ||
// Flush the microtask queue before restoring the `act` environment | ||
await flushMicroTasksLegacy(); | ||
return result; | ||
} finally { | ||
setReactActEnvironment(previousActEnvironment); | ||
} | ||
} | ||
|
||
if (!checkReactVersionAtLeast(16, 9)) { | ||
return callback(); | ||
} | ||
|
||
// Wrapping with act for react version 16.9 to 17.x | ||
let result: Result; | ||
await act(async () => { | ||
result = await callback(); | ||
}); | ||
|
||
// Either we have result or `callback` threw error | ||
return result!; | ||
} |
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
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
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
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