diff --git a/test/__testutils__/testInvalidRefRetry.ts b/test/__testutils__/testInvalidRefRetry.ts index fc3192e7..05c428b2 100644 --- a/test/__testutils__/testInvalidRefRetry.ts +++ b/test/__testutils__/testInvalidRefRetry.ts @@ -3,10 +3,9 @@ import { expect, it, vi } from "vitest" import { rest } from "msw" import { createTestClient } from "./createClient" -import { getMasterRef } from "./getMasterRef" import { mockPrismicRestAPIV2 } from "./mockPrismicRestAPIV2" -import * as prismic from "../../src" +import type * as prismic from "../../src" type TestInvalidRefRetryArgs = { run: ( @@ -15,74 +14,85 @@ type TestInvalidRefRetryArgs = { ) => Promise } -export const testInvalidRefRetry = ( - description: string, - args: TestInvalidRefRetryArgs, -): void => { - it.concurrent(description, async (ctx) => { - const client = createTestClient({ ctx }) - - const triedRefs: string[] = [] - - const repositoryResponse = ctx.mock.api.repository() - repositoryResponse.refs = [ctx.mock.api.ref({ isMasterRef: true })] - - const latestRef = ctx.mock.api.ref().ref - - mockPrismicRestAPIV2({ ctx, repositoryResponse }) - - const queryEndpoint = new URL( - "documents/search", - `${client.documentAPIEndpoint}/`, - ).toString() - - ctx.server.use( - rest.get(queryEndpoint, (req, res, ctx) => { - const ref = req.url.searchParams.get("ref") - if (ref) { - triedRefs.push(ref) - } - - if (triedRefs.length <= 1) { - return res( - ctx.status(404), +export const testInvalidRefRetry = (args: TestInvalidRefRetryArgs): void => { + it.concurrent( + "retries with the master ref when an invalid ref is used", + async (ctx) => { + const client = createTestClient({ ctx }) + const badRef = ctx.mock.api.ref().ref + const masterRef = ctx.mock.api.ref().ref + const queryResponse = ctx.mock.api.query({ + documents: [ctx.mock.value.document()], + }) + + const triedRefs: (string | null)[] = [] + + mockPrismicRestAPIV2({ ctx, queryResponse }) + const endpoint = new URL( + "documents/search", + `${client.documentAPIEndpoint}/`, + ).toString() + ctx.server.use( + rest.get(endpoint, (req) => { + triedRefs.push(req.url.searchParams.get("ref")) + }), + rest.get(endpoint, (_req, res, ctx) => + res.once( ctx.json({ type: "api_notfound_error", - message: `Ref not found. Ensure you have the correct ref and try again. Master ref is: ${latestRef}`, + message: `Master ref is: ${masterRef}`, }), - ) - } - }), - ) - - const consoleWarnSpy = vi - .spyOn(console, "warn") - .mockImplementation(() => void 0) - - await args.run(client) - - expect(triedRefs).toStrictEqual([ - getMasterRef(repositoryResponse), - latestRef, - ]) - - // Check that refs are not retried more than once. - ctx.server.use( - rest.get(queryEndpoint, (_req, res, ctx) => { - return res( - ctx.status(404), - ctx.json({ - type: "api_notfound_error", - message: `Ref not found. Ensure you have the correct ref and try again. Master ref is: ${triedRefs[0]}`, - }), - ) - }), - ) - - await expect(async () => { - await args.run(client) - }).rejects.toThrow(prismic.RefNotFoundError) - - consoleWarnSpy.mockRestore() - }) + ctx.status(404), + ), + ), + ) + + const consoleWarnSpy = vi + .spyOn(console, "warn") + .mockImplementation(() => void 0) + await args.run(client, { ref: badRef }) + consoleWarnSpy.mockRestore() + + expect(triedRefs).toStrictEqual([badRef, masterRef]) + }, + ) + + it.concurrent( + "retries with the master ref when an expired ref is used", + async (ctx) => { + const client = createTestClient({ ctx }) + const badRef = ctx.mock.api.ref().ref + const masterRef = ctx.mock.api.ref().ref + const queryResponse = ctx.mock.api.query({ + documents: [ctx.mock.value.document()], + }) + + const triedRefs: (string | null)[] = [] + + mockPrismicRestAPIV2({ ctx, queryResponse }) + const endpoint = new URL( + "documents/search", + `${client.documentAPIEndpoint}/`, + ).toString() + ctx.server.use( + rest.get(endpoint, (req) => { + triedRefs.push(req.url.searchParams.get("ref")) + }), + rest.get(endpoint, (_req, res, ctx) => + res.once( + ctx.json({ message: `Master ref is: ${masterRef}` }), + ctx.status(410), + ), + ), + ) + + const consoleWarnSpy = vi + .spyOn(console, "warn") + .mockImplementation(() => void 0) + await args.run(client, { ref: badRef }) + consoleWarnSpy.mockRestore() + + expect(triedRefs).toStrictEqual([badRef, masterRef]) + }, + ) } diff --git a/test/client-get.test.ts b/test/client-get.test.ts index d3db5f57..ecadaa38 100644 --- a/test/client-get.test.ts +++ b/test/client-get.test.ts @@ -93,7 +93,7 @@ it("uses cached repository metadata within the client's repository cache TTL", a ) }) -testInvalidRefRetry("retries on expired refs", { +testInvalidRefRetry({ run: (client, params) => client.get(params), }) diff --git a/test/client-getAllByEveryTag.test.ts b/test/client-getAllByEveryTag.test.ts index ffca2b71..73a45ce2 100644 --- a/test/client-getAllByEveryTag.test.ts +++ b/test/client-getAllByEveryTag.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetAllMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetAllMethod("returns all documents by every tag from paginated response", { run: (client) => client.getAllByEveryTag(["foo", "bar"]), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getAllByEveryTag(["foo", "bar"], params), }) +testInvalidRefRetry({ + run: (client, params) => client.getAllByEveryTag(["foo", "bar"], params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getAllByEveryTag(["foo", "bar"], params), }) diff --git a/test/client-getAllByIDs.test.ts b/test/client-getAllByIDs.test.ts index fbe027a3..44fd5e73 100644 --- a/test/client-getAllByIDs.test.ts +++ b/test/client-getAllByIDs.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetAllMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetAllMethod("returns all documents by IDs from paginated response", { run: (client) => client.getAllByIDs(["id1", "id2"]), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getAllByIDs(["id1", "id2"], params), }) +testInvalidRefRetry({ + run: (client, params) => client.getAllByIDs(["id1", "id2"], params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getAllByIDs(["id1", "id2"], params), }) diff --git a/test/client-getAllBySomeTags.test.ts b/test/client-getAllBySomeTags.test.ts index 002f92b0..e13ca5cd 100644 --- a/test/client-getAllBySomeTags.test.ts +++ b/test/client-getAllBySomeTags.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetAllMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetAllMethod("returns all documents by some tags from paginated response", { run: (client) => client.getAllBySomeTags(["foo", "bar"]), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getAllBySomeTags(["foo", "bar"], params), }) +testInvalidRefRetry({ + run: (client, params) => client.getAllBySomeTags(["foo", "bar"], params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getAllBySomeTags(["foo", "bar"], params), }) diff --git a/test/client-getAllByTag.test.ts b/test/client-getAllByTag.test.ts index d2b81bd4..488afdac 100644 --- a/test/client-getAllByTag.test.ts +++ b/test/client-getAllByTag.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetAllMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetAllMethod("returns all documents by tag from paginated response", { run: (client) => client.getAllByTag("tag"), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getAllByTag("tag", params), }) +testInvalidRefRetry({ + run: (client, params) => client.getAllByTag("tag", params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getAllByTag("tag", params), }) diff --git a/test/client-getAllByType.test.ts b/test/client-getAllByType.test.ts index 2af6e770..d1045604 100644 --- a/test/client-getAllByType.test.ts +++ b/test/client-getAllByType.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetAllMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetAllMethod("returns all documents by type from paginated response", { run: (client) => client.getAllByType("type"), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getAllByType("type", params), }) +testInvalidRefRetry({ + run: (client, params) => client.getAllByType("type", params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getAllByType("type", params), }) diff --git a/test/client-getAllByUIDs.test.ts b/test/client-getAllByUIDs.test.ts index c1ddb21b..9d6d2b7d 100644 --- a/test/client-getAllByUIDs.test.ts +++ b/test/client-getAllByUIDs.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetAllMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetAllMethod("returns all documents by UIDs from paginated response", { run: (client) => client.getAllByUIDs("type", ["uid1", "uid2"]), @@ -36,6 +37,11 @@ testFetchOptions("supports fetch options", { client.getAllByUIDs("type", ["uid1", "uid2"], params), }) +testInvalidRefRetry({ + run: (client, params) => + client.getAllByUIDs("type", ["uid1", "uid2"], params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getAllByUIDs("type", ["uid1", "uid2"], params), diff --git a/test/client-getByEveryTag.test.ts b/test/client-getByEveryTag.test.ts index fa5b34a5..1ee3a19b 100644 --- a/test/client-getByEveryTag.test.ts +++ b/test/client-getByEveryTag.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetMethod("queries for documents by tag", { run: (client) => client.getByEveryTag(["foo", "bar"]), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getByEveryTag(["foo", "bar"], params), }) +testInvalidRefRetry({ + run: (client, params) => client.getByEveryTag(["foo", "bar"], params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getByEveryTag(["foo", "bar"], params), }) diff --git a/test/client-getByID.test.ts b/test/client-getByID.test.ts index 1914dffa..1559a9a3 100644 --- a/test/client-getByID.test.ts +++ b/test/client-getByID.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetFirstMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetFirstMethod("queries for document by ID", { run: (client) => client.getByID("id"), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getByID("id", params), }) +testInvalidRefRetry({ + run: (client, params) => client.getByID("id", params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getByID("id", params), }) diff --git a/test/client-getByIDs.test.ts b/test/client-getByIDs.test.ts index 1bbfbad9..b73e21e0 100644 --- a/test/client-getByIDs.test.ts +++ b/test/client-getByIDs.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetMethod("queries for documents by IDs", { run: (client) => client.getByIDs(["id1", "id2"]), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getByIDs(["id1", "id2"], params), }) +testInvalidRefRetry({ + run: (client, params) => client.getByIDs(["id1", "id2"], params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getByIDs(["id1", "id2"], params), }) diff --git a/test/client-getBySomeTags.test.ts b/test/client-getBySomeTags.test.ts index b90922be..7adef377 100644 --- a/test/client-getBySomeTags.test.ts +++ b/test/client-getBySomeTags.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetMethod("queries for documents by some tags", { run: (client) => client.getBySomeTags(["foo", "bar"]), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getBySomeTags(["foo", "bar"], params), }) +testInvalidRefRetry({ + run: (client, params) => client.getBySomeTags(["foo", "bar"], params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getBySomeTags(["foo", "bar"], params), }) diff --git a/test/client-getByTag.test.ts b/test/client-getByTag.test.ts index fa6d0567..e12d19d4 100644 --- a/test/client-getByTag.test.ts +++ b/test/client-getByTag.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetMethod("queries for documents by tag", { run: (client) => client.getByTag("tag"), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getByTag("tag", params), }) +testInvalidRefRetry({ + run: (client, params) => client.getByTag("tag", params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getByTag("tag", params), }) diff --git a/test/client-getByType.test.ts b/test/client-getByType.test.ts index 9ef415b2..42cf032c 100644 --- a/test/client-getByType.test.ts +++ b/test/client-getByType.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetMethod("queries for documents by type", { run: (client) => client.getByType("type"), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getByType("type", params), }) +testInvalidRefRetry({ + run: (client, params) => client.getByType("type", params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getByType("type", params), }) diff --git a/test/client-getByUID.test.ts b/test/client-getByUID.test.ts index 27fa358a..4f4a49c8 100644 --- a/test/client-getByUID.test.ts +++ b/test/client-getByUID.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetFirstMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetFirstMethod("queries for document by UID", { run: (client) => client.getByUID("type", "uid"), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getByUID("type", "uid", params), }) +testInvalidRefRetry({ + run: (client, params) => client.getByUID("type", "uid", params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getByUID("type", "uid", params), }) diff --git a/test/client-getByUIDs.test.ts b/test/client-getByUIDs.test.ts index 45ac76ac..00f89b69 100644 --- a/test/client-getByUIDs.test.ts +++ b/test/client-getByUIDs.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetMethod("queries for documents by UIDs", { run: (client) => client.getByUIDs("type", ["uid1", "uid2"]), @@ -35,6 +36,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getByUIDs("type", ["uid1", "uid2"], params), }) +testInvalidRefRetry({ + run: (client, params) => client.getByUIDs("type", ["uid1", "uid2"], params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getByUIDs("type", ["uid1", "uid2"], params), }) diff --git a/test/client-getFirst.test.ts b/test/client-getFirst.test.ts index 48634f67..8235233b 100644 --- a/test/client-getFirst.test.ts +++ b/test/client-getFirst.test.ts @@ -8,6 +8,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetFirstMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" import * as prismic from "../src" @@ -98,6 +99,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getFirst(params), }) +testInvalidRefRetry({ + run: (client, params) => client.getFirst(params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getFirst(params), }) diff --git a/test/client-getSingle.test.ts b/test/client-getSingle.test.ts index 8e123578..b1fa3f51 100644 --- a/test/client-getSingle.test.ts +++ b/test/client-getSingle.test.ts @@ -2,6 +2,7 @@ import { testAbortableMethod } from "./__testutils__/testAbortableMethod" import { testGetFirstMethod } from "./__testutils__/testAnyGetMethod" import { testConcurrentMethod } from "./__testutils__/testConcurrentMethod" import { testFetchOptions } from "./__testutils__/testFetchOptions" +import { testInvalidRefRetry } from "./__testutils__/testInvalidRefRetry" testGetFirstMethod("queries for singleton document", { run: (client) => client.getSingle("type"), @@ -29,6 +30,10 @@ testFetchOptions("supports fetch options", { run: (client, params) => client.getSingle("type", params), }) +testInvalidRefRetry({ + run: (client, params) => client.getSingle("type", params), +}) + testAbortableMethod("is abortable with an AbortController", { run: (client, params) => client.getSingle("type", params), })