Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for useEffect ordering with useMemo #3360

Merged
merged 2 commits into from
Dec 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion hooks/test/browser/combinations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
useReducer,
useEffect,
useLayoutEffect,
useRef
useRef,
useMemo
} from 'preact/hooks';
import { scheduleEffectAssert } from '../_util/useEffectUtil';

Expand Down Expand Up @@ -298,4 +299,47 @@ describe('combinations', () => {
'effect outer call <span>hello 2</span>'
]);
});

it('should run effects child-first even for children separated by memoization', () => {
let ops = [];

/** @type {() => void} */
let updateChild;
/** @type {() => void} */
let updateParent;

function Child() {
const [, setCount] = useState(0);
updateChild = () => setCount(c => c + 1);
useEffect(() => {
ops.push('child effect');
});
return <div>Child</div>;
}

function Parent() {
const [, setCount] = useState(0);
updateParent = () => setCount(c => c + 1);
const memoedChild = useMemo(() => <Child />, []);
useEffect(() => {
ops.push('parent effect');
});
return (
<div>
<div>Parent</div>
{memoedChild}
</div>
);
}

act(() => render(<Parent />, scratch));
expect(ops).to.deep.equal(['child effect', 'parent effect']);

ops = [];
updateChild();
updateParent();
act(() => rerender());

expect(ops).to.deep.equal(['child effect', 'parent effect']);
});
});