File
Blob: tests/client/lib/request-guard.test.ts
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { createRequestGuard } from "@/client/lib/request-guard"; |
| 3 | |
| 4 | describe("createRequestGuard", () => { |
| 5 | it("treats the newest request as current", () => { |
| 6 | const epochRef = { current: 0 }; |
| 7 | const activeRef = { current: true }; |
| 8 | |
| 9 | const request = createRequestGuard(epochRef, activeRef); |
| 10 | |
| 11 | expect(request.isCurrent()).toBe(true); |
| 12 | }); |
| 13 | |
| 14 | it("invalidates older requests after navigation starts a newer request", () => { |
| 15 | const epochRef = { current: 0 }; |
| 16 | const activeRef = { current: true }; |
| 17 | |
| 18 | const first = createRequestGuard(epochRef, activeRef); |
| 19 | const second = createRequestGuard(epochRef, activeRef); |
| 20 | |
| 21 | expect(first.isCurrent()).toBe(false); |
| 22 | expect(second.isCurrent()).toBe(true); |
| 23 | }); |
| 24 | |
| 25 | it("invalidates requests on cleanup", () => { |
| 26 | const epochRef = { current: 0 }; |
| 27 | const activeRef = { current: true }; |
| 28 | |
| 29 | const request = createRequestGuard(epochRef, activeRef); |
| 30 | request.cancel(); |
| 31 | |
| 32 | expect(request.isCurrent()).toBe(false); |
| 33 | }); |
| 34 | |
| 35 | it("invalidates requests after the caller becomes inactive", () => { |
| 36 | const epochRef = { current: 0 }; |
| 37 | const activeRef = { current: true }; |
| 38 | |
| 39 | const request = createRequestGuard(epochRef, activeRef); |
| 40 | activeRef.current = false; |
| 41 | |
| 42 | expect(request.isCurrent()).toBe(false); |
| 43 | }); |
| 44 | }); |