Skip to content

Commit 1528c5c

Browse files
authored
SchedulerMock.unstable_yieldValue -> SchedulerMock.log (#26312)
(This only affects our own internal repo; it's not a public API.) I think most of us agree this is a less confusing name. It's possible someone will confuse it with `console.log`. If that becomes a problem we can warn in dev or something.
1 parent 4bbac04 commit 1528c5c

File tree

99 files changed

+2286
-2889
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+2286
-2889
lines changed

packages/internal-test-utils/ReactInternalTestUtils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {equals} from '@jest/expect-utils';
1313
import enqueueTask from './enqueueTask';
1414

1515
function assertYieldsWereCleared(Scheduler) {
16-
const actualYields = Scheduler.unstable_clearYields();
16+
const actualYields = Scheduler.unstable_clearLog();
1717
if (actualYields.length !== 0) {
1818
const error = Error(
1919
'The event log is not empty. Call assertLog(...) first.',
@@ -45,14 +45,14 @@ export async function waitFor(expectedLog) {
4545
SchedulerMock.unstable_flushNumberOfYields(
4646
expectedLog.length - actualLog.length,
4747
);
48-
actualLog.push(...SchedulerMock.unstable_clearYields());
48+
actualLog.push(...SchedulerMock.unstable_clearLog());
4949
if (expectedLog.length > actualLog.length) {
5050
// Continue flushing until we've logged the expected number of items.
5151
} else {
5252
// Once we've reached the expected sequence, wait one more microtask to
5353
// flush any remaining synchronous work.
5454
await waitForMicrotasks();
55-
actualLog.push(...SchedulerMock.unstable_clearYields());
55+
actualLog.push(...SchedulerMock.unstable_clearLog());
5656
break;
5757
}
5858
} else {
@@ -91,7 +91,7 @@ export async function waitForAll(expectedLog) {
9191
SchedulerMock.unstable_flushAllWithoutAsserting();
9292
} while (true);
9393

94-
const actualLog = SchedulerMock.unstable_clearYields();
94+
const actualLog = SchedulerMock.unstable_clearLog();
9595
if (equals(actualLog, expectedLog)) {
9696
return;
9797
}
@@ -166,7 +166,7 @@ export async function waitForPaint(expectedLog) {
166166
await waitForMicrotasks();
167167
}
168168

169-
const actualLog = SchedulerMock.unstable_clearYields();
169+
const actualLog = SchedulerMock.unstable_clearLog();
170170
if (equals(actualLog, expectedLog)) {
171171
return;
172172
}
@@ -180,7 +180,7 @@ ${diff(expectedLog, actualLog)}
180180
}
181181

182182
export function assertLog(expectedLog) {
183-
const actualLog = SchedulerMock.unstable_clearYields();
183+
const actualLog = SchedulerMock.unstable_clearLog();
184184
if (equals(actualLog, expectedLog)) {
185185
return;
186186
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails react-core
8+
* @jest-environment node
9+
*/
10+
11+
'use strict';
12+
13+
const React = require('react');
14+
const {startTransition, useDeferredValue} = React;
15+
const ReactNoop = require('react-noop-renderer');
16+
const {
17+
waitFor,
18+
waitForAll,
19+
waitForPaint,
20+
waitForThrow,
21+
assertLog,
22+
} = require('internal-test-utils');
23+
const act = require('jest-react').act;
24+
const Scheduler = require('scheduler/unstable_mock');
25+
26+
describe('ReactInternalTestUtils', () => {
27+
test('waitFor', async () => {
28+
const Yield = ({id}) => {
29+
Scheduler.log(id);
30+
return id;
31+
};
32+
33+
const root = ReactNoop.createRoot();
34+
startTransition(() => {
35+
root.render(
36+
<div>
37+
<Yield id="foo" />
38+
<Yield id="bar" />
39+
<Yield id="baz" />
40+
</div>
41+
);
42+
});
43+
44+
await waitFor(['foo', 'bar']);
45+
expect(root).toMatchRenderedOutput(null);
46+
await waitFor(['baz']);
47+
expect(root).toMatchRenderedOutput(null);
48+
await waitForAll([]);
49+
expect(root).toMatchRenderedOutput(<div>foobarbaz</div>);
50+
});
51+
52+
test('waitForAll', async () => {
53+
const Yield = ({id}) => {
54+
Scheduler.log(id);
55+
return id;
56+
};
57+
58+
const root = ReactNoop.createRoot();
59+
startTransition(() => {
60+
root.render(
61+
<div>
62+
<Yield id="foo" />
63+
<Yield id="bar" />
64+
<Yield id="baz" />
65+
</div>
66+
);
67+
});
68+
69+
await waitForAll(['foo', 'bar', 'baz']);
70+
expect(root).toMatchRenderedOutput(<div>foobarbaz</div>);
71+
});
72+
73+
test('waitForThrow', async () => {
74+
const Yield = ({id}) => {
75+
Scheduler.log(id);
76+
return id;
77+
};
78+
79+
function BadRender() {
80+
throw new Error('Oh no!');
81+
}
82+
83+
function App() {
84+
return (
85+
<div>
86+
<Yield id="A" />
87+
<Yield id="B" />
88+
<BadRender />
89+
<Yield id="C" />
90+
<Yield id="D" />
91+
</div>
92+
);
93+
}
94+
95+
const root = ReactNoop.createRoot();
96+
root.render(<App />);
97+
98+
await waitForThrow('Oh no!');
99+
assertLog([
100+
'A',
101+
'B',
102+
'C',
103+
'D',
104+
// React will try one more time before giving up.
105+
'A',
106+
'B',
107+
'C',
108+
'D',
109+
]);
110+
});
111+
112+
test('waitForPaint', async () => {
113+
function App({prop}) {
114+
const deferred = useDeferredValue(prop);
115+
const text = `Urgent: ${prop}, Deferred: ${deferred}`;
116+
Scheduler.log(text);
117+
return text;
118+
}
119+
120+
const root = ReactNoop.createRoot();
121+
root.render(<App prop="A" />);
122+
123+
await waitForAll(['Urgent: A, Deferred: A']);
124+
expect(root).toMatchRenderedOutput('Urgent: A, Deferred: A');
125+
126+
// This update will result in two separate paints: an urgent one, and a
127+
// deferred one.
128+
root.render(<App prop="B" />);
129+
// Urgent paint
130+
await waitForPaint(['Urgent: B, Deferred: A']);
131+
expect(root).toMatchRenderedOutput('Urgent: B, Deferred: A');
132+
133+
// Deferred paint
134+
await waitForPaint(['Urgent: B, Deferred: B']);
135+
expect(root).toMatchRenderedOutput('Urgent: B, Deferred: B');
136+
});
137+
138+
test('assertLog', async () => {
139+
const Yield = ({id}) => {
140+
Scheduler.log(id);
141+
return id;
142+
};
143+
144+
function App() {
145+
return (
146+
<div>
147+
<Yield id="A" />
148+
<Yield id="B" />
149+
<Yield id="C" />
150+
</div>
151+
);
152+
}
153+
154+
const root = ReactNoop.createRoot();
155+
await act(async () => {
156+
root.render(<App />);
157+
});
158+
assertLog(['A', 'B', 'C']);
159+
});
160+
});

packages/jest-react/src/JestReact.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function captureAssertion(fn) {
2929

3030
function assertYieldsWereCleared(root) {
3131
const Scheduler = root._Scheduler;
32-
const actualYields = Scheduler.unstable_clearYields();
32+
const actualYields = Scheduler.unstable_clearLog();
3333
if (actualYields.length !== 0) {
3434
const error = Error(
3535
'Log of yielded values is not empty. ' +

packages/react-cache/src/__tests__/ReactCacheOld-test.internal.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,12 @@ describe('ReactCache', () => {
5454
listeners = [{resolve, reject}];
5555
setTimeout(() => {
5656
if (textResourceShouldFail) {
57-
Scheduler.unstable_yieldValue(
58-
`Promise rejected [${text}]`,
59-
);
57+
Scheduler.log(`Promise rejected [${text}]`);
6058
status = 'rejected';
6159
value = new Error('Failed to load: ' + text);
6260
listeners.forEach(listener => listener.reject(value));
6361
} else {
64-
Scheduler.unstable_yieldValue(
65-
`Promise resolved [${text}]`,
66-
);
62+
Scheduler.log(`Promise resolved [${text}]`);
6763
status = 'resolved';
6864
value = text;
6965
listeners.forEach(listener => listener.resolve(value));
@@ -93,21 +89,21 @@ describe('ReactCache', () => {
9389
});
9490

9591
function Text(props) {
96-
Scheduler.unstable_yieldValue(props.text);
92+
Scheduler.log(props.text);
9793
return props.text;
9894
}
9995

10096
function AsyncText(props) {
10197
const text = props.text;
10298
try {
10399
TextResource.read([props.text, props.ms]);
104-
Scheduler.unstable_yieldValue(text);
100+
Scheduler.log(text);
105101
return text;
106102
} catch (promise) {
107103
if (typeof promise.then === 'function') {
108-
Scheduler.unstable_yieldValue(`Suspend! [${text}]`);
104+
Scheduler.log(`Suspend! [${text}]`);
109105
} else {
110-
Scheduler.unstable_yieldValue(`Error! [${text}]`);
106+
Scheduler.log(`Error! [${text}]`);
111107
}
112108
throw promise;
113109
}
@@ -171,7 +167,7 @@ describe('ReactCache', () => {
171167
});
172168

173169
function App() {
174-
Scheduler.unstable_yieldValue('App');
170+
Scheduler.log('App');
175171
return BadTextResource.read(['Hi', 100]);
176172
}
177173

@@ -322,13 +318,13 @@ describe('ReactCache', () => {
322318
const text = props.text;
323319
try {
324320
const actualText = BadTextResource.read([props.text, props.ms]);
325-
Scheduler.unstable_yieldValue(actualText);
321+
Scheduler.log(actualText);
326322
return actualText;
327323
} catch (promise) {
328324
if (typeof promise.then === 'function') {
329-
Scheduler.unstable_yieldValue(`Suspend! [${text}]`);
325+
Scheduler.log(`Suspend! [${text}]`);
330326
} else {
331-
Scheduler.unstable_yieldValue(`Error! [${text}]`);
327+
Scheduler.log(`Error! [${text}]`);
332328
}
333329
throw promise;
334330
}

packages/react-client/src/__tests__/ReactFlight-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ describe('ReactFlight', () => {
827827
}
828828

829829
function ClientDoubler({el}) {
830-
Scheduler.unstable_yieldValue('ClientDoubler');
830+
Scheduler.log('ClientDoubler');
831831
return (
832832
<>
833833
{el}
@@ -1018,10 +1018,10 @@ describe('ReactFlight', () => {
10181018

10191019
function Bar() {
10201020
if (!promise.unsuspend) {
1021-
Scheduler.unstable_yieldValue('suspended');
1021+
Scheduler.log('suspended');
10221022
throw promise;
10231023
}
1024-
Scheduler.unstable_yieldValue('rendered');
1024+
Scheduler.log('rendered');
10251025
const context = React.useContext(ServerContext);
10261026
return context;
10271027
}
@@ -1055,7 +1055,7 @@ describe('ReactFlight', () => {
10551055
);
10561056

10571057
function ClientBar() {
1058-
Scheduler.unstable_yieldValue('ClientBar');
1058+
Scheduler.log('ClientBar');
10591059
const context = React.useContext(ServerContext);
10601060
return <span>{context}</span>;
10611061
}

0 commit comments

Comments
 (0)