Skip to content

Commit

Permalink
[fresh] Add failing test for useMemoCache reset case
Browse files Browse the repository at this point in the history
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: 2f1a071303983c67825a1836f0d278552b49a75d
Pull Request resolved: #30662
  • Loading branch information
poteto committed Aug 12, 2024
1 parent 68dbd84 commit 87d2972
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,61 @@ describe('ReactFreshIntegration', () => {
}
});

// eslint-disable-next-line jest/no-disabled-tests
it.skip('resets useMemoCache cache slots', async () => {
if (__DEV__) {
await render(`
const useMemoCache = require('react/compiler-runtime').c;
let cacheMisses = 0;
const cacheMiss = (id) => {
cacheMisses++;
return id;
};
export default function App(t0) {
const $ = useMemoCache(1);
const {reset1} = t0;
let t1;
if ($[0] !== reset1) {
$[0] = t1 = cacheMiss({reset1});
} else {
t1 = $[1];
}
return <h1>{cacheMisses}</h1>;
}
`);
const el = container.firstChild;
expect(el.textContent).toBe('1');
await patch(`
const useMemoCache = require('react/compiler-runtime').c;
let cacheMisses = 0;
const cacheMiss = (id) => {
cacheMisses++;
return id;
};
export default function App(t0) {
const $ = useMemoCache(2);
const {reset1, reset2} = t0;
let t1;
if ($[0] !== reset1) {
$[0] = t1 = cacheMiss({reset1});
} else {
t1 = $[1];
}
let t2;
if ($[1] !== reset2) {
$[1] = t2 = cacheMiss({reset2});
} else {
t2 = $[1];
}
return <h1>{cacheMisses}</h1>;
}
`);
expect(container.firstChild).toBe(el);
// cache size changed between refreshes
expect(el.textContent).toBe('2');
}
});

describe('with inline requires', () => {
beforeEach(() => {
global.FakeModuleSystem = {};
Expand Down

0 comments on commit 87d2972

Please sign in to comment.