From 6d56815f9b7f369fce46701ff5cac3392a4f9d72 Mon Sep 17 00:00:00 2001 From: Matan Borenkraout Date: Sun, 12 Jul 2020 21:08:53 +0300 Subject: [PATCH] chore(flushMicroTasks): Use the mocked scheduler when available and mock the scheduler in our setup-env. --- src/flush-microtasks.js | 12 ++++++++---- tests/setup-env.js | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/flush-microtasks.js b/src/flush-microtasks.js index 3789bf80..1c3e4c65 100644 --- a/src/flush-microtasks.js +++ b/src/flush-microtasks.js @@ -55,13 +55,14 @@ try { 'if you encounter this warning.', ) } - } } -const isModernScheduleCallbackSupported = Scheduler && satisfies(React.version, '>16.8.6', { - includePrerelease: true, -}) +const isModernScheduleCallbackSupported = + Scheduler && + satisfies(React.version, '>16.8.6', { + includePrerelease: true, + }) function scheduleCallback(cb) { const NormalPriority = Scheduler @@ -86,6 +87,9 @@ export default function flushMicroTasks() { // reproduce the problem, so there's no test for it. But it works! jest.advanceTimersByTime(0) resolve() + } else if (Scheduler && Scheduler.unstable_flushAll) { + Scheduler.unstable_flushAll() + enqueueTask(resolve) } else { scheduleCallback(() => enqueueTask(resolve)) } diff --git a/tests/setup-env.js b/tests/setup-env.js index 264828a9..d8d12140 100644 --- a/tests/setup-env.js +++ b/tests/setup-env.js @@ -1 +1,2 @@ import '@testing-library/jest-dom/extend-expect' +jest.mock('scheduler', () => jest.requireActual('scheduler/unstable_mock'))