@samitouri / QOS-React / commits / d9eb1540e2

[fresh] Add failing test for useMemoCache reset case

During local development it's common to add or remove code which may change the cache size between renders. Add a failing test to show that currently (without the compiled fast refresh check) this issues a warning and reuses the cache which may have stale values. ghstack-source-id: efdcb017ba3bdadd88b1f8bb5523b1a9f6217eb5 Pull Request resolved: https://github.com/facebook/react/pull/30662

Lauren Tan committed Aug 14, 2024 at 14:45 UTC d9eb1540e242a8679659f58719546320a2a28b2b
1 file changed +55
packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js
+55
@@ -1637,6 +1637,61 @@ describe('ReactFreshIntegration', () => {
1637 }
1638 });
1639
1640 + // eslint-disable-next-line jest/no-disabled-tests
1641 + it.skip('resets useMemoCache cache slots', async () => {
1642 + if (__DEV__) {
1643 + await render(`
1644 + const useMemoCache = require('react/compiler-runtime').c;
1645 + let cacheMisses = 0;
1646 + const cacheMiss = (id) => {
1647 + cacheMisses++;
1648 + return id;
1649 + };
1650 + export default function App(t0) {
1651 + const $ = useMemoCache(1);
1652 + const {reset1} = t0;
1653 + let t1;
1654 + if ($[0] !== reset1) {
1655 + $[0] = t1 = cacheMiss({reset1});
1656 + } else {
1657 + t1 = $[1];
1658 + }
1659 + return <h1>{cacheMisses}</h1>;
1660 + }
1661 + `);
1662 + const el = container.firstChild;
1663 + expect(el.textContent).toBe('1');
1664 + await patch(`
1665 + const useMemoCache = require('react/compiler-runtime').c;
1666 + let cacheMisses = 0;
1667 + const cacheMiss = (id) => {
1668 + cacheMisses++;
1669 + return id;
1670 + };
1671 + export default function App(t0) {
1672 + const $ = useMemoCache(2);
1673 + const {reset1, reset2} = t0;
1674 + let t1;
1675 + if ($[0] !== reset1) {
1676 + $[0] = t1 = cacheMiss({reset1});
1677 + } else {
1678 + t1 = $[1];
1679 + }
1680 + let t2;
1681 + if ($[1] !== reset2) {
1682 + $[1] = t2 = cacheMiss({reset2});
1683 + } else {
1684 + t2 = $[1];
1685 + }
1686 + return <h1>{cacheMisses}</h1>;
1687 + }
1688 + `);
1689 + expect(container.firstChild).toBe(el);
1690 + // cache size changed between refreshes
1691 + expect(el.textContent).toBe('2');
1692 + }
1693 + });
1694 +
1695 describe('with inline requires', () => {
1696 beforeEach(() => {
1697 global.FakeModuleSystem = {};