|
| 1 | +import { |
| 2 | + ErrorBoundary, |
| 3 | + ParentProps, |
| 4 | + Suspense, |
| 5 | + catchError, |
| 6 | + createRoot, |
| 7 | + createSignal |
| 8 | +} from "solid-js"; |
| 9 | +import { render } from "solid-js/web"; |
| 10 | +import { createAsync, createAsyncStore } from "../src/data"; |
| 11 | +import { awaitPromise, waitFor } from "./helpers"; |
| 12 | + |
| 13 | +function Parent(props: ParentProps) { |
| 14 | + return <ErrorBoundary fallback={<div id="parentError" />}>{props.children}</ErrorBoundary>; |
| 15 | +} |
| 16 | + |
| 17 | +async function getText(arg?: string) { |
| 18 | + return arg || "fallback"; |
| 19 | +} |
| 20 | +async function getError(arg?: any): Promise<any> { |
| 21 | + throw Error("error"); |
| 22 | +} |
| 23 | + |
| 24 | +describe("createAsync should", () => { |
| 25 | + test("return 'fallback'", () => { |
| 26 | + createRoot(() => { |
| 27 | + const data = createAsync(() => getText()); |
| 28 | + setTimeout(() => expect(data()).toBe("fallback"), 1); |
| 29 | + }); |
| 30 | + }); |
| 31 | + test("return 'text'", () => { |
| 32 | + createRoot(() => { |
| 33 | + const data = createAsync(() => getText("text")); |
| 34 | + setTimeout(() => expect(data()).toBe("text"), 1); |
| 35 | + }); |
| 36 | + }); |
| 37 | + test("initial error to be caught ", () => { |
| 38 | + createRoot(() => { |
| 39 | + const data = createAsync(() => getError()); |
| 40 | + setTimeout(() => catchError(data, err => expect(err).toBeInstanceOf(Error)), 1); |
| 41 | + }); |
| 42 | + }); |
| 43 | + test("catch error after arg change - initial valid", () => |
| 44 | + createRoot(async dispose => { |
| 45 | + async function throwWhenError(arg: string): Promise<string> { |
| 46 | + if (arg === "error") throw new Error("error"); |
| 47 | + return arg; |
| 48 | + } |
| 49 | + |
| 50 | + const [arg, setArg] = createSignal(""); |
| 51 | + function Child() { |
| 52 | + const data = createAsync(() => throwWhenError(arg())); |
| 53 | + |
| 54 | + return ( |
| 55 | + <div id="child"> |
| 56 | + <ErrorBoundary |
| 57 | + fallback={(_, reset) => ( |
| 58 | + <div id="childError"> |
| 59 | + <button |
| 60 | + id="reset" |
| 61 | + onClick={() => { |
| 62 | + setArg("true"); |
| 63 | + reset(); |
| 64 | + }} |
| 65 | + /> |
| 66 | + </div> |
| 67 | + )} |
| 68 | + > |
| 69 | + <Suspense> |
| 70 | + <p id="data">{data()}</p> |
| 71 | + <p id="latest">{data.latest}</p> |
| 72 | + </Suspense> |
| 73 | + </ErrorBoundary> |
| 74 | + </div> |
| 75 | + ); |
| 76 | + } |
| 77 | + await render( |
| 78 | + () => ( |
| 79 | + <Parent> |
| 80 | + <Child /> |
| 81 | + </Parent> |
| 82 | + ), |
| 83 | + document.body |
| 84 | + ); |
| 85 | + const childErrorElement = () => document.getElementById("childError"); |
| 86 | + const parentErrorElement = document.getElementById("parentError"); |
| 87 | + expect(childErrorElement()).toBeNull(); |
| 88 | + expect(parentErrorElement).toBeNull(); |
| 89 | + setArg("error"); |
| 90 | + await awaitPromise(); |
| 91 | + |
| 92 | + // after changing the arg the error should still be caught by the Child's ErrorBoundary |
| 93 | + expect(childErrorElement()).not.toBeNull(); |
| 94 | + expect(parentErrorElement).toBeNull(); |
| 95 | + |
| 96 | + //reset ErrorBoundary |
| 97 | + document.getElementById("reset")?.click(); |
| 98 | + |
| 99 | + expect(childErrorElement()).toBeNull(); |
| 100 | + await awaitPromise(); |
| 101 | + const dataEl = () => document.getElementById("data"); |
| 102 | + |
| 103 | + expect(dataEl()).not.toBeNull(); |
| 104 | + expect(document.getElementById("data")?.innerHTML).toBe("true"); |
| 105 | + expect(document.getElementById("latest")?.innerHTML).toBe("true"); |
| 106 | + |
| 107 | + document.body.innerHTML = ""; |
| 108 | + dispose(); |
| 109 | + })); |
| 110 | + test("catch consecutive error after initial error change to be caught after arg change", () => |
| 111 | + createRoot(async cleanup => { |
| 112 | + const [arg, setArg] = createSignal("error"); |
| 113 | + function Child() { |
| 114 | + const data = createAsync(() => getError(arg())); |
| 115 | + |
| 116 | + return ( |
| 117 | + <div id="child"> |
| 118 | + <ErrorBoundary |
| 119 | + fallback={(_, reset) => ( |
| 120 | + <div id="childError"> |
| 121 | + <button id="reset" onClick={() => reset()} /> |
| 122 | + </div> |
| 123 | + )} |
| 124 | + > |
| 125 | + <Suspense>{data()}</Suspense> |
| 126 | + </ErrorBoundary> |
| 127 | + </div> |
| 128 | + ); |
| 129 | + } |
| 130 | + await render( |
| 131 | + () => ( |
| 132 | + <Parent> |
| 133 | + <Child /> |
| 134 | + </Parent> |
| 135 | + ), |
| 136 | + document.body |
| 137 | + ); |
| 138 | + |
| 139 | + // Child's ErrorBoundary should catch the error |
| 140 | + expect(document.getElementById("childError")).not.toBeNull(); |
| 141 | + expect(document.getElementById("parentError")).toBeNull(); |
| 142 | + setArg("error_2"); |
| 143 | + await awaitPromise(); |
| 144 | + // after changing the arg the error should still be caught by the Child's ErrorBoundary |
| 145 | + expect(document.getElementById("childError")).not.toBeNull(); |
| 146 | + expect(document.getElementById("parentError")).toBeNull(); |
| 147 | + |
| 148 | + document.getElementById("reset")?.click(); |
| 149 | + await awaitPromise(); |
| 150 | + expect(document.getElementById("childError")).not.toBeNull(); |
| 151 | + expect(document.getElementById("parentError")).toBeNull(); |
| 152 | + |
| 153 | + document.body.innerHTML = ""; |
| 154 | + cleanup(); |
| 155 | + })); |
| 156 | +}); |
0 commit comments