|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | import { describe, expect, test, vi, afterEach } from "vitest"; |
7 | | -import { createMemo, Errored, Loading, flush } from "solid-js"; |
| 7 | +import { createMemo, createOptimisticStore, createStore, Errored, Loading, flush } from "solid-js"; |
8 | 8 | import { render } from "../src/index.js"; |
9 | 9 |
|
10 | 10 | describe("Dev-mode async warning", () => { |
@@ -301,3 +301,132 @@ describe("Deferred root mount", () => { |
301 | 301 | warnSpy.mockRestore(); |
302 | 302 | }); |
303 | 303 | }); |
| 304 | + |
| 305 | +// Top-level async STORES follow the same designed contract as memo-over- |
| 306 | +// promise above: no boundary means the root mount is held until the first |
| 307 | +// truth lands (static siblings included — the reveal is atomic) and the |
| 308 | +// Loading-boundary dev warning fires. Deliberate design, not an accident of |
| 309 | +// the store machinery — these pin the store-shaped paths (an optimistic |
| 310 | +// store's first flight rides pending propagation only; its flight |
| 311 | +// transaction declares nothing while uninitialized, #3264) so a future |
| 312 | +// "fix" to pending-at-root can't silently trade it away. |
| 313 | +describe("Deferred root mount — async stores", () => { |
| 314 | + let divs: HTMLDivElement[] = []; |
| 315 | + const disposers: Array<() => void> = []; |
| 316 | + |
| 317 | + afterEach(() => { |
| 318 | + disposers.splice(0).forEach(d => d()); |
| 319 | + divs.splice(0).forEach(d => d.remove()); |
| 320 | + }); |
| 321 | + |
| 322 | + function makeMount(): HTMLDivElement { |
| 323 | + const el = document.createElement("div"); |
| 324 | + document.body.appendChild(el); |
| 325 | + divs.push(el); |
| 326 | + return el; |
| 327 | + } |
| 328 | + |
| 329 | + const warned = (spy: ReturnType<typeof vi.spyOn>) => |
| 330 | + spy.mock.calls.some(args => |
| 331 | + args.some(a => typeof a === "string" && a.includes("Loading boundary")) |
| 332 | + ); |
| 333 | + |
| 334 | + test("optimistic store first flight: root mount held, warning fired, atomic reveal", async () => { |
| 335 | + const el = makeMount(); |
| 336 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 337 | + let resolveFn!: (rows: { name: string }[]) => void; |
| 338 | + |
| 339 | + disposers.push( |
| 340 | + render(() => { |
| 341 | + const [rows] = createOptimisticStore<{ name: string }[]>( |
| 342 | + () => new Promise<{ name: string }[]>(r => (resolveFn = r)), |
| 343 | + [] |
| 344 | + ); |
| 345 | + return ( |
| 346 | + <div class="outer"> |
| 347 | + <span class="sibling">static</span> |
| 348 | + <span class="row">{rows[0]?.name ?? "none"}</span> |
| 349 | + </div> |
| 350 | + ); |
| 351 | + }, el) |
| 352 | + ); |
| 353 | + flush(); |
| 354 | + |
| 355 | + // Held: nothing mounts, not even the static sibling. |
| 356 | + expect(el.innerHTML).toBe(""); |
| 357 | + // Warned: uncaught pending at the root is diagnosed. |
| 358 | + expect(warned(warnSpy)).toBe(true); |
| 359 | + |
| 360 | + resolveFn([{ name: "first-truth" }]); |
| 361 | + await Promise.resolve(); |
| 362 | + await Promise.resolve(); |
| 363 | + flush(); |
| 364 | + |
| 365 | + const html = el.innerHTML; |
| 366 | + expect(html).toContain("static"); |
| 367 | + expect(html).toContain("first-truth"); |
| 368 | + warnSpy.mockRestore(); |
| 369 | + }); |
| 370 | + |
| 371 | + test("derived store first flight: root mount held, warning fired, atomic reveal", async () => { |
| 372 | + const el = makeMount(); |
| 373 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 374 | + let resolveFn!: (rows: { name: string }[]) => void; |
| 375 | + |
| 376 | + disposers.push( |
| 377 | + render(() => { |
| 378 | + const [rows] = createStore<{ name: string }[]>( |
| 379 | + () => new Promise<{ name: string }[]>(r => (resolveFn = r)), |
| 380 | + [] |
| 381 | + ); |
| 382 | + return ( |
| 383 | + <div class="outer"> |
| 384 | + <span class="sibling">static</span> |
| 385 | + <span class="row">{rows[0]?.name ?? "none"}</span> |
| 386 | + </div> |
| 387 | + ); |
| 388 | + }, el) |
| 389 | + ); |
| 390 | + flush(); |
| 391 | + |
| 392 | + expect(el.innerHTML).toBe(""); |
| 393 | + expect(warned(warnSpy)).toBe(true); |
| 394 | + |
| 395 | + resolveFn([{ name: "first-truth" }]); |
| 396 | + await Promise.resolve(); |
| 397 | + await Promise.resolve(); |
| 398 | + flush(); |
| 399 | + |
| 400 | + const html = el.innerHTML; |
| 401 | + expect(html).toContain("static"); |
| 402 | + expect(html).toContain("first-truth"); |
| 403 | + warnSpy.mockRestore(); |
| 404 | + }); |
| 405 | + |
| 406 | + test("optimistic store first flight inside Loading: fallback shows, no warning", () => { |
| 407 | + const el = makeMount(); |
| 408 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 409 | + |
| 410 | + disposers.push( |
| 411 | + render(() => { |
| 412 | + const [rows] = createOptimisticStore<{ name: string }[]>( |
| 413 | + () => new Promise<{ name: string }[]>(() => {}), |
| 414 | + [] |
| 415 | + ); |
| 416 | + return ( |
| 417 | + <div class="outer"> |
| 418 | + <Loading fallback={<span class="fallback">wait</span>}> |
| 419 | + <span class="row">{rows[0]?.name ?? "none"}</span> |
| 420 | + </Loading> |
| 421 | + </div> |
| 422 | + ); |
| 423 | + }, el) |
| 424 | + ); |
| 425 | + flush(); |
| 426 | + |
| 427 | + expect(el.innerHTML).toContain("fallback"); |
| 428 | + expect(el.innerHTML).toContain("outer"); |
| 429 | + expect(warned(warnSpy)).toBe(false); |
| 430 | + warnSpy.mockRestore(); |
| 431 | + }); |
| 432 | +}); |
0 commit comments