|
1 | 1 | import { beforeEach, expect, jest, test } from "@jest/globals"; |
2 | | -import { Component, useEffect, useId } from "react"; |
| 2 | +import { Component, Suspense, use, useEffect, useId } from "react"; |
3 | 3 |
|
4 | 4 | import { createRoot } from "../renderer"; |
5 | | -import { renderWithAct } from "../test-utils/render"; |
| 5 | +import { act, renderWithAct } from "../test-utils/render"; |
| 6 | +import type { JsonElement, JsonNode } from "../to-json"; |
6 | 7 |
|
7 | 8 | beforeEach(() => { |
8 | 9 | global.IS_REACT_ACT_ENVIRONMENT = true; |
@@ -97,3 +98,149 @@ test("onCaughtError is called when error is caught by Error Boundary", async () |
97 | 98 | expect(onCaughtError.mock.calls[0]?.[0]).toBeInstanceOf(Error); |
98 | 99 | expect((onCaughtError.mock.calls[0]?.[0] as Error).message).toBe("Test caught error"); |
99 | 100 | }); |
| 101 | + |
| 102 | +test("transformHiddenInstanceProps keeps hidden instances in children and JSON output", async () => { |
| 103 | + let resolvePromise: (value: string) => void; |
| 104 | + const pendingPromise = new Promise<string>((resolve) => { |
| 105 | + resolvePromise = resolve; |
| 106 | + }); |
| 107 | + |
| 108 | + function AsyncContent({ valuePromise }: { valuePromise: Promise<string> }) { |
| 109 | + const value = use(valuePromise); |
| 110 | + return ( |
| 111 | + <div data-testid="content" style={{ opacity: 1 }}> |
| 112 | + Content: {value} |
| 113 | + </div> |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + const renderer = createRoot({ |
| 118 | + transformHiddenInstanceProps: ({ props, type }) => ({ |
| 119 | + ...props, |
| 120 | + style: withHiddenStyle(props.style), |
| 121 | + "data-hidden-instance-type": type, |
| 122 | + }), |
| 123 | + }); |
| 124 | + |
| 125 | + await renderWithAct( |
| 126 | + renderer, |
| 127 | + <Suspense fallback={<div data-testid="fallback">Loading...</div>}> |
| 128 | + <AsyncContent valuePromise={Promise.resolve("Visible")} /> |
| 129 | + </Suspense>, |
| 130 | + ); |
| 131 | + |
| 132 | + await renderWithAct( |
| 133 | + renderer, |
| 134 | + <Suspense fallback={<div data-testid="fallback">Loading...</div>}> |
| 135 | + <AsyncContent valuePromise={pendingPromise} /> |
| 136 | + </Suspense>, |
| 137 | + ); |
| 138 | + |
| 139 | + const contentInstances = renderer.container.queryAll( |
| 140 | + (instance) => instance.props["data-testid"] === "content", |
| 141 | + ); |
| 142 | + expect(contentInstances).toHaveLength(1); |
| 143 | + expect(contentInstances[0]?.props.style).toEqual([{ opacity: 1 }, { display: "none" }]); |
| 144 | + expect(contentInstances[0]?.props["data-hidden-instance-type"]).toBe("div"); |
| 145 | + |
| 146 | + const fallbackInstances = renderer.container.queryAll( |
| 147 | + (instance) => instance.props["data-testid"] === "fallback", |
| 148 | + ); |
| 149 | + expect(fallbackInstances).toHaveLength(1); |
| 150 | + |
| 151 | + const json = renderer.container.toJSON(); |
| 152 | + expect(json).not.toBeNull(); |
| 153 | + expect(findJsonByTestId(json!, "content")).not.toBeNull(); |
| 154 | + expect(findJsonByTestId(json!, "fallback")).not.toBeNull(); |
| 155 | + |
| 156 | + await act(() => { |
| 157 | + resolvePromise!("Done"); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
| 161 | +test("transformHiddenInstanceProps restores original props when instance becomes visible", async () => { |
| 162 | + let resolvePromise: (value: string) => void; |
| 163 | + const pendingPromise = new Promise<string>((resolve) => { |
| 164 | + resolvePromise = resolve; |
| 165 | + }); |
| 166 | + |
| 167 | + function AsyncContent({ valuePromise }: { valuePromise: Promise<string> }) { |
| 168 | + const value = use(valuePromise); |
| 169 | + return ( |
| 170 | + <div data-testid="content" style={{ opacity: 1 }}> |
| 171 | + Content: {value} |
| 172 | + </div> |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + const renderer = createRoot({ |
| 177 | + transformHiddenInstanceProps: ({ props, type }) => ({ |
| 178 | + ...props, |
| 179 | + style: withHiddenStyle(props.style), |
| 180 | + "data-hidden-instance-type": type, |
| 181 | + }), |
| 182 | + }); |
| 183 | + |
| 184 | + await renderWithAct( |
| 185 | + renderer, |
| 186 | + <Suspense fallback={<div>Loading...</div>}> |
| 187 | + <AsyncContent valuePromise={Promise.resolve("Visible")} /> |
| 188 | + </Suspense>, |
| 189 | + ); |
| 190 | + |
| 191 | + await renderWithAct( |
| 192 | + renderer, |
| 193 | + <Suspense fallback={<div>Loading...</div>}> |
| 194 | + <AsyncContent valuePromise={pendingPromise} /> |
| 195 | + </Suspense>, |
| 196 | + ); |
| 197 | + |
| 198 | + const hiddenContent = renderer.container.queryAll( |
| 199 | + (instance) => instance.props["data-testid"] === "content", |
| 200 | + ); |
| 201 | + expect(hiddenContent).toHaveLength(1); |
| 202 | + expect(hiddenContent[0]?.props.style).toEqual([{ opacity: 1 }, { display: "none" }]); |
| 203 | + expect(hiddenContent[0]?.props["data-hidden-instance-type"]).toBe("div"); |
| 204 | + |
| 205 | + await act(() => { |
| 206 | + resolvePromise!("Done"); |
| 207 | + }); |
| 208 | + |
| 209 | + const visibleContent = renderer.container.queryAll( |
| 210 | + (instance) => instance.props["data-testid"] === "content", |
| 211 | + ); |
| 212 | + expect(visibleContent).toHaveLength(1); |
| 213 | + expect(visibleContent[0]?.props.style).toEqual({ opacity: 1 }); |
| 214 | + expect(visibleContent[0]?.props["data-hidden-instance-type"]).toBeUndefined(); |
| 215 | +}); |
| 216 | + |
| 217 | +function withHiddenStyle(style: unknown): unknown[] { |
| 218 | + if (Array.isArray(style)) { |
| 219 | + return [...style, { display: "none" }]; |
| 220 | + } |
| 221 | + |
| 222 | + if (style == null) { |
| 223 | + return [{ display: "none" }]; |
| 224 | + } |
| 225 | + |
| 226 | + return [style, { display: "none" }]; |
| 227 | +} |
| 228 | + |
| 229 | +function findJsonByTestId(node: JsonNode, testId: string): JsonElement | null { |
| 230 | + if (typeof node === "string") { |
| 231 | + return null; |
| 232 | + } |
| 233 | + |
| 234 | + if (node.props["data-testid"] === testId) { |
| 235 | + return node; |
| 236 | + } |
| 237 | + |
| 238 | + for (const child of node.children) { |
| 239 | + const match = findJsonByTestId(child, testId); |
| 240 | + if (match != null) { |
| 241 | + return match; |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + return null; |
| 246 | +} |
0 commit comments