|
| 1 | +import { expect, test, vi } from "vitest"; |
| 2 | +import { createMemo, onCleanup } from "solid-js"; |
| 3 | +import { renderToStream, ssr, escape } from "@solidjs/web"; |
| 4 | +import { watchEscapes } from "./stream-settle-harness.js"; |
| 5 | + |
| 6 | +for (const timing of ["before render", "before then", "after then"] as const) { |
| 7 | + test(`rejects thenable consumers when aborted ${timing}`, async () => { |
| 8 | + const controller = new AbortController(); |
| 9 | + const reason = { message: "request disconnected" }; |
| 10 | + const onCompleteAll = vi.fn(); |
| 11 | + if (timing === "before render") controller.abort(reason); |
| 12 | + const stream = renderToStream(() => "hello", { |
| 13 | + signal: controller.signal, |
| 14 | + onCompleteAll |
| 15 | + }); |
| 16 | + if (timing === "before then") controller.abort(reason); |
| 17 | + const results: unknown[] = []; |
| 18 | + const fulfilled = vi.fn(); |
| 19 | + stream.then(fulfilled, error => results.push(error)); |
| 20 | + stream.then(fulfilled, error => results.push(error)); |
| 21 | + if (timing === "after then") controller.abort(reason); |
| 22 | + await Promise.resolve(); |
| 23 | + expect(results).toEqual([reason, reason]); |
| 24 | + expect(results[0]).toBe(reason); |
| 25 | + expect(results[1]).toBe(reason); |
| 26 | + expect(fulfilled).not.toHaveBeenCalled(); |
| 27 | + expect(onCompleteAll).not.toHaveBeenCalled(); |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +test("rejects with the default abort reason", async () => { |
| 32 | + const controller = new AbortController(); |
| 33 | + const stream = renderToStream(() => "hello", { signal: controller.signal }); |
| 34 | + const rejected = vi.fn(); |
| 35 | + stream.then(undefined, rejected); |
| 36 | + controller.abort(); |
| 37 | + await Promise.resolve(); |
| 38 | + expect(rejected).toHaveBeenCalledExactlyOnceWith(controller.signal.reason); |
| 39 | +}); |
| 40 | + |
| 41 | +test("an abort after completion preserves the fulfilled result", async () => { |
| 42 | + const controller = new AbortController(); |
| 43 | + const onCompleteAll = vi.fn(); |
| 44 | + const stream = renderToStream(() => "hello", { signal: controller.signal, onCompleteAll }); |
| 45 | + const result = Promise.resolve(stream); |
| 46 | + expect(await result).toBe("hello"); |
| 47 | + controller.abort(); |
| 48 | + expect(await result).toBe("hello"); |
| 49 | + expect(onCompleteAll).toHaveBeenCalledTimes(1); |
| 50 | +}); |
| 51 | + |
| 52 | +test("rejects while an asynchronous render is pending", async () => { |
| 53 | + const controller = new AbortController(); |
| 54 | + const reason = new Error("client disconnected"); |
| 55 | + let resolve!: (value: string) => void; |
| 56 | + const pending = new Promise<string>(done => (resolve = done)); |
| 57 | + const onCompleteAll = vi.fn(); |
| 58 | + const stream = renderToStream( |
| 59 | + () => { |
| 60 | + const message = createMemo(() => pending); |
| 61 | + return ssr(["<p>", "</p>"], () => escape(message())); |
| 62 | + }, |
| 63 | + { signal: controller.signal, onCompleteAll } |
| 64 | + ); |
| 65 | + const rejected = vi.fn(); |
| 66 | + stream.then(undefined, rejected); |
| 67 | + controller.abort(reason); |
| 68 | + await Promise.resolve(); |
| 69 | + expect(rejected).toHaveBeenCalledExactlyOnceWith(reason); |
| 70 | + resolve("loaded"); |
| 71 | + await Promise.resolve(); |
| 72 | + expect(onCompleteAll).not.toHaveBeenCalled(); |
| 73 | +}); |
| 74 | + |
| 75 | +test("rejects both consumers with the abort reason when cleanup throws without escaping", async () => { |
| 76 | + const controller = new AbortController(); |
| 77 | + const reason = new Error("client disconnected"); |
| 78 | + const cleanupError = new Error("cleanup threw"); |
| 79 | + const onCompleteAll = vi.fn(); |
| 80 | + const onError = vi.fn(); |
| 81 | + const fulfilled = vi.fn(); |
| 82 | + const rejected = [vi.fn(), vi.fn()]; |
| 83 | + |
| 84 | + const escaped = await watchEscapes(async () => { |
| 85 | + const stream = renderToStream( |
| 86 | + () => { |
| 87 | + onCleanup(() => { |
| 88 | + throw cleanupError; |
| 89 | + }); |
| 90 | + return "hello"; |
| 91 | + }, |
| 92 | + { signal: controller.signal, onCompleteAll, onError } |
| 93 | + ); |
| 94 | + stream.then(fulfilled, rejected[0]); |
| 95 | + stream.then(fulfilled, rejected[1]); |
| 96 | + expect(() => controller.abort(reason)).not.toThrow(); |
| 97 | + await Promise.resolve(); |
| 98 | + for (const consumer of rejected) { |
| 99 | + expect(consumer).toHaveBeenCalledTimes(1); |
| 100 | + expect(consumer.mock.calls[0][0]).toBe(controller.signal.reason); |
| 101 | + expect(consumer.mock.calls[0][0]).toBe(reason); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + expect(escaped).toEqual([]); |
| 106 | + expect(fulfilled).not.toHaveBeenCalled(); |
| 107 | + expect(onCompleteAll).not.toHaveBeenCalled(); |
| 108 | + expect(onError).toHaveBeenCalledTimes(1); |
| 109 | + expect(onError.mock.calls[0][0]).toBe(cleanupError); |
| 110 | + expect(onError.mock.calls[0][1]).toMatchObject({ kind: "render", handling: "failed" }); |
| 111 | +}); |
0 commit comments