/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import * as React from 'react'; import {render} from '@testing-library/react'; globalThis.constantValue = 'global test value'; test('literal-constant-propagation', () => { function Component() { 'use memo'; const x = 'test value 1'; return
{x}
; } const {asFragment, rerender} = render(); expect(asFragment()).toMatchInlineSnapshot(`
test value 1
`); rerender(); expect(asFragment()).toMatchInlineSnapshot(`
test value 1
`); }); test('global-constant-propagation', () => { function Component() { 'use memo'; const x = constantValue; return
{x}
; } const {asFragment, rerender} = render(); expect(asFragment()).toMatchInlineSnapshot(`
global test value
`); rerender(); expect(asFragment()).toMatchInlineSnapshot(`
global test value
`); }); test('lambda-constant-propagation', () => { function Component() { 'use memo'; const x = 'test value 1'; const getDiv = () =>
{x}
; return getDiv(); } const {asFragment, rerender} = render(); expect(asFragment()).toMatchInlineSnapshot(`
test value 1
`); rerender(); expect(asFragment()).toMatchInlineSnapshot(`
test value 1
`); }); test('lambda-constant-propagation-of-phi-node', () => { function Component({noopCallback}) { 'use memo'; const x = 'test value 1'; if (constantValue) { noopCallback(); } for (let i = 0; i < 5; i++) { if (!constantValue) { noopCallback(); } } const getDiv = () =>
{x}
; return getDiv(); } const {asFragment, rerender} = render( {}} />); expect(asFragment()).toMatchInlineSnapshot(`
test value 1
`); rerender( {}} />); expect(asFragment()).toMatchInlineSnapshot(`
test value 1
`); });