From 87d297210f25d8c13c13307b419840ed3e1ef9f6 Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Mon, 12 Aug 2024 11:56:21 -0400 Subject: [PATCH] [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: 2f1a071303983c67825a1836f0d278552b49a75d Pull Request resolved: https://github.com/facebook/react/pull/30662 --- .../__tests__/ReactFreshIntegration-test.js | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js b/packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js index c83e36f13a44b..2115a15f67705 100644 --- a/packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js +++ b/packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js @@ -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

{cacheMisses}

; + } + `); + 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

{cacheMisses}

; + } + `); + expect(container.firstChild).toBe(el); + // cache size changed between refreshes + expect(el.textContent).toBe('2'); + } + }); + describe('with inline requires', () => { beforeEach(() => { global.FakeModuleSystem = {};