More test cases for useCallback

Adds test cases per the previous PR for useCallback: 

* callback that references another callback, which in turn references a 
possibly-mutated value 

* callback that references a ref
This commit is contained in:
Joe Savona
2023-12-15 15:19:39 -08:00
parent 5bfd70ac6f
commit 08e92a3a8d
9 changed files with 410 additions and 0 deletions
@@ -0,0 +1,84 @@
## Input
```javascript
// @enablePreserveExistingMemoizationGuarantees:false
import { useCallback } from "react";
import {
identity,
logValue,
makeObject_Primitives,
useHook,
} from "shared-runtime";
function Component(props) {
const object = makeObject_Primitives();
useHook();
const log = () => {
logValue(object);
};
const onClick = useCallback(() => {
log();
}, [log]);
identity(object);
return <div onClick={onClick} />;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
## Code
```javascript
// @enablePreserveExistingMemoizationGuarantees:false
import { useCallback, unstable_useMemoCache as useMemoCache } from "react";
import {
identity,
logValue,
makeObject_Primitives,
useHook,
} from "shared-runtime";
function Component(props) {
const $ = useMemoCache(1);
const object = makeObject_Primitives();
useHook();
const log = () => {
logValue(object);
};
const onClick = () => {
log();
};
identity(object);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = <div onClick={onClick} />;
$[0] = t0;
} else {
t0 = $[0];
}
return t0;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
### Eval output
(kind: ok) <div></div>
@@ -0,0 +1,31 @@
// @enablePreserveExistingMemoizationGuarantees:false
import { useCallback } from "react";
import {
identity,
logValue,
makeObject_Primitives,
useHook,
} from "shared-runtime";
function Component(props) {
const object = makeObject_Primitives();
useHook();
const log = () => {
logValue(object);
};
const onClick = useCallback(() => {
log();
}, [log]);
identity(object);
return <div onClick={onClick} />;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
@@ -0,0 +1,84 @@
## Input
```javascript
// @enablePreserveExistingMemoizationGuarantees
import { useCallback } from "react";
import {
identity,
logValue,
makeObject_Primitives,
useHook,
} from "shared-runtime";
function Component(props) {
const object = makeObject_Primitives();
useHook();
const log = () => {
logValue(object);
};
const onClick = useCallback(() => {
log();
}, [log]);
identity(object);
return <div onClick={onClick} />;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
## Code
```javascript
// @enablePreserveExistingMemoizationGuarantees
import { useCallback, unstable_useMemoCache as useMemoCache } from "react";
import {
identity,
logValue,
makeObject_Primitives,
useHook,
} from "shared-runtime";
function Component(props) {
const $ = useMemoCache(1);
const object = makeObject_Primitives();
useHook();
const log = () => {
logValue(object);
};
const onClick = () => {
log();
};
identity(object);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = <div onClick={onClick} />;
$[0] = t0;
} else {
t0 = $[0];
}
return t0;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
### Eval output
(kind: ok) <div></div>
@@ -0,0 +1,31 @@
// @enablePreserveExistingMemoizationGuarantees
import { useCallback } from "react";
import {
identity,
logValue,
makeObject_Primitives,
useHook,
} from "shared-runtime";
function Component(props) {
const object = makeObject_Primitives();
useHook();
const log = () => {
logValue(object);
};
const onClick = useCallback(() => {
log();
}, [log]);
identity(object);
return <div onClick={onClick} />;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
@@ -0,0 +1,69 @@
## Input
```javascript
// @enablePreserveExistingMemoizationGuarantees
import { useCallback, useRef } from "react";
function Component(props) {
const ref = useRef(null);
const onChange = useCallback((event) => {
// The ref should still be mutable here even though function deps are frozen in
// @enablePreserveExistingMemoizationGuarantees mode
ref.current = event.target.value;
});
return <input onChange={onChange} />;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
## Code
```javascript
// @enablePreserveExistingMemoizationGuarantees
import {
useCallback,
useRef,
unstable_useMemoCache as useMemoCache,
} from "react";
function Component(props) {
const $ = useMemoCache(3);
const ref = useRef(null);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = (event) => {
ref.current = event.target.value;
};
$[0] = t0;
} else {
t0 = $[0];
}
const onChange = t0;
let t1;
if ($[1] !== onChange) {
t1 = <input onChange={onChange} />;
$[1] = onChange;
$[2] = t1;
} else {
t1 = $[2];
}
return t1;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
### Eval output
(kind: ok) <input>
@@ -0,0 +1,19 @@
// @enablePreserveExistingMemoizationGuarantees
import { useCallback, useRef } from "react";
function Component(props) {
const ref = useRef(null);
const onChange = useCallback((event) => {
// The ref should still be mutable here even though function deps are frozen in
// @enablePreserveExistingMemoizationGuarantees mode
ref.current = event.target.value;
});
return <input onChange={onChange} />;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
@@ -0,0 +1,69 @@
## Input
```javascript
// @enablePreserveExistingMemoizationGuarantees
import { useCallback, useRef } from "react";
function Component(props) {
const ref = useRef(null);
const onChange = useCallback((event) => {
// The ref should still be mutable here even though function deps are frozen in
// @enablePreserveExistingMemoizationGuarantees mode
ref.current = event.target.value;
});
return <input onChange={onChange} />;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
## Code
```javascript
// @enablePreserveExistingMemoizationGuarantees
import {
useCallback,
useRef,
unstable_useMemoCache as useMemoCache,
} from "react";
function Component(props) {
const $ = useMemoCache(3);
const ref = useRef(null);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = (event) => {
ref.current = event.target.value;
};
$[0] = t0;
} else {
t0 = $[0];
}
const onChange = t0;
let t1;
if ($[1] !== onChange) {
t1 = <input onChange={onChange} />;
$[1] = onChange;
$[2] = t1;
} else {
t1 = $[2];
}
return t1;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
### Eval output
(kind: ok) <input>
@@ -0,0 +1,19 @@
// @enablePreserveExistingMemoizationGuarantees
import { useCallback, useRef } from "react";
function Component(props) {
const ref = useRef(null);
const onChange = useCallback((event) => {
// The ref should still be mutable here even though function deps are frozen in
// @enablePreserveExistingMemoizationGuarantees mode
ref.current = event.target.value;
});
return <input onChange={onChange} />;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
@@ -153,6 +153,10 @@ export function throwInput(x: Object): never {
throw x;
}
export function logValue<T>(value: T): void {
console.log(value);
}
export function useHook(): Object {
return makeObject_Primitives();
}