Skip to content

Commit

Permalink
Use the disabled log to avoid double yielding values in scheduler mock
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Apr 8, 2020
1 parent 057e4a0 commit 2d742e5
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 56 deletions.
6 changes: 2 additions & 4 deletions packages/react-art/src/__tests__/ReactART-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ describe('ReactART', () => {
</CurrentRendererContext.Provider>,
);

expect(Scheduler).toFlushAndYieldThrough(__DEV__ ? ['A', 'A'] : ['A']);
expect(Scheduler).toFlushAndYieldThrough(['A']);

ReactDOM.render(
<Surface>
Expand All @@ -406,9 +406,7 @@ describe('ReactART', () => {
expect(ops).toEqual([null, 'ART']);

ops = [];
expect(Scheduler).toFlushAndYield(
__DEV__ ? ['B', 'B', 'C', 'C'] : ['B', 'C'],
);
expect(Scheduler).toFlushAndYield(['B', 'C']);

expect(ops).toEqual(['Test']);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,9 +639,7 @@ describe('ReactDOMFiberAsync', () => {
expect(container.textContent).toEqual('');

// Everything should render immediately in the next event
expect(Scheduler).toFlushExpired(
__DEV__ ? ['A', 'A', 'B', 'B', 'C', 'C'] : ['A', 'B', 'C'],
);
expect(Scheduler).toFlushExpired(['A', 'B', 'C']);
expect(container.textContent).toEqual('ABC');
});
});
Expand Down
28 changes: 3 additions & 25 deletions packages/react-dom/src/__tests__/ReactUpdates-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1324,30 +1324,12 @@ describe('ReactUpdates', () => {
let hiddenDiv;
act(() => {
root.render(<Foo />);
if (__DEV__) {
expect(Scheduler).toFlushAndYieldThrough([
'Foo',
'Foo',
'Baz',
'Baz',
'Foo#effect',
]);
} else {
expect(Scheduler).toFlushAndYieldThrough([
'Foo',
'Baz',
'Foo#effect',
]);
}
expect(Scheduler).toFlushAndYieldThrough(['Foo', 'Baz', 'Foo#effect']);
hiddenDiv = container.firstChild.firstChild;
expect(hiddenDiv.hidden).toBe(true);
expect(hiddenDiv.innerHTML).toBe('');
// Run offscreen update
if (__DEV__) {
expect(Scheduler).toFlushAndYield(['Bar', 'Bar']);
} else {
expect(Scheduler).toFlushAndYield(['Bar']);
}
expect(Scheduler).toFlushAndYield(['Bar']);
expect(hiddenDiv.hidden).toBe(true);
expect(hiddenDiv.innerHTML).toBe('<p>bar 0</p>');
});
Expand All @@ -1359,11 +1341,7 @@ describe('ReactUpdates', () => {
expect(hiddenDiv.innerHTML).toBe('<p>bar 0</p>');

// Run offscreen update
if (__DEV__) {
expect(Scheduler).toFlushAndYield(['Bar', 'Bar']);
} else {
expect(Scheduler).toFlushAndYield(['Bar']);
}
expect(Scheduler).toFlushAndYield(['Bar']);
expect(hiddenDiv.innerHTML).toBe('<p>bar 1</p>');
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1701,12 +1701,7 @@ describe('DOMModernPluginEventSystem', () => {
const root = ReactDOM.createRoot(container);
root.render(<Test counter={0} />);

// Dev double-render
if (__DEV__) {
expect(Scheduler).toFlushAndYield(['Test', 'Test']);
} else {
expect(Scheduler).toFlushAndYield(['Test']);
}
expect(Scheduler).toFlushAndYield(['Test']);

// Click the button
dispatchClickEvent(ref.current);
Expand All @@ -1718,12 +1713,7 @@ describe('DOMModernPluginEventSystem', () => {
// Increase counter
root.render(<Test counter={1} />);
// Yield before committing
// Dev double-render
if (__DEV__) {
expect(Scheduler).toFlushAndYieldThrough(['Test', 'Test']);
} else {
expect(Scheduler).toFlushAndYieldThrough(['Test']);
}
expect(Scheduler).toFlushAndYieldThrough(['Test']);

// Click the button again
dispatchClickEvent(ref.current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ describe('DOMEventResponderSystem', () => {

const root = ReactDOM.createRoot(container);
root.render(<Test counter={0} />);
expect(Scheduler).toFlushAndYield(__DEV__ ? ['Test', 'Test'] : ['Test']);
expect(Scheduler).toFlushAndYield(['Test']);

// Click the button
dispatchClickEvent(ref.current);
Expand All @@ -823,9 +823,7 @@ describe('DOMEventResponderSystem', () => {
// Increase counter
root.render(<Test counter={1} />);
// Yield before committing
expect(Scheduler).toFlushAndYieldThrough(
__DEV__ ? ['Test', 'Test'] : ['Test'],
);
expect(Scheduler).toFlushAndYieldThrough(['Test']);

// Click the button again
dispatchClickEvent(ref.current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,13 @@ describe('ReactIncrementalErrorLogging', () => {
expect(Scheduler).toFlushAndYield(
[
'render: 0',
__DEV__ && 'render: 0', // replay

'render: 1',
__DEV__ && 'render: 1', // replay
__DEV__ && 'render: 1', // replay due to invokeGuardedCallback

// Retry one more time before handling error
'render: 1',
__DEV__ && 'render: 1', // replay
__DEV__ && 'render: 1', // replay due to invokeGuardedCallback

'componentWillUnmount: 0',
].filter(Boolean),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1103,9 +1103,7 @@ describe('ReactNewContext', () => {

// Render the provider again using a different renderer
ReactNoop.render(<App value={1} />);
expect(Scheduler).toFlushAndYield(
__DEV__ ? ['Foo', 'Foo', 'Foo', 'Foo'] : ['Foo', 'Foo'],
);
expect(Scheduler).toFlushAndYield(['Foo', 'Foo']);

if (__DEV__) {
expect(console.error.calls.argsFor(0)[0]).toContain(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe('ReactProfiler DevTools integration', () => {

// Commit something
root.update(<Text text="A" />);
expect(Scheduler).toFlushAndYield(__DEV__ ? ['A', 'A'] : ['A']);
expect(Scheduler).toFlushAndYield(['A']);
expect(root).toMatchRenderedOutput('A');

// Advance time by many seconds, larger than the default expiration time
Expand All @@ -200,7 +200,7 @@ describe('ReactProfiler DevTools integration', () => {
// Update B should not instantly expire.
expect(Scheduler).toFlushExpired([]);

expect(Scheduler).toFlushAndYield(__DEV__ ? ['B', 'B'] : ['B']);
expect(Scheduler).toFlushAndYield(['B']);
expect(root).toMatchRenderedOutput('B');
});
});
12 changes: 12 additions & 0 deletions packages/scheduler/src/forks/SchedulerHostConfig.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ export function unstable_flushAll(): void {
}

export function unstable_yieldValue(value: mixed): void {
// eslint-disable-next-line react-internal/no-production-logging
if (console.log.name === 'disabledLog') {
// If console.log has been patched, we assume we're in render
// replaying and we ignore any values yielding in the second pass.
return;
}
if (yieldedValues === null) {
yieldedValues = [value];
} else {
Expand All @@ -200,6 +206,12 @@ export function unstable_yieldValue(value: mixed): void {
}

export function unstable_advanceTime(ms: number) {
// eslint-disable-next-line react-internal/no-production-logging
if (console.log.name === 'disabledLog') {
// If console.log has been patched, we assume we're in render
// replaying and we ignore any time advancing in the second pass.
return;
}
currentTime += ms;
if (scheduledTimeout !== null && timeoutTime <= currentTime) {
scheduledTimeout(currentTime);
Expand Down

0 comments on commit 2d742e5

Please sign in to comment.