ruact 0.0.16 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -1
  3. data/lib/ruact/client_manifest.rb +11 -2
  4. data/lib/ruact/controller.rb +3 -1
  5. data/lib/ruact/doctor.rb +64 -4
  6. data/lib/ruact/flight/renderer.rb +23 -9
  7. data/lib/ruact/flight/request.rb +31 -2
  8. data/lib/ruact/flight/row_emitter.rb +9 -0
  9. data/lib/ruact/flight/serializer.rb +22 -7
  10. data/lib/ruact/render_pipeline.rb +10 -3
  11. data/lib/ruact/testing/component_query.rb +11 -4
  12. data/lib/ruact/testing/flight_wire_parser.rb +2 -2
  13. data/lib/ruact/version.rb +1 -1
  14. data/vendor/javascript/vite-plugin-ruact/bootstrap.test.mjs +44 -2
  15. data/vendor/javascript/vite-plugin-ruact/flight-conformance.test.mjs +286 -0
  16. data/vendor/javascript/vite-plugin-ruact/index.js +89 -14
  17. data/vendor/javascript/vite-plugin-ruact/package-lock.json +804 -3
  18. data/vendor/javascript/vite-plugin-ruact/package.json +5 -1
  19. data/vendor/javascript/vite-plugin-ruact/ruact-router.test.mjs +53 -0
  20. data/vendor/javascript/vite-plugin-ruact/runtime/bootstrap.jsx +46 -17
  21. data/vendor/javascript/vite-plugin-ruact/runtime/flight-modules.js +40 -0
  22. data/vendor/javascript/vite-plugin-ruact/runtime/flight-webpack-globals.js +15 -0
  23. data/vendor/javascript/vite-plugin-ruact/runtime/root-boundary.js +41 -0
  24. data/vendor/javascript/vite-plugin-ruact/runtime/ruact-router.js +65 -83
  25. data/vendor/javascript/vite-plugin-ruact/runtime/suspense-boundary.js +83 -0
  26. data/vendor/javascript/vite-plugin-ruact/runtime/transport.js +41 -0
  27. data/vendor/javascript/vite-plugin-ruact/runtime/vendor/react-server-dom-webpack/LICENSE +21 -0
  28. data/vendor/javascript/vite-plugin-ruact/runtime/vendor/react-server-dom-webpack/VERSION.json +5 -0
  29. data/vendor/javascript/vite-plugin-ruact/runtime/vendor/react-server-dom-webpack/client.browser.development.js +5473 -0
  30. data/vendor/javascript/vite-plugin-ruact/runtime/vendor/react-server-dom-webpack/client.browser.production.js +2126 -0
  31. data/vendor/javascript/vite-plugin-ruact/scripts/vendor-flight-client.mjs +124 -0
  32. data/vendor/javascript/vite-plugin-ruact/vitest.config.mjs +14 -0
  33. metadata +13 -4
  34. data/vendor/javascript/vite-plugin-ruact/flight-client.test.mjs +0 -499
  35. data/vendor/javascript/vite-plugin-ruact/runtime/flight-client.js +0 -436
@@ -1,499 +0,0 @@
1
- // Story 17.0a (issue #62) — the Flight client must not collapse arrays.
2
- //
3
- // `flight-client.js` was the only runtime module in this package without a unit
4
- // test, and the defect this file pins is what that gap allowed: `_buildTree`
5
- // applied a single-element unwrap (`items.length === 1 ? items[0] : items`) to
6
- // EVERY array it walked. `_buildTree` is a generic value walker with no
7
- // positional information, so it could not tell a `children` array from a data
8
- // array sitting in a prop — a list prop holding exactly one row arrived at the
9
- // client component as the row itself.
10
- //
11
- // What this file covers:
12
- //
13
- // 1. Data props — 0 / 1 / 2 elements, at top level and nested, arrive with
14
- // their arity intact. The n=1 cases are the regression; n=0 and n=2 are
15
- // guards that the fix does not over-correct in the other direction.
16
- // 2. Children — 0 / 1 / 2, asserting the REBUILT content (type, key, nested
17
- // children), not just arity, so a rebuild returning nulls cannot pass.
18
- // Children keep their arity too: the server collapses a lone TAG-NESTED
19
- // child before the wire (`html_converter.rb:188`, Suspense at `:202`), but
20
- // an explicit `children={[...]}` prop reaches the client as an array and
21
- // now stays one. See the contract note on that describe block.
22
- // 3. The gem-produced fixture — the same assertion driven by wire bytes the
23
- // Ruby serializer wrote, not by a literal typed here.
24
- //
25
- // Isolation: `flight-client.js` keeps `pendingChunks` and `lazyCache` as
26
- // module-level Maps that survive between tests in one process. `lazyCache` is
27
- // memoized by rowId, so two cases reusing an id would leak into each other.
28
- // `clearPendingChunks()` in `beforeEach` is what keeps this file order-
29
- // independent. Vitest is not configured to shuffle, so nothing here depends on
30
- // that guard today — it is insurance against a case that reuses a row id, and
31
- // against shuffling being turned on later.
32
-
33
- import { describe, it, expect, beforeEach } from "vitest";
34
- import { Fragment } from "react";
35
- import fs from "node:fs";
36
- import path from "node:path";
37
- import {
38
- buildTree,
39
- createFromFlightPayload,
40
- createRowParser,
41
- clearPendingChunks,
42
- } from "./runtime/flight-client.js";
43
-
44
- // A stand-in client component. Never rendered — these tests assert the props
45
- // the client BUILDS, which is the contract the component then consumes.
46
- const TaskList = () => null;
47
- const PostList = () => null; // the fixture's component — see the last describe
48
- const MODULE_REGISTRY = {
49
- "/TaskList.jsx": { TaskList },
50
- "/PostList.jsx": { PostList },
51
- };
52
-
53
- const EMPTY_ROWS = new Map();
54
-
55
- /** Wire payload: one import row + a root element carrying `props`. */
56
- function payloadWithProps(props) {
57
- return [
58
- '1:I["/TaskList.jsx","TaskList"]',
59
- `0:["$","$L1",null,${JSON.stringify(props)}]`,
60
- "",
61
- ].join("\n");
62
- }
63
-
64
- beforeEach(() => {
65
- clearPendingChunks();
66
- });
67
-
68
- describe("Story 17.0a — data props keep their arity", () => {
69
- it("a prop holding ONE item arrives as a one-element array", () => {
70
- const tree = createFromFlightPayload(
71
- payloadWithProps({ tasks: [{ id: 6, title: "Buy coffee" }] }),
72
- MODULE_REGISTRY,
73
- );
74
-
75
- expect(Array.isArray(tree.props.tasks)).toBe(true);
76
- expect(tree.props.tasks).toEqual([{ id: 6, title: "Buy coffee" }]);
77
- });
78
-
79
- it("a prop holding TWO items is unchanged", () => {
80
- const tree = createFromFlightPayload(
81
- payloadWithProps({ tasks: [{ id: 6 }, { id: 7 }] }),
82
- MODULE_REGISTRY,
83
- );
84
-
85
- expect(tree.props.tasks).toEqual([{ id: 6 }, { id: 7 }]);
86
- });
87
-
88
- it("a prop holding an EMPTY array stays an empty array", () => {
89
- const tree = createFromFlightPayload(
90
- payloadWithProps({ tasks: [] }),
91
- MODULE_REGISTRY,
92
- );
93
-
94
- expect(Array.isArray(tree.props.tasks)).toBe(true);
95
- expect(tree.props.tasks).toEqual([]);
96
- });
97
-
98
- it("a one-element array NESTED inside an object prop keeps its arity", () => {
99
- const tree = createFromFlightPayload(
100
- payloadWithProps({ page: { rows: [{ id: 6 }], total: 1 } }),
101
- MODULE_REGISTRY,
102
- );
103
-
104
- expect(Array.isArray(tree.props.page.rows)).toBe(true);
105
- expect(tree.props.page.rows).toEqual([{ id: 6 }]);
106
- });
107
-
108
- it("a one-element array of PRIMITIVES keeps its arity", () => {
109
- const tree = createFromFlightPayload(
110
- payloadWithProps({ tags: ["$$ruby"] }),
111
- MODULE_REGISTRY,
112
- );
113
-
114
- // Decoded, not merely carried: "$$ruby" on the wire is "$ruby" in the prop.
115
- expect(tree.props.tags).toEqual(["$ruby"]);
116
- });
117
-
118
- it("the scaffold's own prop shape survives (issue #62 reproduction)", () => {
119
- // `<PostList posts={rows} />` with exactly one record. The generated
120
- // scaffold gates its table on `sortedRows.length > 0`, so an object here
121
- // renders neither the table nor the empty state — a blank list, no error.
122
- // The throw needs a specific sequence, because the sort controls live
123
- // inside the table that did not render: search (which works, since results
124
- // arrive as JSON), sort, then clear the search — that puts the object back
125
- // through `[...rows]`.
126
- const tree = createFromFlightPayload(
127
- payloadWithProps({ posts: [{ id: 1, title: "First post" }] }),
128
- MODULE_REGISTRY,
129
- );
130
-
131
- // The component itself, not just its props — a rebuild that resolved the
132
- // `$L` reference to a plain "div" would keep every prop assertion green
133
- // while never running PostList at all.
134
- expect(tree.type).toBe(TaskList);
135
-
136
- const { posts } = tree.props;
137
- expect(Array.isArray(posts)).toBe(true);
138
- expect(posts).toHaveLength(1);
139
- expect(() => [...posts]).not.toThrow();
140
- });
141
-
142
- it("an array INSIDE an array keeps both dimensions", () => {
143
- // `.flat()` in the walker would collapse this and still satisfy every
144
- // one-dimensional assertion above.
145
- const tree = createFromFlightPayload(
146
- payloadWithProps({ grid: [[1, 2], [3]] }),
147
- MODULE_REGISTRY,
148
- );
149
-
150
- expect(tree.props.grid).toEqual([[1, 2], [3]]);
151
- });
152
-
153
- it("a ONE-element outer array wrapping an array keeps both levels", () => {
154
- // Distinct from the grid case above, whose outer array has two members: a
155
- // collapse conditioned on `length === 1 && Array.isArray(items[0])` would
156
- // pass that one and corrupt this one.
157
- const tree = createFromFlightPayload(
158
- payloadWithProps({ matrix: [[1, 2]] }),
159
- MODULE_REGISTRY,
160
- );
161
-
162
- expect(tree.props.matrix).toEqual([[1, 2]]);
163
- });
164
-
165
- it("an `undefined` MEMBER is kept — Flight distinguishes it from absence", () => {
166
- // `$undefined` is a wire sentinel (project-context §7), so `[undefined]` is
167
- // a one-member array, not an empty one. A `.filter(v => v !== undefined)`
168
- // in the walker would silently make it empty.
169
- const tree = createFromFlightPayload(
170
- payloadWithProps({ slots: ["$undefined"] }),
171
- MODULE_REGISTRY,
172
- );
173
-
174
- expect(tree.props.slots).toHaveLength(1);
175
- expect(tree.props.slots[0]).toBeUndefined();
176
- });
177
-
178
- it("falsy items survive — they are values, not absences", () => {
179
- // `.filter(Boolean)` in the walker would drop these and still satisfy
180
- // every length assertion that uses truthy items.
181
- const tree = createFromFlightPayload(
182
- payloadWithProps({ flags: [false, 0, null, "", 1] }),
183
- MODULE_REGISTRY,
184
- );
185
-
186
- expect(tree.props.flags).toEqual([false, 0, null, "", 1]);
187
- });
188
- });
189
-
190
- describe("Story 17.0a — children keep their arity too (contract, decided 2026-09-09)", () => {
191
- // Decision: arity is preserved for `children` as well as for data props.
192
- //
193
- // The server collapses a lone child produced by TAG NESTING before the wire
194
- // (`html_converter.rb:188`, Suspense at `:202`), so that path is unaffected.
195
- // But `children` passed as an EXPLICIT PROP on a self-closing tag —
196
- // `<Label children={["hi"]} />` — reaches the wire as an array, and Story
197
- // 15.2's loud error does not cover it (`erb_preprocessor.rb:174` exempts
198
- // self-closing tags). Before this story the client collapsed that to `"hi"`.
199
- // It no longer does: you passed an array, you get an array.
200
- //
201
- // Taken deliberately while the library has no external adopters — the only
202
- // moment where making the contract consistent costs nothing.
203
-
204
- it("a single child NESTED IN A TAG arrives collapsed, because the server collapsed it", () => {
205
- const tree = buildTree(
206
- ["$", "div", null, { children: ["$", "em", null, { children: "hi" }] }],
207
- EMPTY_ROWS,
208
- MODULE_REGISTRY,
209
- );
210
-
211
- // Not an array: the server sent the element itself, and it rebuilds as one.
212
- expect(Array.isArray(tree.props.children)).toBe(false);
213
- expect(tree.props.children.type).toBe("em");
214
- expect(tree.props.children.props.children).toBe("hi");
215
- });
216
-
217
- it("a single TEXT child nested in a tag arrives as the string", () => {
218
- const tree = buildTree(
219
- ["$", "div", null, { children: "hi" }],
220
- EMPTY_ROWS,
221
- MODULE_REGISTRY,
222
- );
223
-
224
- expect(tree.props.children).toBe("hi");
225
- });
226
-
227
- it("multiple children arrive as an array, each one rebuilt", () => {
228
- const tree = buildTree(
229
- ["$", "ul", null, {
230
- children: [
231
- ["$", "li", "a", { children: "one" }],
232
- ["$", "li", "b", { children: "two" }],
233
- ],
234
- }],
235
- EMPTY_ROWS,
236
- MODULE_REGISTRY,
237
- );
238
-
239
- const kids = tree.props.children;
240
- expect(Array.isArray(kids)).toBe(true);
241
- expect(kids).toHaveLength(2);
242
- // Content, not just arity — a rebuild that returned nulls would pass a
243
- // length check and fail this one.
244
- expect(kids.map((k) => k.type)).toEqual(["li", "li"]);
245
- expect(kids.map((k) => k.key)).toEqual(["a", "b"]);
246
- expect(kids.map((k) => k.props.children)).toEqual(["one", "two"]);
247
- });
248
-
249
- it("an EXPLICIT one-element children prop stays a one-element array", () => {
250
- // The contract change. `<Label children={["hi"]} />` — verified against the
251
- // Ruby pipeline to reach the wire as `{"children":["hi"]}`.
252
- const tree = buildTree(
253
- ["$", "span", null, { children: ["hi"] }],
254
- EMPTY_ROWS,
255
- MODULE_REGISTRY,
256
- );
257
-
258
- expect(tree.props.children).toEqual(["hi"]);
259
- });
260
-
261
- it("a one-element children array of ELEMENTS stays an array, rebuilt", () => {
262
- const tree = buildTree(
263
- ["$", "ul", null, { children: [["$", "li", "only", { children: "just one" }]] }],
264
- EMPTY_ROWS,
265
- MODULE_REGISTRY,
266
- );
267
-
268
- const kids = tree.props.children;
269
- expect(Array.isArray(kids)).toBe(true);
270
- expect(kids).toHaveLength(1);
271
- expect(kids[0].type).toBe("li");
272
- expect(kids[0].key).toBe("only");
273
- expect(kids[0].props.children).toBe("just one");
274
- });
275
-
276
- it("an EMPTY children array stays an empty array", () => {
277
- // AC3's n=0 for children. Distinct from "no children prop at all" below:
278
- // this one is present and empty, and must not become visible content.
279
- const tree = buildTree(
280
- ["$", "ul", null, { children: [] }],
281
- EMPTY_ROWS,
282
- MODULE_REGISTRY,
283
- );
284
-
285
- expect(Array.isArray(tree.props.children)).toBe(true);
286
- expect(tree.props.children).toHaveLength(0);
287
- });
288
-
289
- it("an element with no children prop has none after rebuilding", () => {
290
- const tree = buildTree(["$", "br", null, {}], EMPTY_ROWS, MODULE_REGISTRY);
291
-
292
- expect("children" in tree.props).toBe(false);
293
- });
294
- });
295
-
296
- describe("Story 17.0a — the contract, driven by a gem-produced fixture", () => {
297
- // The bytes were written by the Ruby serializer, not typed here.
298
- //
299
- // The two sides guarantee DIFFERENT things, and neither reddens for the
300
- // other: `spec/ruact/flight/serializer_spec.rb` ("single-element array prop
301
- // survives the wire (Story 17.0a)") asserts that what the serializer emits
302
- // still equals this file, so a server-side change reddens THERE; this test
303
- // asserts that these bytes rebuild into a one-element array, so a client-side
304
- // change reddens HERE. What the pair buys is that the client is exercised
305
- // against real server output rather than a literal someone typed — the
306
- // Story 5.2 pattern. It does not buy simultaneous failure.
307
- const FIXTURE = path.join(
308
- import.meta.dirname,
309
- "../../../spec/fixtures/flight/single_element_array_prop.txt",
310
- );
311
-
312
- it("the fixture the gem wrote rebuilds as a one-element array", () => {
313
- const payload = fs.readFileSync(FIXTURE, "utf8");
314
- const tree = createFromFlightPayload(payload, MODULE_REGISTRY);
315
-
316
- expect(tree.type).toBe(PostList);
317
- expect(Array.isArray(tree.props.posts)).toBe(true);
318
- // The title is "$$5 plan" ON THE WIRE (§7 prepends one "$"). Asserting the
319
- // DECODED value means a walker that preserved arity but stopped rebuilding
320
- // scalar members would fail here rather than pass.
321
- expect(tree.props.posts).toEqual([{ id: 1, title: "$5 plan" }]);
322
- });
323
- });
324
-
325
- // Story 17-0d — every string of 1024+ bytes travels as a `T` row framed by its
326
- // BYTE length, with no trailing newline. The decoder split on "\n" and so lost
327
- // the root row: one long post body blanked the whole page. The fixtures are
328
- // what the Ruby serializer emits (spec/ruact/flight/text_framing_fixtures_spec.rb
329
- // writes and guards them).
330
- describe("text rows (Story 17-0d)", () => {
331
- const FIXTURES = path.join(import.meta.dirname, "../../../spec/fixtures/flight");
332
- const read = (name) => fs.readFileSync(path.join(FIXTURES, name));
333
- const expected = JSON.parse(read("text_framing_expected.json").toString("utf8"));
334
-
335
- const rowsOf = (chunks) => {
336
- const rows = new Map();
337
- const parser = createRowParser(({ id, row }) => rows.set(id, row));
338
- for (const chunk of chunks) parser.push(chunk);
339
- parser.end();
340
- return rows;
341
- };
342
-
343
- it("decodes every value the serializer framed, byte for byte", () => {
344
- const tree = createFromFlightPayload(read("text_framing.txt").toString("utf8"), MODULE_REGISTRY);
345
- expect(tree).toEqual(expected);
346
- });
347
-
348
- it("puts long text inside Suspense content where the deferred row points", () => {
349
- const tree = createFromFlightPayload(read("text_framing_suspense.txt").toString("utf8"), MODULE_REGISTRY);
350
- const deferred = tree.props.children.type; // the already-arrived deferred row, wrapped
351
- expect(deferred().props.children).toBe(expected.multibyte);
352
- });
353
-
354
- it("does not depend on where the network splits the bytes", () => {
355
- const bytes = new Uint8Array(read("text_framing.txt"));
356
- const whole = rowsOf([bytes]);
357
-
358
- // Every single split point: inside ids, `T` lengths, text bodies and
359
- // multibyte code points, and between rows.
360
- for (let cut = 1; cut < bytes.length; cut += 1) {
361
- const split = rowsOf([bytes.subarray(0, cut), bytes.subarray(cut)]);
362
- expect(split).toEqual(whole);
363
- }
364
-
365
- // And one byte at a time.
366
- expect(rowsOf([...bytes].map((b) => Uint8Array.of(b)))).toEqual(whole);
367
- });
368
-
369
- it("refuses a truncated text row instead of rendering part of the page", () => {
370
- const bytes = new Uint8Array(read("text_framing.txt"));
371
- const firstText = Buffer.from(bytes).indexOf(":T") + 10;
372
- expect(() => rowsOf([bytes.subarray(0, firstText)])).toThrow(/Truncated Flight payload/);
373
- });
374
-
375
- it("refuses a malformed text length", () => {
376
- const enc = new TextEncoder();
377
- expect(() => rowsOf([enc.encode("1:Tzz,abc0:\"$T1\"\n")])).toThrow(/Malformed text row length/);
378
- });
379
-
380
- it("names a text reference whose row never arrived", () => {
381
- expect(() => createFromFlightPayload('0:{"body":"$T7"}\n', MODULE_REGISTRY))
382
- .toThrow(/Text row 7 is referenced but was never received/);
383
- });
384
-
385
- it("skips a stray line without a colon instead of losing the row after it", () => {
386
- const rows = rowsOf([new TextEncoder().encode('garbage\n0:{"a":1}\n')]);
387
- expect(rows.get(0)).toEqual({ kind: "model", value: { a: 1 } });
388
- });
389
-
390
- it("reads a large row arriving in small chunks in linear time", () => {
391
- const big = JSON.stringify({ items: Array.from({ length: 60000 }, (_, i) => ({ id: i, name: `item ${i}` })) });
392
- const bytes = new TextEncoder().encode(`0:${big}\n`);
393
- const chunks = [];
394
- for (let i = 0; i < bytes.length; i += 4096) chunks.push(bytes.subarray(i, i + 4096));
395
- const started = performance.now();
396
- const rows = rowsOf(chunks);
397
- expect(rows.get(0).value.items).toHaveLength(60000);
398
- // ~2.3 MB in 4 KB chunks; quadratic re-copying took seconds.
399
- expect(performance.now() - started).toBeLessThan(1000);
400
- });
401
-
402
- it("still reads a last row that lost its trailing newline", () => {
403
- const rows = rowsOf([new TextEncoder().encode('0:{"a":1}')]);
404
- expect(rows.get(0)).toEqual({ kind: "model", value: { a: 1 } });
405
- });
406
- });
407
-
408
- // Story 17-0c — the serializer emits Ruby Time/DateTime as "$D<ISO>" and
409
- // integers beyond ±(2^53 − 1) as "$n<decimal>". Components got the marker
410
- // strings. The fixture is the Ruby serializer's own output
411
- // (spec/ruact/flight/scalar_fixtures_spec.rb writes and guards it).
412
- describe("dates and big integers (Story 17-0c)", () => {
413
- const FIXTURES = path.join(import.meta.dirname, "../../../spec/fixtures/flight");
414
- const payload = fs.readFileSync(path.join(FIXTURES, "scalar_round_trip.txt"), "utf8");
415
- const expected = JSON.parse(fs.readFileSync(path.join(FIXTURES, "scalar_round_trip_expected.json"), "utf8"));
416
-
417
- // Expected JSON tags what JSON cannot hold: { date: ISO } and { bigint: "…" }.
418
- const revive = (value) => {
419
- if (Array.isArray(value)) return value.map(revive);
420
- if (value && typeof value === "object") {
421
- if ("date" in value) return new Date(value.date);
422
- if ("bigint" in value) return BigInt(value.bigint);
423
- return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, revive(v)]));
424
- }
425
- return value;
426
- };
427
-
428
- it("rebuilds every value with its JS type, nested ones included", () => {
429
- const tree = createFromFlightPayload(payload, MODULE_REGISTRY);
430
- expect(tree).toEqual(revive(expected));
431
- });
432
-
433
- it("gives a Date the same instant, offset applied, to the millisecond", () => {
434
- const tree = createFromFlightPayload(payload, MODULE_REGISTRY);
435
- expect(tree.time).toBeInstanceOf(Date);
436
- expect(tree.time.toISOString()).toBe("2026-09-08T12:30:45.123Z");
437
- expect(tree.zoned.toISOString()).toBe("2026-09-08T12:30:00.000Z");
438
- });
439
-
440
- it("keeps safe integers as numbers and only the unsafe ones as BigInt", () => {
441
- const tree = createFromFlightPayload(payload, MODULE_REGISTRY);
442
- expect(typeof tree.max_safe).toBe("number");
443
- expect(typeof tree.min_safe).toBe("number");
444
- expect(typeof tree.big).toBe("bigint");
445
- expect(tree.big).toBe(9007199254740993n);
446
- expect(tree.negative_big).toBe(-18446744073709551616n);
447
- });
448
-
449
- it("leaves a literal string that starts with $D a string", () => {
450
- const tree = createFromFlightPayload(payload, MODULE_REGISTRY);
451
- expect(tree.literal).toBe("$D2026-01-01 is a string");
452
- });
453
- });
454
-
455
- // A server-rendered tree has unkeyed siblings (an <h1> beside a component, the
456
- // rows of an ERB loop). Handed to React as an array they made it log "Each
457
- // child in a list should have a unique key" on the Getting Started page. They
458
- // are passed as separate children instead — static, like JSX — and no key is
459
- // invented (an invented index key can collide with a template's own key and
460
- // duplicate a row, or reset state when a sibling comes or goes).
461
- describe("sibling children", () => {
462
- const payload = [
463
- '1:I["/TaskList.jsx","TaskList"]',
464
- '0:[["$","h1",null,{"children":"Hello"}],["$","$L1",null,{"tasks":[]}],["$","ul",null,{"children":[["$","li",null,{"children":"a"}],["$","li","explicit",{"children":"b"}]]}]]',
465
- "",
466
- ].join("\n");
467
-
468
- it("wraps several root siblings in a Fragment, without inventing keys", () => {
469
- const tree = createFromFlightPayload(payload, MODULE_REGISTRY);
470
- expect(tree.type).toBe(Fragment);
471
- expect(tree.props.children.map((el) => el.key)).toEqual([null, null, null]);
472
- });
473
-
474
- it("keeps the template's own key and invents none for the others", () => {
475
- const tree = createFromFlightPayload(payload, MODULE_REGISTRY);
476
- const ul = tree.props.children[2];
477
- expect(ul.props.children.map((el) => el.key)).toEqual([null, "explicit"]);
478
- expect(ul.props.children).toHaveLength(2);
479
- });
480
-
481
- it("wraps a Suspense boundary's deferred siblings the same way", () => {
482
- const deferred = buildTree([["$", "h2", null, { children: "A" }], ["$", "p", null, { children: "B" }]], EMPTY_ROWS, MODULE_REGISTRY);
483
- expect(deferred.type).toBe(Fragment);
484
- expect(deferred.props.children).toHaveLength(2);
485
- });
486
-
487
- it("does not spread a data array passed as children (it stays mutable)", () => {
488
- const tree = createFromFlightPayload(payloadWithProps({ children: ["a", "b", "c"] }), MODULE_REGISTRY);
489
- expect(tree.props.children).toEqual(["a", "b", "c"]);
490
- expect(Object.isFrozen(tree.props.children)).toBe(false);
491
- });
492
-
493
- it("leaves a single root, data arrays and one-element children as they were", () => {
494
- const tree = createFromFlightPayload(payloadWithProps({ tasks: [{ id: 1 }, { id: 2 }] }), MODULE_REGISTRY);
495
- expect(tree.key).toBeNull();
496
- expect(tree.props.tasks).toEqual([{ id: 1 }, { id: 2 }]);
497
- expect(createFromFlightPayload('0:[1,2]\n', MODULE_REGISTRY)).toEqual([1, 2]);
498
- });
499
- });