|
| 1 | +import { beforeEach, expect } from "@jest/globals"; |
| 2 | +import { Activity, useState } from "react"; |
| 3 | + |
| 4 | +import type { Props } from "../reconciler"; |
| 5 | +import { createRoot } from "../renderer"; |
| 6 | +import { testGateReact19_2 } from "../test-utils/react-version"; |
| 7 | +import { renderWithAct } from "../test-utils/render"; |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + global.IS_REACT_ACT_ENVIRONMENT = true; |
| 11 | +}); |
| 12 | + |
| 13 | +const transformHiddenInstanceProps = ({ props }: { props: Props }) => ({ |
| 14 | + ...props, |
| 15 | + "data-is-hidden": true, |
| 16 | +}); |
| 17 | + |
| 18 | +testGateReact19_2("Activity hides content while preserving child state", async () => { |
| 19 | + let nextStateId = 0; |
| 20 | + |
| 21 | + function CounterPane() { |
| 22 | + const [stateId] = useState(() => ++nextStateId); |
| 23 | + return <div>Pane state: {stateId}</div>; |
| 24 | + } |
| 25 | + |
| 26 | + function App({ mode }: { mode: "hidden" | "visible" }) { |
| 27 | + return ( |
| 28 | + <Activity mode={mode}> |
| 29 | + <CounterPane /> |
| 30 | + </Activity> |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + const renderer = createRoot(); |
| 35 | + |
| 36 | + await renderWithAct(renderer, <App mode="visible" />); |
| 37 | + expect(renderer.container).toMatchInlineSnapshot(` |
| 38 | + <> |
| 39 | + <div> |
| 40 | + Pane state: |
| 41 | + 1 |
| 42 | + </div> |
| 43 | + </> |
| 44 | + `); |
| 45 | + |
| 46 | + await renderWithAct(renderer, <App mode="hidden" />); |
| 47 | + expect(renderer.container).toMatchInlineSnapshot(`< />`); |
| 48 | + |
| 49 | + await renderWithAct(renderer, <App mode="visible" />); |
| 50 | + expect(renderer.container).toMatchInlineSnapshot(` |
| 51 | + <> |
| 52 | + <div> |
| 53 | + Pane state: |
| 54 | + 1 |
| 55 | + </div> |
| 56 | + </> |
| 57 | + `); |
| 58 | +}); |
| 59 | + |
| 60 | +testGateReact19_2( |
| 61 | + "Activity keeps hidden instances in output with transformHiddenInstanceProps", |
| 62 | + async () => { |
| 63 | + function Pane() { |
| 64 | + return <div>Content</div>; |
| 65 | + } |
| 66 | + |
| 67 | + function App({ mode }: { mode: "hidden" | "visible" }) { |
| 68 | + return ( |
| 69 | + <Activity mode={mode}> |
| 70 | + <Pane /> |
| 71 | + </Activity> |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + const renderer = createRoot({ transformHiddenInstanceProps }); |
| 76 | + |
| 77 | + await renderWithAct(renderer, <App mode="visible" />); |
| 78 | + expect(renderer.container).toMatchInlineSnapshot(` |
| 79 | + <> |
| 80 | + <div> |
| 81 | + Content |
| 82 | + </div> |
| 83 | + </> |
| 84 | + `); |
| 85 | + |
| 86 | + await renderWithAct(renderer, <App mode="hidden" />); |
| 87 | + expect(renderer.container).toMatchInlineSnapshot(` |
| 88 | + <> |
| 89 | + <div |
| 90 | + data-is-hidden={true} |
| 91 | + > |
| 92 | + Content |
| 93 | + </div> |
| 94 | + </> |
| 95 | + `); |
| 96 | + |
| 97 | + await renderWithAct(renderer, <App mode="visible" />); |
| 98 | + expect(renderer.container).toMatchInlineSnapshot(` |
| 99 | + <> |
| 100 | + <div> |
| 101 | + Content |
| 102 | + </div> |
| 103 | + </> |
| 104 | + `); |
| 105 | + }, |
| 106 | +); |
0 commit comments