|
| 1 | +import { expect, it } from "vite-plus/test"; |
| 2 | +import { loadFileAncestors } from "./loadFileAncestors"; |
| 3 | + |
| 4 | +function queuedLoader() { |
| 5 | + const paths: string[] = []; |
| 6 | + const resolve: Array<() => void> = []; |
| 7 | + const load = (path: string) => { |
| 8 | + paths.push(path); |
| 9 | + return new Promise<void>((done) => resolve.push(done)); |
| 10 | + }; |
| 11 | + return { paths, resolve, load }; |
| 12 | +} |
| 13 | + |
| 14 | +it("bounds loads to four and finishes children even when parents finish last", async () => { |
| 15 | + const queue = queuedLoader(); |
| 16 | + const loading = loadFileAncestors("a/b/c/d/e/f/file.ts", queue.load, new AbortController()); |
| 17 | + for (let index = 3; index < 6; index++) { |
| 18 | + queue.resolve[index]?.(); |
| 19 | + await Promise.resolve(); |
| 20 | + expect(queue.paths).toHaveLength(index + 2); |
| 21 | + } |
| 22 | + expect(queue.paths).toEqual(["", "a", "a/b", "a/b/c", "a/b/c/d", "a/b/c/d/e", "a/b/c/d/e/f"]); |
| 23 | + for (const resolve of queue.resolve) resolve(); |
| 24 | + await loading; |
| 25 | +}); |
| 26 | + |
| 27 | +it("stops aborted reveals while a new selection loads independently", async () => { |
| 28 | + const old = queuedLoader(); |
| 29 | + const controller = new AbortController(); |
| 30 | + const oldReveal = loadFileAncestors("old/a/b/c/d/file.ts", old.load, controller); |
| 31 | + controller.abort(); |
| 32 | + const fresh = queuedLoader(); |
| 33 | + const newReveal = loadFileAncestors("new/file.ts", fresh.load, new AbortController()); |
| 34 | + for (const resolve of [...old.resolve, ...fresh.resolve]) resolve(); |
| 35 | + await Promise.all([oldReveal, newReveal]); |
| 36 | + await loadFileAncestors("aborted/path/file.ts", old.load, controller); |
| 37 | + expect(old.paths).toEqual(["", "old", "old/a", "old/a/b"]); |
| 38 | + expect(fresh.paths).toEqual(["", "new"]); |
| 39 | +}); |
0 commit comments