mirror of
https://github.com/zebrajr/react.git
synced 2025-12-07 12:20:38 +01:00
* Replace noop's fake Scheduler implementation with mock Scheduler build The noop renderer has its own mock implementation of the Scheduler interface, with the ability to partially render work in tests. Now that this functionality has been lifted into a proper mock Scheduler build, we can use that instead. Most of the existing noop tests were unaffected, but I did have to make some changes. The biggest one involved passive effects: previously, they were scheduled on a separate queue from the queue that handles rendering. After this change, both rendering and effects are scheduled in the Scheduler queue. I think this is a better approach because tests no longer have to worry about the difference; if you call `flushAll`, all the work is flushed, both rendering and effects. But for those few tests that do care to flush the rendering without the effects, that's still possible using the `yieldValue` API. Follow-up: Do the same for test renderer. * Fix import to scheduler/unstable_mock
101 lines
2.9 KiB
JavaScript
101 lines
2.9 KiB
JavaScript
'use strict';
|
|
|
|
const JestReact = require('jest-react');
|
|
const SchedulerMatchers = require('./schedulerTestMatchers');
|
|
|
|
function captureAssertion(fn) {
|
|
// Trick to use a Jest matcher inside another Jest matcher. `fn` contains an
|
|
// assertion; if it throws, we capture the error and return it, so the stack
|
|
// trace presented to the user points to the original assertion in the
|
|
// test file.
|
|
try {
|
|
fn();
|
|
} catch (error) {
|
|
return {
|
|
pass: false,
|
|
message: () => error.message,
|
|
};
|
|
}
|
|
return {pass: true};
|
|
}
|
|
|
|
function resolveScheduler(obj) {
|
|
if (obj._Scheduler !== undefined) {
|
|
return obj._Scheduler;
|
|
}
|
|
if (typeof obj.unstable_scheduleCallback === 'function') {
|
|
return obj;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function assertYieldsWereCleared(Scheduler) {
|
|
const actualYields = Scheduler.unstable_clearYields();
|
|
if (actualYields.length !== 0) {
|
|
throw new Error(
|
|
'Log of yielded values is not empty. ' +
|
|
'Call expect(ReactNoop).toHaveYielded(...) first.'
|
|
);
|
|
}
|
|
}
|
|
|
|
function toFlushAndYield(ReactNoop, expectedYields) {
|
|
const Scheduler = resolveScheduler(ReactNoop);
|
|
if (Scheduler === null) {
|
|
return JestReact.unstable_toFlushAndYield(ReactNoop, expectedYields);
|
|
}
|
|
return SchedulerMatchers.toFlushAndYield(Scheduler, expectedYields);
|
|
}
|
|
|
|
function toFlushAndYieldThrough(ReactNoop, expectedYields) {
|
|
const Scheduler = resolveScheduler(ReactNoop);
|
|
if (Scheduler === null) {
|
|
return JestReact.unstable_toFlushAndYieldThrough(ReactNoop, expectedYields);
|
|
}
|
|
return SchedulerMatchers.toFlushAndYieldThrough(Scheduler, expectedYields);
|
|
}
|
|
|
|
function toFlushWithoutYielding(ReactNoop) {
|
|
const Scheduler = resolveScheduler(ReactNoop);
|
|
if (Scheduler === null) {
|
|
return JestReact.unstable_toFlushWithoutYielding(ReactNoop);
|
|
}
|
|
return SchedulerMatchers.toFlushWithoutYielding(Scheduler);
|
|
}
|
|
|
|
function toHaveYielded(ReactNoop, expectedYields) {
|
|
const Scheduler = resolveScheduler(ReactNoop);
|
|
if (Scheduler === null) {
|
|
return JestReact.unstable_toHaveYielded(ReactNoop, expectedYields);
|
|
}
|
|
return SchedulerMatchers.toHaveYielded(Scheduler, expectedYields);
|
|
}
|
|
|
|
function toFlushAndThrow(ReactNoop, ...rest) {
|
|
const Scheduler = resolveScheduler(ReactNoop);
|
|
if (Scheduler === null) {
|
|
return JestReact.unstable_toFlushAndThrow(ReactNoop, ...rest);
|
|
}
|
|
return SchedulerMatchers.toFlushAndThrow(Scheduler, ...rest);
|
|
}
|
|
|
|
function toMatchRenderedOutput(ReactNoop, expectedJSX) {
|
|
if (typeof ReactNoop.getChildrenAsJSX === 'function') {
|
|
const Scheduler = ReactNoop._Scheduler;
|
|
assertYieldsWereCleared(Scheduler);
|
|
return captureAssertion(() => {
|
|
expect(ReactNoop.getChildrenAsJSX()).toEqual(expectedJSX);
|
|
});
|
|
}
|
|
return JestReact.unstable_toMatchRenderedOutput(ReactNoop, expectedJSX);
|
|
}
|
|
|
|
module.exports = {
|
|
toFlushAndYield,
|
|
toFlushAndYieldThrough,
|
|
toFlushWithoutYielding,
|
|
toHaveYielded,
|
|
toFlushAndThrow,
|
|
toMatchRenderedOutput,
|
|
};
|