Skip to content

Commit

Permalink
test: setup mocks for asset API and migration API
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Sep 2, 2024
1 parent 7f75b64 commit 69f3c2f
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/types/api/asset/asset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Tag } from "./tag"
import type { AssetTag } from "./tag"

/**
* Asset types.
Expand Down Expand Up @@ -88,7 +88,7 @@ export type Asset = {
/**
* Asset tags.
*/
tags?: Tag[]
tags?: AssetTag[]

/**
* @internal
Expand Down
8 changes: 4 additions & 4 deletions src/types/api/asset/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @see Prismic asset API technical references: {@link https://prismic.io/docs/asset-api-technical-reference}
*/
export type Tag = {
export type AssetTag = {
/**
* Tag ID.
*/
Expand Down Expand Up @@ -40,14 +40,14 @@ export type Tag = {
*
* @see Prismic asset API technical references: {@link https://prismic.io/docs/asset-api-technical-reference}
*/
export type GetTagsResult = { items: Tag[] }
export type GetAssetTagsResult = { items: AssetTag[] }

/**
* Parameters for creating a tag in the asset API.
*
* @see Prismic asset API technical references: {@link https://prismic.io/docs/asset-api-technical-reference}
*/
export type PostTagParams = {
export type PostAssetTagParams = {
name: string
}

Expand All @@ -56,4 +56,4 @@ export type PostTagParams = {
*
* @see Prismic asset API technical references: {@link https://prismic.io/docs/asset-api-technical-reference}
*/
export type PostTagResult = Tag
export type PostAssetTagResult = AssetTag
2 changes: 1 addition & 1 deletion src/types/api/migration/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type PostDocumentResult<
lang: TLang
} & (TDocument["uid"] extends string
? { uid: TDocument["uid"] }
: Record<string, never>)
: { uid?: TDocument["uid"] })
: never

/**
Expand Down
151 changes: 151 additions & 0 deletions test/__testutils__/mockPrismicAssetAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import type { TestContext } from "vitest"

import { rest } from "msw"

import { createRepositoryName } from "./createRepositoryName"

import type {
Asset,
GetAssetsResult,
PostAssetResult,
} from "../../src/types/api/asset/asset"
import type {
AssetTag,
GetAssetTagsResult,
PostAssetTagParams,
PostAssetTagResult,
} from "../../src/types/api/asset/tag"

type MockPrismicMigrationAPIV2Args = {
ctx: TestContext
writeToken: string
expectedAsset?: Asset
expectedAssets?: Asset[]
expectedTag?: AssetTag
expectedTags?: AssetTag[]
}

const DEFAULT_TAG: AssetTag = {
id: "091daea1-4954-46c9-a819-faabb69464ea",
name: "fooTag",
uploader_id: "uploaded_id",
created_at: 0,
last_modified: 0,
count: 0,
}

const DEFAULT_ASSET: Asset = {
id: "Yz7kzxAAAB0AREK7",
uploader_id: "uploader_id",
created_at: 0,
last_modified: 0,
kind: "image",
filename: "foo.jpg",
extension: "jpg",
url: "https://example.com/foo.jpg",
width: 1,
height: 1,
size: 1,
notes: "notes",
credits: "credits",
alt: "alt",
origin_url: "origin_url",
search_highlight: { filename: [], notes: [], credits: [], alt: [] },
tags: [],
}

export const mockPrismicRestAPIV2 = (
args: MockPrismicMigrationAPIV2Args,
): void => {
const repositoryName = createRepositoryName()
const assetAPIEndpoint = `https://asset-api.prismic.io`

args.ctx.server.use(
rest.get(`${assetAPIEndpoint}/assets`, async (req, res, ctx) => {
if (
req.headers.get("authorization") !== `Bearer ${args.writeToken}` ||
req.headers.get("repository") !== repositoryName
) {
return res(ctx.status(401))
}

const items: Asset[] = args.expectedAssets || [DEFAULT_ASSET]

const response: GetAssetsResult = {
total: items.length,
items,
is_opensearch_result: false,
cursor: undefined,
missing_ids: [],
}

return res(ctx.json(response))
}),
)

args.ctx.server.use(
rest.post(`${assetAPIEndpoint}/assets`, async (req, res, ctx) => {
if (
req.headers.get("authorization") !== `Bearer ${args.writeToken}` ||
req.headers.get("repository") !== repositoryName
) {
return res(ctx.status(401))
}

const response: PostAssetResult = args.expectedAsset || DEFAULT_ASSET

return res(ctx.json(response))
}),
)

args.ctx.server.use(
rest.patch(`${assetAPIEndpoint}/assets/:id`, async (req, res, ctx) => {
if (
req.headers.get("authorization") !== `Bearer ${args.writeToken}` ||
req.headers.get("repository") !== repositoryName
) {
return res(ctx.status(401))
}

const response: PostAssetResult = args.expectedAsset || DEFAULT_ASSET

return res(ctx.json(response))
}),
)

args.ctx.server.use(
rest.get(`${assetAPIEndpoint}/tags`, async (req, res, ctx) => {
if (
req.headers.get("authorization") !== `Bearer ${args.writeToken}` ||
req.headers.get("repository") !== repositoryName
) {
return res(ctx.status(401))
}

const items: AssetTag[] = args.expectedTags || [DEFAULT_TAG]

const response: GetAssetTagsResult = { items }

return res(ctx.json(response))
}),
)

args.ctx.server.use(
rest.post(`${assetAPIEndpoint}/tags`, async (req, res, ctx) => {
if (
req.headers.get("authorization") !== `Bearer ${args.writeToken}` ||
req.headers.get("repository") !== repositoryName
) {
return res(ctx.status(401))
}

const body = await req.json<PostAssetTagParams>()
const response: PostAssetTagResult = args.expectedTag || {
...DEFAULT_TAG,
name: body.name,
}

return res(ctx.json(201), ctx.json(response))
}),
)
}
81 changes: 81 additions & 0 deletions test/__testutils__/mockPrismicMigrationAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import type { TestContext } from "vitest"

import { rest } from "msw"

import { createRepositoryName } from "./createRepositoryName"

import type {
PostDocumentParams,
PostDocumentResult,
PutDocumentParams,
PutDocumentResult,
} from "../../src/types/api/migration/document"

type MockPrismicMigrationAPIV2Args = {
ctx: TestContext
writeToken: string
migrationAPIKey: string
expectedID?: string
}

export const mockPrismicRestAPIV2 = (
args: MockPrismicMigrationAPIV2Args,
): void => {
const repositoryName = createRepositoryName()
const migrationAPIEndpoint = `https://migration.prismic.io`

args.ctx.server.use(
rest.post(`${migrationAPIEndpoint}/documents`, async (req, res, ctx) => {
if (
req.headers.get("authorization") !== `Bearer ${args.writeToken}` ||
req.headers.get("x-apy-key") !== args.migrationAPIKey ||
req.headers.get("repository") !== repositoryName
) {
return res(ctx.status(401))
}

const id = args.expectedID || args.ctx.mock.value.document().id
const body = await req.json<PostDocumentParams>()

const response: PostDocumentResult = {
title: body.title,
id,
lang: body.lang,
type: body.type,
uid: body.uid,
}

return res(ctx.status(201), ctx.json(response))
}),
)

args.ctx.server.use(
rest.put(`${migrationAPIEndpoint}/documents/:id`, async (req, res, ctx) => {
if (
req.headers.get("authorization") !== `Bearer ${args.writeToken}` ||
req.headers.get("x-apy-key") !== args.migrationAPIKey ||
req.headers.get("repository") !== repositoryName
) {
return res(ctx.status(401))
}

if (req.params.id !== args.expectedID) {
return res(ctx.status(404))
}

const document = args.ctx.mock.value.document()
const id = args.expectedID || document.id
const body = await req.json<PutDocumentParams>()

const response: PutDocumentResult = {
title: body.title || args.ctx.mock.value.keyText({ state: "filled" }),
id,
lang: document.lang,
type: document.type,
uid: body.uid,
}

return res(ctx.json(response))
}),
)
}
12 changes: 12 additions & 0 deletions test/writeClient-migrate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect, it, vi } from "vitest"

Check failure on line 1 in test/writeClient-migrate.test.ts

View workflow job for this annotation

GitHub Actions / Suite (ubuntu-latest, Node 16)

'vi' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 1 in test/writeClient-migrate.test.ts

View workflow job for this annotation

GitHub Actions / Suite (ubuntu-latest, Node 18)

'vi' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 1 in test/writeClient-migrate.test.ts

View workflow job for this annotation

GitHub Actions / Suite (ubuntu-latest, Node 20)

'vi' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 1 in test/writeClient-migrate.test.ts

View workflow job for this annotation

GitHub Actions / Suite (ubuntu-latest, Node 22)

'vi' is defined but never used. Allowed unused vars must match /^_/u

import * as prismic from "../src"

it("createWriteClient creates a write client", () => {
const client = prismic.createWriteClient("qwerty", {
writeToken: "xxx",
migrationAPIKey: "yyy",
})

expect(client).toBeInstanceOf(prismic.WriteClient)
})

0 comments on commit 69f3c2f

Please sign in to comment.