|
13 | 13 | import { expect, test } from "vitest"; |
14 | 14 | import { |
15 | 15 | createEffect, |
| 16 | + createLoadingBoundary, |
16 | 17 | createOptimisticStore, |
17 | 18 | createRoot, |
18 | 19 | createSignal, |
19 | | - flush |
| 20 | + createStore, |
| 21 | + flush, |
| 22 | + isPending |
20 | 23 | } from "../../src/index.js"; |
21 | 24 | import type { Store } from "../../src/store.js"; |
22 | 25 |
|
@@ -138,3 +141,113 @@ test("#3146: per-yield landings still reveal on arrival under the flight-owned t |
138 | 141 | dispose(); |
139 | 142 | flush(); |
140 | 143 | }); |
| 144 | + |
| 145 | +// The FIRST flight is the carve-out: nothing has committed yet, so there is |
| 146 | +// no truth to keep on screen and no optimistic state to protect. Declaring |
| 147 | +// the owned transaction for the uninitialized ask held every |
| 148 | +// transition-riding consumer — render()'s scheduled root insert included — |
| 149 | +// until the fetch landed: the page stayed blank and the Loading boundary's |
| 150 | +// fallback never showed, while createStore(fn, seed) and |
| 151 | +// createOptimistic(fn, seed) in the same spot showed it. A first flight |
| 152 | +// declares nothing, like the loading window (#2933); refetch flights |
| 153 | +// (initialized) declare exactly as the tests above pin. |
| 154 | +type Resolver = (items: string[]) => void; |
| 155 | + |
| 156 | +function boundaryHarness(make: (dep: () => number, resolvers: Resolver[]) => { items: string[] }) { |
| 157 | + const resolvers: Resolver[] = []; |
| 158 | + let setDep!: (v: number) => void; |
| 159 | + let result: unknown = "never-ran"; |
| 160 | + let store!: { items: string[] }; |
| 161 | + let dispose!: () => void; |
| 162 | + createRoot(d => { |
| 163 | + dispose = d; |
| 164 | + const [dep, _setDep] = createSignal(0); |
| 165 | + setDep = _setDep; |
| 166 | + store = make(dep, resolvers); |
| 167 | + const boundary = createLoadingBoundary( |
| 168 | + () => store.items.join(","), |
| 169 | + () => "loading" |
| 170 | + ); |
| 171 | + // A USER effect, like render()'s scheduled root insert: its apply is |
| 172 | + // stashed by an open transition until that transition settles. |
| 173 | + createEffect( |
| 174 | + () => boundary(), |
| 175 | + v => { |
| 176 | + result = v; |
| 177 | + } |
| 178 | + ); |
| 179 | + }); |
| 180 | + return { |
| 181 | + resolvers, |
| 182 | + setDep, |
| 183 | + get result() { |
| 184 | + return result; |
| 185 | + }, |
| 186 | + get store() { |
| 187 | + return store; |
| 188 | + }, |
| 189 | + dispose |
| 190 | + }; |
| 191 | +} |
| 192 | + |
| 193 | +test("#3146 carve-out: the first flight shows the boundary fallback; the refetch keeps content", async () => { |
| 194 | + const h = boundaryHarness((dep, resolvers) => { |
| 195 | + const [store] = createOptimisticStore<{ items: string[] }>( |
| 196 | + async () => { |
| 197 | + dep(); |
| 198 | + const items = await new Promise<string[]>(r => resolvers.push(r)); |
| 199 | + return { items }; |
| 200 | + }, |
| 201 | + { items: [] } |
| 202 | + ); |
| 203 | + return store; |
| 204 | + }); |
| 205 | + flush(); |
| 206 | + // The boundary's effect must APPLY on the initial flush (not be stashed |
| 207 | + // by a flight transaction) and show the fallback. |
| 208 | + expect(h.result).toBe("loading"); |
| 209 | + |
| 210 | + h.resolvers[0](["A"]); |
| 211 | + await tick(); |
| 212 | + flush(); |
| 213 | + expect(h.result).toBe("A"); |
| 214 | + |
| 215 | + // Refetch: stale-while-revalidate — content stays, isPending flips. |
| 216 | + h.setDep(1); |
| 217 | + flush(); |
| 218 | + await tick(); |
| 219 | + expect(h.result).toBe("A"); |
| 220 | + expect(isPending(() => h.store.items.length)).toBe(true); |
| 221 | + |
| 222 | + h.resolvers[1](["A", "B"]); |
| 223 | + await tick(); |
| 224 | + flush(); |
| 225 | + await tick(); |
| 226 | + flush(); |
| 227 | + expect(h.result).toBe("A,B"); |
| 228 | + expect(isPending(() => h.store.items.length)).toBe(false); |
| 229 | + h.dispose(); |
| 230 | + flush(); |
| 231 | +}); |
| 232 | + |
| 233 | +test("#3146 carve-out control: createStore(fn, seed) first flight shows the same fallback", async () => { |
| 234 | + const h = boundaryHarness((dep, resolvers) => { |
| 235 | + const [store] = createStore<{ items: string[] }>( |
| 236 | + async () => { |
| 237 | + dep(); |
| 238 | + const items = await new Promise<string[]>(r => resolvers.push(r)); |
| 239 | + return { items }; |
| 240 | + }, |
| 241 | + { items: [] } |
| 242 | + ); |
| 243 | + return store; |
| 244 | + }); |
| 245 | + flush(); |
| 246 | + expect(h.result).toBe("loading"); |
| 247 | + h.resolvers[0](["A"]); |
| 248 | + await tick(); |
| 249 | + flush(); |
| 250 | + expect(h.result).toBe("A"); |
| 251 | + h.dispose(); |
| 252 | + flush(); |
| 253 | +}); |
0 commit comments