Skip to content

Commit 5b1d0f0

Browse files
author
Brian Vaughn
committed
Split useRef tests into separate file
1 parent b225d4f commit 5b1d0f0

File tree

2 files changed

+128
-86
lines changed

2 files changed

+128
-86
lines changed

packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.internal.js

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ describe('ReactHooksWithNoopRenderer', () => {
14171417
it('does not show a warning when a component updates a childs state from within passive unmount function', () => {
14181418
function Parent() {
14191419
Scheduler.unstable_yieldValue('Parent');
1420-
const updaterRef = React.useRef(null);
1420+
const updaterRef = useRef(null);
14211421
React.useEffect(() => {
14221422
Scheduler.unstable_yieldValue('Parent passive create');
14231423
return () => {
@@ -2526,91 +2526,6 @@ describe('ReactHooksWithNoopRenderer', () => {
25262526
});
25272527
});
25282528

2529-
describe('useRef', () => {
2530-
it('creates a ref object initialized with the provided value', () => {
2531-
jest.useFakeTimers();
2532-
2533-
function useDebouncedCallback(callback, ms, inputs) {
2534-
const timeoutID = useRef(-1);
2535-
useEffect(() => {
2536-
return function unmount() {
2537-
clearTimeout(timeoutID.current);
2538-
};
2539-
}, []);
2540-
const debouncedCallback = useCallback(
2541-
(...args) => {
2542-
clearTimeout(timeoutID.current);
2543-
timeoutID.current = setTimeout(callback, ms, ...args);
2544-
},
2545-
[callback, ms],
2546-
);
2547-
return useCallback(debouncedCallback, inputs);
2548-
}
2549-
2550-
let ping;
2551-
function App() {
2552-
ping = useDebouncedCallback(
2553-
value => {
2554-
Scheduler.unstable_yieldValue('ping: ' + value);
2555-
},
2556-
100,
2557-
[],
2558-
);
2559-
return null;
2560-
}
2561-
2562-
act(() => {
2563-
ReactNoop.render(<App />);
2564-
});
2565-
expect(Scheduler).toHaveYielded([]);
2566-
2567-
ping(1);
2568-
ping(2);
2569-
ping(3);
2570-
2571-
expect(Scheduler).toHaveYielded([]);
2572-
2573-
jest.advanceTimersByTime(100);
2574-
2575-
expect(Scheduler).toHaveYielded(['ping: 3']);
2576-
2577-
ping(4);
2578-
jest.advanceTimersByTime(20);
2579-
ping(5);
2580-
ping(6);
2581-
jest.advanceTimersByTime(80);
2582-
2583-
expect(Scheduler).toHaveYielded([]);
2584-
2585-
jest.advanceTimersByTime(20);
2586-
expect(Scheduler).toHaveYielded(['ping: 6']);
2587-
});
2588-
2589-
it('should return the same ref during re-renders', () => {
2590-
function Counter() {
2591-
const ref = useRef('val');
2592-
const [count, setCount] = useState(0);
2593-
const [firstRef] = useState(ref);
2594-
2595-
if (firstRef !== ref) {
2596-
throw new Error('should never change');
2597-
}
2598-
2599-
if (count < 3) {
2600-
setCount(count + 1);
2601-
}
2602-
2603-
return <Text text={ref.current} />;
2604-
}
2605-
2606-
ReactNoop.render(<Counter />);
2607-
expect(Scheduler).toFlushAndYield(['val']);
2608-
2609-
ReactNoop.render(<Counter />);
2610-
expect(Scheduler).toFlushAndYield(['val']);
2611-
});
2612-
});
2613-
26142529
describe('useImperativeHandle', () => {
26152530
it('does not update when deps are the same', () => {
26162531
const INCREMENT = 'INCREMENT';
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
/* eslint-disable no-func-assign */
12+
13+
'use strict';
14+
15+
describe('useRef', () => {
16+
let React;
17+
let ReactNoop;
18+
let Scheduler;
19+
let act;
20+
let useCallback;
21+
let useEffect;
22+
let useRef;
23+
let useState;
24+
25+
beforeEach(() => {
26+
React = require('react');
27+
ReactNoop = require('react-noop-renderer');
28+
Scheduler = require('scheduler');
29+
30+
const ReactFeatureFlags = require('shared/ReactFeatureFlags');
31+
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
32+
33+
act = ReactNoop.act;
34+
useCallback = React.useCallback;
35+
useEffect = React.useEffect;
36+
useRef = React.useRef;
37+
useState = React.useState;
38+
});
39+
40+
function Text(props) {
41+
Scheduler.unstable_yieldValue(props.text);
42+
return <span prop={props.text} />;
43+
}
44+
45+
it('creates a ref object initialized with the provided value', () => {
46+
jest.useFakeTimers();
47+
48+
function useDebouncedCallback(callback, ms, inputs) {
49+
const timeoutID = useRef(-1);
50+
useEffect(() => {
51+
return function unmount() {
52+
clearTimeout(timeoutID.current);
53+
};
54+
}, []);
55+
const debouncedCallback = useCallback(
56+
(...args) => {
57+
clearTimeout(timeoutID.current);
58+
timeoutID.current = setTimeout(callback, ms, ...args);
59+
},
60+
[callback, ms],
61+
);
62+
return useCallback(debouncedCallback, inputs);
63+
}
64+
65+
let ping;
66+
function App() {
67+
ping = useDebouncedCallback(
68+
value => {
69+
Scheduler.unstable_yieldValue('ping: ' + value);
70+
},
71+
100,
72+
[],
73+
);
74+
return null;
75+
}
76+
77+
act(() => {
78+
ReactNoop.render(<App />);
79+
});
80+
expect(Scheduler).toHaveYielded([]);
81+
82+
ping(1);
83+
ping(2);
84+
ping(3);
85+
86+
expect(Scheduler).toHaveYielded([]);
87+
88+
jest.advanceTimersByTime(100);
89+
90+
expect(Scheduler).toHaveYielded(['ping: 3']);
91+
92+
ping(4);
93+
jest.advanceTimersByTime(20);
94+
ping(5);
95+
ping(6);
96+
jest.advanceTimersByTime(80);
97+
98+
expect(Scheduler).toHaveYielded([]);
99+
100+
jest.advanceTimersByTime(20);
101+
expect(Scheduler).toHaveYielded(['ping: 6']);
102+
});
103+
104+
it('should return the same ref during re-renders', () => {
105+
function Counter() {
106+
const ref = useRef('val');
107+
const [count, setCount] = useState(0);
108+
const [firstRef] = useState(ref);
109+
110+
if (firstRef !== ref) {
111+
throw new Error('should never change');
112+
}
113+
114+
if (count < 3) {
115+
setCount(count + 1);
116+
}
117+
118+
return <Text text={ref.current} />;
119+
}
120+
121+
ReactNoop.render(<Counter />);
122+
expect(Scheduler).toFlushAndYield(['val']);
123+
124+
ReactNoop.render(<Counter />);
125+
expect(Scheduler).toFlushAndYield(['val']);
126+
});
127+
});

0 commit comments

Comments
 (0)