Skip to content

Commit 7b020c0

Browse files
committed
Current behavior for use resulting in stuck isPending
1 parent 81c5ff2 commit 7b020c0

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

packages/react-reconciler/src/__tests__/ReactUse-test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ let act;
1616
let use;
1717
let useDebugValue;
1818
let useState;
19+
let useTransition;
1920
let useMemo;
2021
let useEffect;
2122
let Suspense;
@@ -38,6 +39,7 @@ describe('ReactUse', () => {
3839
use = React.use;
3940
useDebugValue = React.useDebugValue;
4041
useState = React.useState;
42+
useTransition = React.useTransition;
4143
useMemo = React.useMemo;
4244
useEffect = React.useEffect;
4345
Suspense = React.Suspense;
@@ -1915,4 +1917,62 @@ describe('ReactUse', () => {
19151917
assertLog(['Hi', 'World']);
19161918
expect(root).toMatchRenderedOutput(<div>Hi World</div>);
19171919
});
1920+
1921+
it('does not get stuck in pending statewith usable values in state', async () => {
1922+
const initial = new Promise(resolve => {
1923+
setTimeout(() => {
1924+
resolve('initial');
1925+
}, 1000);
1926+
});
1927+
let click;
1928+
function Reader({promise, setPromise}) {
1929+
const value = use(promise);
1930+
const [isPending, startLocalTransition] = useTransition();
1931+
click = () => {
1932+
startLocalTransition(() => {
1933+
setPromise(
1934+
new Promise(resolve => {
1935+
setTimeout(() => {
1936+
resolve('updated');
1937+
}, 1000);
1938+
}),
1939+
);
1940+
});
1941+
};
1942+
1943+
Scheduler.log(`Value: ${value}, Pending: ${String(isPending)}`);
1944+
1945+
return (
1946+
<>
1947+
Value: {value}, Pending: {String(isPending)}
1948+
</>
1949+
);
1950+
}
1951+
function App() {
1952+
const [promise, setPromise] = useState(initial);
1953+
1954+
return (
1955+
<Suspense fallback="Loading">
1956+
<Reader promise={promise} setPromise={setPromise} />
1957+
</Suspense>
1958+
);
1959+
}
1960+
1961+
const root = ReactNoop.createRoot();
1962+
await act(() => {
1963+
root.render(<App />);
1964+
});
1965+
assertLog(['Value: initial, Pending: false']);
1966+
expect(root).toMatchRenderedOutput('Value: initial, Pending: false');
1967+
1968+
await act(() => {
1969+
click();
1970+
});
1971+
1972+
assertLog([
1973+
'Value: initial, Pending: true',
1974+
'Value: updated, Pending: true',
1975+
]);
1976+
expect(root).toMatchRenderedOutput('Value: updated, Pending: true');
1977+
});
19181978
});

0 commit comments

Comments
 (0)