|
| 1 | +import type { LoaderContext } from "astro/loaders"; |
| 2 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 3 | +import packageJson from "../../package.json"; |
| 4 | +import { cleanupEntries } from "../../src/loader/cleanup-entries"; |
| 5 | +import { handleRealtimeUpdates } from "../../src/loader/handle-realtime-updates"; |
| 6 | +import { loadEntries } from "../../src/loader/load-entries"; |
| 7 | +import { loader } from "../../src/loader/loader"; |
| 8 | +import { getSuperuserToken } from "../../src/utils/get-superuser-token"; |
| 9 | +import { createLoaderContext } from "../_mocks/create-loader-context"; |
| 10 | +import { createLoaderOptions } from "../_mocks/create-loader-options"; |
| 11 | +import { createPocketbaseEntry } from "../_mocks/create-pocketbase-entry"; |
| 12 | + |
| 13 | +vi.mock("../../src/utils/get-superuser-token"); |
| 14 | +vi.mock("../../src/utils/should-refresh"); |
| 15 | +vi.mock("../../src/loader/cleanup-entries"); |
| 16 | +vi.mock("../../src/loader/handle-realtime-updates"); |
| 17 | +vi.mock("../../src/loader/load-entries"); |
| 18 | + |
| 19 | +describe("loader", async () => { |
| 20 | + let context: LoaderContext; |
| 21 | + const options = createLoaderOptions({ updatedField: "updated" }); |
| 22 | + const srm = await import("../../src/utils/should-refresh"); |
| 23 | + const hrum = await import("../../src/loader/handle-realtime-updates"); |
| 24 | + const gstm = await import("../../src/utils/get-superuser-token"); |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + context = createLoaderContext(); |
| 28 | + context.meta.set("version", packageJson.version); |
| 29 | + context.meta.set( |
| 30 | + "last-modified", |
| 31 | + new Date().toISOString().replace("T", " ") |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + vi.resetAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + test("should not refresh if shouldRefresh returns false", async () => { |
| 40 | + srm.shouldRefresh = vi.fn().mockReturnValue(false); |
| 41 | + |
| 42 | + await loader(context, options); |
| 43 | + |
| 44 | + expect(srm.shouldRefresh).toHaveBeenCalledOnce(); |
| 45 | + expect(handleRealtimeUpdates).not.toHaveBeenCalled(); |
| 46 | + expect(loadEntries).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + test("should not refresh if handleRealtimeUpdates handled update", async () => { |
| 50 | + srm.shouldRefresh = vi.fn().mockReturnValue(true); |
| 51 | + hrum.handleRealtimeUpdates = vi.fn().mockResolvedValue(true); |
| 52 | + |
| 53 | + await loader(context, options); |
| 54 | + |
| 55 | + expect(handleRealtimeUpdates).toHaveBeenCalledOnce(); |
| 56 | + expect(loadEntries).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + test("should clear store and disable incremental builds if version changes", async () => { |
| 60 | + srm.shouldRefresh = vi.fn().mockReturnValue(true); |
| 61 | + hrum.handleRealtimeUpdates = vi.fn().mockResolvedValue(false); |
| 62 | + gstm.getSuperuserToken = vi.fn().mockResolvedValue(undefined); |
| 63 | + const storeClearSpy = vi.spyOn(context.store, "clear"); |
| 64 | + context.meta.set("version", "invalidVersion"); |
| 65 | + |
| 66 | + await loader(context, options); |
| 67 | + |
| 68 | + expect(storeClearSpy).toHaveBeenCalledOnce(); |
| 69 | + expect(loadEntries).toHaveBeenCalledWith( |
| 70 | + options, |
| 71 | + context, |
| 72 | + undefined, |
| 73 | + undefined |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + test("should disable incremental builds if no updatedField is provided", async () => { |
| 78 | + srm.shouldRefresh = vi.fn().mockReturnValue(true); |
| 79 | + hrum.handleRealtimeUpdates = vi.fn().mockResolvedValue(false); |
| 80 | + gstm.getSuperuserToken = vi.fn().mockResolvedValue(undefined); |
| 81 | + options.updatedField = undefined; |
| 82 | + |
| 83 | + await loader(context, options); |
| 84 | + |
| 85 | + expect(loadEntries).toHaveBeenCalledWith( |
| 86 | + options, |
| 87 | + context, |
| 88 | + undefined, |
| 89 | + undefined |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + test("should get superuser token if superuserCredentials are provided", async () => { |
| 94 | + const token = "token"; |
| 95 | + srm.shouldRefresh = vi.fn().mockReturnValue(true); |
| 96 | + hrum.handleRealtimeUpdates = vi.fn().mockResolvedValue(false); |
| 97 | + gstm.getSuperuserToken = vi.fn().mockResolvedValue(token); |
| 98 | + const entry = createPocketbaseEntry(); |
| 99 | + context.store.set({ id: entry.id, data: entry }); |
| 100 | + |
| 101 | + await loader(context, options); |
| 102 | + |
| 103 | + expect(getSuperuserToken).toHaveBeenCalledOnce(); |
| 104 | + expect(cleanupEntries).toHaveBeenCalledWith(options, context, token); |
| 105 | + expect(loadEntries).toHaveBeenCalledWith( |
| 106 | + options, |
| 107 | + context, |
| 108 | + token, |
| 109 | + undefined |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + test("should cleanup old entries if store has keys", async () => { |
| 114 | + srm.shouldRefresh = vi.fn().mockReturnValue(true); |
| 115 | + hrum.handleRealtimeUpdates = vi.fn().mockResolvedValue(false); |
| 116 | + gstm.getSuperuserToken = vi.fn().mockResolvedValue(undefined); |
| 117 | + const entry = createPocketbaseEntry(); |
| 118 | + context.store.set({ id: entry.id, data: entry }); |
| 119 | + |
| 120 | + await loader(context, options); |
| 121 | + |
| 122 | + expect(cleanupEntries).toHaveBeenCalledWith(options, context, undefined); |
| 123 | + }); |
| 124 | + |
| 125 | + test("should set last-modified and version in meta after loading entries", async () => { |
| 126 | + srm.shouldRefresh = vi.fn().mockReturnValue(true); |
| 127 | + hrum.handleRealtimeUpdates = vi.fn().mockResolvedValue(false); |
| 128 | + gstm.getSuperuserToken = vi.fn().mockResolvedValue(undefined); |
| 129 | + context.meta.delete("last-modified"); |
| 130 | + context.meta.delete("version"); |
| 131 | + |
| 132 | + await loader(context, options); |
| 133 | + |
| 134 | + expect(context.meta.get("last-modified")).toBeDefined(); |
| 135 | + expect(context.meta.get("version")).toBe(packageJson.version); |
| 136 | + }); |
| 137 | +}); |
0 commit comments