| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import * as React from 'react'; |
| 9 | import {render} from '@testing-library/react'; |
| 10 | |
| 11 | globalThis.constantValue = 'global test value'; |
| 12 | |
| 13 | test('literal-constant-propagation', () => { |
| 14 | function Component() { |
| 15 | 'use memo'; |
| 16 | const x = 'test value 1'; |
| 17 | return <div>{x}</div>; |
| 18 | } |
| 19 | const {asFragment, rerender} = render(<Component />); |
| 20 | |
| 21 | expect(asFragment()).toMatchInlineSnapshot(` |
| 22 | <DocumentFragment> |
| 23 | <div> |
| 24 | test value 1 |
| 25 | </div> |
| 26 | </DocumentFragment> |
| 27 | `); |
| 28 | |
| 29 | rerender(<Component />); |
| 30 | |
| 31 | expect(asFragment()).toMatchInlineSnapshot(` |
| 32 | <DocumentFragment> |
| 33 | <div> |
| 34 | test value 1 |
| 35 | </div> |
| 36 | </DocumentFragment> |
| 37 | `); |
| 38 | }); |
| 39 | |
| 40 | test('global-constant-propagation', () => { |
| 41 | function Component() { |
| 42 | 'use memo'; |
| 43 | const x = constantValue; |
| 44 | |
| 45 | return <div>{x}</div>; |
| 46 | } |
| 47 | const {asFragment, rerender} = render(<Component />); |
| 48 | |
| 49 | expect(asFragment()).toMatchInlineSnapshot(` |
| 50 | <DocumentFragment> |
| 51 | <div> |
| 52 | global test value |
| 53 | </div> |
| 54 | </DocumentFragment> |
| 55 | `); |
| 56 | |
| 57 | rerender(<Component />); |
| 58 | |
| 59 | expect(asFragment()).toMatchInlineSnapshot(` |
| 60 | <DocumentFragment> |
| 61 | <div> |
| 62 | global test value |
| 63 | </div> |
| 64 | </DocumentFragment> |
| 65 | `); |
| 66 | }); |
| 67 | |
| 68 | test('lambda-constant-propagation', () => { |
| 69 | function Component() { |
| 70 | 'use memo'; |
| 71 | const x = 'test value 1'; |
| 72 | const getDiv = () => <div>{x}</div>; |
| 73 | return getDiv(); |
| 74 | } |
| 75 | const {asFragment, rerender} = render(<Component />); |
| 76 | |
| 77 | expect(asFragment()).toMatchInlineSnapshot(` |
| 78 | <DocumentFragment> |
| 79 | <div> |
| 80 | test value 1 |
| 81 | </div> |
| 82 | </DocumentFragment> |
| 83 | `); |
| 84 | |
| 85 | rerender(<Component />); |
| 86 | |
| 87 | expect(asFragment()).toMatchInlineSnapshot(` |
| 88 | <DocumentFragment> |
| 89 | <div> |
| 90 | test value 1 |
| 91 | </div> |
| 92 | </DocumentFragment> |
| 93 | `); |
| 94 | }); |
| 95 | |
| 96 | test('lambda-constant-propagation-of-phi-node', () => { |
| 97 | function Component({noopCallback}) { |
| 98 | 'use memo'; |
| 99 | const x = 'test value 1'; |
| 100 | if (constantValue) { |
| 101 | noopCallback(); |
| 102 | } |
| 103 | for (let i = 0; i < 5; i++) { |
| 104 | if (!constantValue) { |
| 105 | noopCallback(); |
| 106 | } |
| 107 | } |
| 108 | const getDiv = () => <div>{x}</div>; |
| 109 | return getDiv(); |
| 110 | } |
| 111 | |
| 112 | const {asFragment, rerender} = render(<Component noopCallback={() => {}} />); |
| 113 | |
| 114 | expect(asFragment()).toMatchInlineSnapshot(` |
| 115 | <DocumentFragment> |
| 116 | <div> |
| 117 | test value 1 |
| 118 | </div> |
| 119 | </DocumentFragment> |
| 120 | `); |
| 121 | |
| 122 | rerender(<Component noopCallback={() => {}} />); |
| 123 | |
| 124 | expect(asFragment()).toMatchInlineSnapshot(` |
| 125 | <DocumentFragment> |
| 126 | <div> |
| 127 | test value 1 |
| 128 | </div> |
| 129 | </DocumentFragment> |
| 130 | `); |
| 131 | }); |