|
| 1 | +import { expect, it, vi } from "vitest" |
| 2 | + |
| 3 | +import { rest } from "msw" |
| 4 | + |
| 5 | +import { createTestClient } from "./createClient" |
| 6 | +import { mockPrismicRestAPIV2 } from "./mockPrismicRestAPIV2" |
| 7 | + |
| 8 | +import * as prismic from "../../src" |
| 9 | + |
| 10 | +type TestInvalidRefRetryArgs = { |
| 11 | + run: ( |
| 12 | + client: prismic.Client, |
| 13 | + params?: Parameters<prismic.Client["get"]>[0], |
| 14 | + ) => Promise<unknown> |
| 15 | +} |
| 16 | + |
| 17 | +export const testInvalidRefRetry = (args: TestInvalidRefRetryArgs): void => { |
| 18 | + it("retries with the master ref when an invalid ref is used", async (ctx) => { |
| 19 | + const client = createTestClient({ ctx }) |
| 20 | + const badRef = ctx.mock.api.ref().ref |
| 21 | + const masterRef = ctx.mock.api.ref().ref |
| 22 | + const queryResponse = ctx.mock.api.query({ |
| 23 | + documents: [ctx.mock.value.document()], |
| 24 | + }) |
| 25 | + |
| 26 | + const triedRefs = new Set<string | null>() |
| 27 | + |
| 28 | + mockPrismicRestAPIV2({ ctx, queryResponse }) |
| 29 | + const endpoint = new URL( |
| 30 | + "documents/search", |
| 31 | + `${client.documentAPIEndpoint}/`, |
| 32 | + ).toString() |
| 33 | + ctx.server.use( |
| 34 | + rest.get(endpoint, (req) => { |
| 35 | + triedRefs.add(req.url.searchParams.get("ref")) |
| 36 | + }), |
| 37 | + rest.get(endpoint, (_req, res, ctx) => |
| 38 | + res.once( |
| 39 | + ctx.json({ |
| 40 | + type: "api_notfound_error", |
| 41 | + message: `Master ref is: ${masterRef}`, |
| 42 | + }), |
| 43 | + ctx.status(404), |
| 44 | + ), |
| 45 | + ), |
| 46 | + ) |
| 47 | + |
| 48 | + const consoleWarnSpy = vi |
| 49 | + .spyOn(console, "warn") |
| 50 | + .mockImplementation(() => void 0) |
| 51 | + await args.run(client, { ref: badRef }) |
| 52 | + consoleWarnSpy.mockRestore() |
| 53 | + |
| 54 | + expect([...triedRefs]).toStrictEqual([badRef, masterRef]) |
| 55 | + }) |
| 56 | + |
| 57 | + it("retries with the master ref when an expired ref is used", async (ctx) => { |
| 58 | + const client = createTestClient({ ctx }) |
| 59 | + const badRef = ctx.mock.api.ref().ref |
| 60 | + const masterRef = ctx.mock.api.ref().ref |
| 61 | + const queryResponse = ctx.mock.api.query({ |
| 62 | + documents: [ctx.mock.value.document()], |
| 63 | + }) |
| 64 | + |
| 65 | + const triedRefs = new Set<string | null>() |
| 66 | + |
| 67 | + mockPrismicRestAPIV2({ ctx, queryResponse }) |
| 68 | + const endpoint = new URL( |
| 69 | + "documents/search", |
| 70 | + `${client.documentAPIEndpoint}/`, |
| 71 | + ).toString() |
| 72 | + ctx.server.use( |
| 73 | + rest.get(endpoint, (req) => { |
| 74 | + triedRefs.add(req.url.searchParams.get("ref")) |
| 75 | + }), |
| 76 | + rest.get(endpoint, (_req, res, ctx) => |
| 77 | + res.once( |
| 78 | + ctx.json({ message: `Master ref is: ${masterRef}` }), |
| 79 | + ctx.status(410), |
| 80 | + ), |
| 81 | + ), |
| 82 | + ) |
| 83 | + |
| 84 | + const consoleWarnSpy = vi |
| 85 | + .spyOn(console, "warn") |
| 86 | + .mockImplementation(() => void 0) |
| 87 | + await args.run(client, { ref: badRef }) |
| 88 | + consoleWarnSpy.mockRestore() |
| 89 | + |
| 90 | + expect([...triedRefs]).toStrictEqual([badRef, masterRef]) |
| 91 | + }) |
| 92 | + |
| 93 | + it("throws if the maximum number of retries with invalid refs is reached", async (ctx) => { |
| 94 | + const client = createTestClient({ ctx }) |
| 95 | + const queryResponse = ctx.mock.api.query({ |
| 96 | + documents: [ctx.mock.value.document()], |
| 97 | + }) |
| 98 | + |
| 99 | + const triedRefs = new Set<string | null>() |
| 100 | + |
| 101 | + mockPrismicRestAPIV2({ ctx, queryResponse }) |
| 102 | + const endpoint = new URL( |
| 103 | + "documents/search", |
| 104 | + `${client.documentAPIEndpoint}/`, |
| 105 | + ).toString() |
| 106 | + ctx.server.use( |
| 107 | + rest.get(endpoint, (req) => { |
| 108 | + triedRefs.add(req.url.searchParams.get("ref")) |
| 109 | + }), |
| 110 | + rest.get(endpoint, (_req, res, requestCtx) => |
| 111 | + res( |
| 112 | + requestCtx.json({ |
| 113 | + type: "api_notfound_error", |
| 114 | + message: `Master ref is: ${ctx.mock.api.ref().ref}`, |
| 115 | + }), |
| 116 | + requestCtx.status(404), |
| 117 | + ), |
| 118 | + ), |
| 119 | + ) |
| 120 | + |
| 121 | + const consoleWarnSpy = vi |
| 122 | + .spyOn(console, "warn") |
| 123 | + .mockImplementation(() => void 0) |
| 124 | + await expect(async () => { |
| 125 | + await args.run(client) |
| 126 | + }).rejects.toThrow(prismic.RefNotFoundError) |
| 127 | + consoleWarnSpy.mockRestore() |
| 128 | + |
| 129 | + expect(triedRefs.size).toBe(3) |
| 130 | + }) |
| 131 | + |
| 132 | + it("fetches a new master ref on subsequent queries if an invalid ref is used", async (ctx) => { |
| 133 | + const client = createTestClient({ ctx }) |
| 134 | + const queryResponse = ctx.mock.api.query({ |
| 135 | + documents: [ctx.mock.value.document()], |
| 136 | + }) |
| 137 | + |
| 138 | + const triedRefs = new Set<string | null>() |
| 139 | + |
| 140 | + mockPrismicRestAPIV2({ ctx, queryResponse }) |
| 141 | + const endpoint = new URL( |
| 142 | + "documents/search", |
| 143 | + `${client.documentAPIEndpoint}/`, |
| 144 | + ).toString() |
| 145 | + ctx.server.use( |
| 146 | + rest.get(endpoint, (req) => { |
| 147 | + triedRefs.add(req.url.searchParams.get("ref")) |
| 148 | + }), |
| 149 | + rest.get(endpoint, (_req, res, requestCtx) => |
| 150 | + res.once( |
| 151 | + requestCtx.json({ |
| 152 | + type: "api_notfound_error", |
| 153 | + message: `Master ref is: ${ctx.mock.api.ref().ref}`, |
| 154 | + }), |
| 155 | + requestCtx.status(404), |
| 156 | + ), |
| 157 | + ), |
| 158 | + ) |
| 159 | + |
| 160 | + const consoleWarnSpy = vi |
| 161 | + .spyOn(console, "warn") |
| 162 | + .mockImplementation(() => void 0) |
| 163 | + await args.run(client) |
| 164 | + consoleWarnSpy.mockRestore() |
| 165 | + |
| 166 | + await args.run(client) |
| 167 | + |
| 168 | + expect(triedRefs.size).toBe(3) |
| 169 | + }) |
| 170 | +} |
0 commit comments