Skip to content

Commit ba1b688

Browse files
committed
Tests
1 parent 19860ab commit ba1b688

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

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

+38-6
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ let assertConsoleErrorDev;
125125

126126
describe('ReactFlight', () => {
127127
beforeEach(() => {
128+
// Mock performance.now for timing tests
129+
let time = 10;
130+
const now = jest.fn().mockImplementation(() => {
131+
return time++;
132+
});
133+
Object.defineProperty(performance, 'timeOrigin', {
134+
value: time,
135+
configurable: true,
136+
});
137+
Object.defineProperty(performance, 'now', {
138+
value: now,
139+
configurable: true,
140+
});
141+
128142
jest.resetModules();
129143
jest.mock('react', () => require('react/react.react-server'));
130144
ReactServer = require('react');
@@ -274,6 +288,7 @@ describe('ReactFlight', () => {
274288
});
275289
});
276290

291+
// @gate enableComponentPerformanceTrack
277292
it('can render a Client Component using a module reference and render there', async () => {
278293
function UserClient(props) {
279294
return (
@@ -300,6 +315,7 @@ describe('ReactFlight', () => {
300315
expect(getDebugInfo(greeting)).toEqual(
301316
__DEV__
302317
? [
318+
{time: 11},
303319
{
304320
name: 'Greeting',
305321
env: 'Server',
@@ -322,6 +338,7 @@ describe('ReactFlight', () => {
322338
expect(ReactNoop).toMatchRenderedOutput(<span>Hello, Seb Smith</span>);
323339
});
324340

341+
// @gate enableComponentPerformanceTrack
325342
it('can render a shared forwardRef Component', async () => {
326343
const Greeting = React.forwardRef(function Greeting(
327344
{firstName, lastName},
@@ -343,6 +360,7 @@ describe('ReactFlight', () => {
343360
expect(getDebugInfo(promise)).toEqual(
344361
__DEV__
345362
? [
363+
{time: 11},
346364
{
347365
name: 'Greeting',
348366
env: 'Server',
@@ -2659,6 +2677,7 @@ describe('ReactFlight', () => {
26592677
);
26602678
});
26612679

2680+
// @gate enableComponentPerformanceTrack
26622681
it('preserves debug info for server-to-server pass through', async () => {
26632682
function ThirdPartyLazyComponent() {
26642683
return <span>!</span>;
@@ -2705,6 +2724,7 @@ describe('ReactFlight', () => {
27052724
expect(getDebugInfo(promise)).toEqual(
27062725
__DEV__
27072726
? [
2727+
{time: 15},
27082728
{
27092729
name: 'ServerComponent',
27102730
env: 'Server',
@@ -2726,6 +2746,7 @@ describe('ReactFlight', () => {
27262746
expect(getDebugInfo(thirdPartyChildren[0])).toEqual(
27272747
__DEV__
27282748
? [
2749+
{time: 26},
27292750
{
27302751
name: 'ThirdPartyComponent',
27312752
env: 'third-party',
@@ -2736,12 +2757,14 @@ describe('ReactFlight', () => {
27362757
: undefined,
27372758
props: {},
27382759
},
2760+
{time: 17},
27392761
]
27402762
: undefined,
27412763
);
27422764
expect(getDebugInfo(thirdPartyChildren[1])).toEqual(
27432765
__DEV__
27442766
? [
2767+
{time: 27},
27452768
{
27462769
name: 'ThirdPartyLazyComponent',
27472770
env: 'third-party',
@@ -2758,6 +2781,7 @@ describe('ReactFlight', () => {
27582781
expect(getDebugInfo(thirdPartyChildren[2])).toEqual(
27592782
__DEV__
27602783
? [
2784+
{time: 25},
27612785
{
27622786
name: 'ThirdPartyFragmentComponent',
27632787
env: 'third-party',
@@ -2833,6 +2857,7 @@ describe('ReactFlight', () => {
28332857
expect(getDebugInfo(promise)).toEqual(
28342858
__DEV__
28352859
? [
2860+
{time: 13},
28362861
{
28372862
name: 'ServerComponent',
28382863
env: 'Server',
@@ -2853,6 +2878,7 @@ describe('ReactFlight', () => {
28532878
expect(getDebugInfo(thirdPartyFragment)).toEqual(
28542879
__DEV__
28552880
? [
2881+
{time: 14},
28562882
{
28572883
name: 'Keyed',
28582884
env: 'Server',
@@ -2872,6 +2898,7 @@ describe('ReactFlight', () => {
28722898
expect(getDebugInfo(thirdPartyFragment.props.children)).toEqual(
28732899
__DEV__
28742900
? [
2901+
{time: 23},
28752902
{
28762903
name: 'ThirdPartyAsyncIterableComponent',
28772904
env: 'third-party',
@@ -3017,6 +3044,7 @@ describe('ReactFlight', () => {
30173044
}
30183045
});
30193046

3047+
// @gate enableComponentPerformanceTrack
30203048
it('can change the environment name inside a component', async () => {
30213049
let env = 'A';
30223050
function Component(props) {
@@ -3041,6 +3069,7 @@ describe('ReactFlight', () => {
30413069
expect(getDebugInfo(greeting)).toEqual(
30423070
__DEV__
30433071
? [
3072+
{time: 11},
30443073
{
30453074
name: 'Component',
30463075
env: 'A',
@@ -3205,6 +3234,7 @@ describe('ReactFlight', () => {
32053234
);
32063235
});
32073236

3237+
// @gate enableComponentPerformanceTrack
32083238
it('uses the server component debug info as the element owner in DEV', async () => {
32093239
function Container({children}) {
32103240
return children;
@@ -3244,7 +3274,9 @@ describe('ReactFlight', () => {
32443274
},
32453275
};
32463276
expect(getDebugInfo(greeting)).toEqual([
3277+
{time: 11},
32473278
greetInfo,
3279+
{time: 12},
32483280
{
32493281
name: 'Container',
32503282
env: 'Server',
@@ -3265,7 +3297,7 @@ describe('ReactFlight', () => {
32653297
]);
32663298
// The owner that created the span was the outer server component.
32673299
// We expect the debug info to be referentially equal to the owner.
3268-
expect(greeting._owner).toBe(greeting._debugInfo[0]);
3300+
expect(greeting._owner).toBe(greeting._debugInfo[1]);
32693301
} else {
32703302
expect(greeting._debugInfo).toBe(undefined);
32713303
expect(greeting._owner).toBe(undefined);
@@ -3531,7 +3563,7 @@ describe('ReactFlight', () => {
35313563
expect(caughtError.digest).toBe('digest("my-error")');
35323564
});
35333565

3534-
// @gate __DEV__
3566+
// @gate __DEV__ && enableComponentPerformanceTrack
35353567
it('can render deep but cut off JSX in debug info', async () => {
35363568
function createDeepJSX(n) {
35373569
if (n <= 0) {
@@ -3555,7 +3587,7 @@ describe('ReactFlight', () => {
35553587
await act(async () => {
35563588
const rootModel = await ReactNoopFlightClient.read(transport);
35573589
const root = rootModel.root;
3558-
const children = root._debugInfo[0].props.children;
3590+
const children = root._debugInfo[1].props.children;
35593591
expect(children.type).toBe('div');
35603592
expect(children.props.children.type).toBe('div');
35613593
ReactNoop.render(root);
@@ -3564,7 +3596,7 @@ describe('ReactFlight', () => {
35643596
expect(ReactNoop).toMatchRenderedOutput(<div>not using props</div>);
35653597
});
35663598

3567-
// @gate __DEV__
3599+
// @gate __DEV__ && enableComponentPerformanceTrack
35683600
it('can render deep but cut off Map/Set in debug info', async () => {
35693601
function createDeepMap(n) {
35703602
if (n <= 0) {
@@ -3603,8 +3635,8 @@ describe('ReactFlight', () => {
36033635

36043636
await act(async () => {
36053637
const rootModel = await ReactNoopFlightClient.read(transport);
3606-
const set = rootModel.set._debugInfo[0].props.set;
3607-
const map = rootModel.map._debugInfo[0].props.map;
3638+
const set = rootModel.set._debugInfo[1].props.set;
3639+
const map = rootModel.map._debugInfo[1].props.map;
36083640
expect(set instanceof Set).toBe(true);
36093641
expect(set.size).toBe(1);
36103642
// eslint-disable-next-line no-for-of-loops/no-for-of-loops

packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMEdge-test.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ function normalizeCodeLocInfo(str) {
4949

5050
describe('ReactFlightDOMEdge', () => {
5151
beforeEach(() => {
52+
// Mock performance.now for timing tests
53+
let time = 10;
54+
const now = jest.fn().mockImplementation(() => {
55+
return time++;
56+
});
57+
Object.defineProperty(performance, 'timeOrigin', {
58+
value: time,
59+
configurable: true,
60+
});
61+
Object.defineProperty(performance, 'now', {
62+
value: now,
63+
configurable: true,
64+
});
65+
5266
jest.resetModules();
5367

5468
reactServerAct = require('internal-test-utils').serverAct;
@@ -401,7 +415,7 @@ describe('ReactFlightDOMEdge', () => {
401415

402416
const serializedContent = await readResult(stream1);
403417

404-
expect(serializedContent.length).toBeLessThan(425);
418+
expect(serializedContent.length).toBeLessThan(490);
405419
expect(timesRendered).toBeLessThan(5);
406420

407421
const model = await ReactServerDOMClient.createFromReadableStream(stream2, {
@@ -472,7 +486,7 @@ describe('ReactFlightDOMEdge', () => {
472486
const [stream1, stream2] = passThrough(stream).tee();
473487

474488
const serializedContent = await readResult(stream1);
475-
expect(serializedContent.length).toBeLessThan(__DEV__ ? 605 : 400);
489+
expect(serializedContent.length).toBeLessThan(__DEV__ ? 680 : 400);
476490
expect(timesRendered).toBeLessThan(5);
477491

478492
const model = await serverAct(() =>
@@ -506,7 +520,7 @@ describe('ReactFlightDOMEdge', () => {
506520
),
507521
);
508522
const serializedContent = await readResult(stream);
509-
const expectedDebugInfoSize = __DEV__ ? 300 * 20 : 0;
523+
const expectedDebugInfoSize = __DEV__ ? 320 * 20 : 0;
510524
expect(serializedContent.length).toBeLessThan(150 + expectedDebugInfoSize);
511525
});
512526

@@ -934,6 +948,7 @@ describe('ReactFlightDOMEdge', () => {
934948
);
935949
});
936950

951+
// @gate enableComponentPerformanceTrack
937952
it('supports async server component debug info as the element owner in DEV', async () => {
938953
function Container({children}) {
939954
return children;
@@ -989,7 +1004,9 @@ describe('ReactFlightDOMEdge', () => {
9891004
owner: null,
9901005
});
9911006
expect(lazyWrapper._debugInfo).toEqual([
1007+
{time: 11},
9921008
greetInfo,
1009+
{time: 12},
9931010
expect.objectContaining({
9941011
name: 'Container',
9951012
env: 'Server',
@@ -998,7 +1015,7 @@ describe('ReactFlightDOMEdge', () => {
9981015
]);
9991016
// The owner that created the span was the outer server component.
10001017
// We expect the debug info to be referentially equal to the owner.
1001-
expect(greeting._owner).toBe(lazyWrapper._debugInfo[0]);
1018+
expect(greeting._owner).toBe(lazyWrapper._debugInfo[1]);
10021019
} else {
10031020
expect(lazyWrapper._debugInfo).toBe(undefined);
10041021
expect(greeting._owner).toBe(undefined);

0 commit comments

Comments
 (0)