-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add `fetchOptions` parameter * test: `fetchOptions`
- Loading branch information
1 parent
8f36b01
commit a492a40
Showing
34 changed files
with
375 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import * as prismic from "../../src"; | ||
import { createTestClient } from "./createClient"; | ||
import { mockPrismicRestAPIV2 } from "./mockPrismicRestAPIV2"; | ||
import fetch from "node-fetch"; | ||
import { expect, it, vi } from "vitest"; | ||
|
||
type TestFetchOptionsArgs = { | ||
run: ( | ||
client: prismic.Client, | ||
params?: Parameters<prismic.Client["get"]>[0], | ||
) => Promise<unknown>; | ||
}; | ||
|
||
export const testFetchOptions = ( | ||
description: string, | ||
args: TestFetchOptionsArgs, | ||
): void => { | ||
it.concurrent(`${description} (on client)`, async (ctx) => { | ||
const abortController = new AbortController(); | ||
|
||
const fetchSpy = vi.fn(fetch); | ||
const fetchOptions: prismic.RequestInitLike = { | ||
cache: "no-store", | ||
headers: { | ||
foo: "bar", | ||
}, | ||
signal: abortController.signal, | ||
}; | ||
|
||
const repositoryResponse = ctx.mock.api.repository(); | ||
const masterRef = ctx.mock.api.ref({ isMasterRef: true }); | ||
const releaseRef = ctx.mock.api.ref({ isMasterRef: false }); | ||
releaseRef.id = "id"; // Referenced in ref-related tests. | ||
releaseRef.label = "label"; // Referenced in ref-related tests. | ||
repositoryResponse.refs = [masterRef, releaseRef]; | ||
|
||
mockPrismicRestAPIV2({ | ||
ctx, | ||
repositoryResponse, | ||
queryResponse: ctx.mock.api.query({ | ||
documents: [ctx.mock.value.document()], | ||
}), | ||
}); | ||
|
||
const client = createTestClient({ | ||
clientConfig: { | ||
fetch: fetchSpy, | ||
fetchOptions, | ||
}, | ||
}); | ||
|
||
await args.run(client); | ||
|
||
for (const [input, init] of fetchSpy.mock.calls) { | ||
expect(init, input.toString()).toStrictEqual(fetchOptions); | ||
} | ||
}); | ||
|
||
it.concurrent(`${description} (on method)`, async (ctx) => { | ||
const abortController = new AbortController(); | ||
|
||
const fetchSpy = vi.fn(fetch); | ||
const fetchOptions: prismic.RequestInitLike = { | ||
cache: "no-store", | ||
headers: { | ||
foo: "bar", | ||
}, | ||
signal: abortController.signal, | ||
}; | ||
|
||
const repositoryResponse = ctx.mock.api.repository(); | ||
const masterRef = ctx.mock.api.ref({ isMasterRef: true }); | ||
const releaseRef = ctx.mock.api.ref({ isMasterRef: false }); | ||
releaseRef.id = "id"; // Referenced in ref-related tests. | ||
releaseRef.label = "label"; // Referenced in ref-related tests. | ||
repositoryResponse.refs = [masterRef, releaseRef]; | ||
|
||
mockPrismicRestAPIV2({ | ||
ctx, | ||
repositoryResponse, | ||
queryResponse: ctx.mock.api.query({ | ||
documents: [ctx.mock.value.document()], | ||
}), | ||
}); | ||
|
||
const client = createTestClient({ | ||
clientConfig: { | ||
fetch: fetchSpy, | ||
}, | ||
}); | ||
|
||
await args.run(client, { fetchOptions }); | ||
|
||
for (const [input, init] of fetchSpy.mock.calls) { | ||
expect(init, input.toString()).toStrictEqual(fetchOptions); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.