From 143943bbedf589660f09046291daca5a124fbee9 Mon Sep 17 00:00:00 2001 From: lihbr Date: Wed, 11 Sep 2024 11:42:27 +0200 Subject: [PATCH] refactor: per review (API, tests) --- src/Migration.ts | 12 +- src/WriteClient.ts | 123 +- src/createClient.ts | 3 +- src/createMigration.ts | 3 +- src/createWriteClient.ts | 3 +- src/getRepositoryName.ts | 22 +- src/lib/isAssetTagID.ts | 15 - src/lib/validateAssetMetadata.ts | 6 +- ...teClient-migrate-patch-image.test.ts.snap} | 0 ...iteClient-migrate-patch-link.test.ts.snap} | 0 ...nt-migrate-patch-linkToMedia.test.ts.snap} | 0 ...migrateUpdateDocuments-images.test.ts.snap | 1800 ----------------- test/__testutils__/mockPrismicAssetAPI.ts | 13 - test/__testutils__/mockPrismicMigrationAPI.ts | 3 + test/getRepositoryName.test.ts | 15 +- test/migration-createAsset.test.ts | 27 +- test/migration-createDocument.test.ts | 20 +- test/writeClient-createAsset.test.ts | 21 +- test/writeClient-getAssets.test.ts | 222 +- ....ts => writeClient-migrate-assets.test.ts} | 54 +- ... => writeClient-migrate-documents.test.ts} | 0 ...> writeClient-migrate-patch-image.test.ts} | 0 ...=> writeClient-migrate-patch-link.test.ts} | 0 ...eClient-migrate-patch-linkToMedia.test.ts} | 0 ...eClient-migrate-patch-simpleField.test.ts} | 0 test/writeClient-migrate.test.ts | 108 +- test/writeClient.test.ts | 2 +- 27 files changed, 397 insertions(+), 2075 deletions(-) delete mode 100644 src/lib/isAssetTagID.ts rename test/__snapshots__/{writeClient-migrateUpdateDocuments-image.test.ts.snap => writeClient-migrate-patch-image.test.ts.snap} (100%) rename test/__snapshots__/{writeClient-migrateUpdateDocuments-link.test.ts.snap => writeClient-migrate-patch-link.test.ts.snap} (100%) rename test/__snapshots__/{writeClient-migrateUpdateDocuments-linkToMedia.test.ts.snap => writeClient-migrate-patch-linkToMedia.test.ts.snap} (100%) delete mode 100644 test/__snapshots__/writeClient-migrateUpdateDocuments-images.test.ts.snap rename test/{writeClient-migrateCreateAssets.test.ts => writeClient-migrate-assets.test.ts} (88%) rename test/{writeClient-migrateCreateDocuments.test.ts => writeClient-migrate-documents.test.ts} (100%) rename test/{writeClient-migrateUpdateDocuments-image.test.ts => writeClient-migrate-patch-image.test.ts} (100%) rename test/{writeClient-migrateUpdateDocuments-link.test.ts => writeClient-migrate-patch-link.test.ts} (100%) rename test/{writeClient-migrateUpdateDocuments-linkToMedia.test.ts => writeClient-migrate-patch-linkToMedia.test.ts} (100%) rename test/{writeClient-migrateUpdateDocuments-simpleField.test.ts => writeClient-migrate-patch-simpleField.test.ts} (100%) diff --git a/src/Migration.ts b/src/Migration.ts index 648e42d2..35b989b1 100644 --- a/src/Migration.ts +++ b/src/Migration.ts @@ -134,7 +134,7 @@ export class Migration< /** * @internal */ - documents: { + _documents: { document: TMigrationDocuments params: PrismicMigrationDocumentParams }[] = [] @@ -143,7 +143,7 @@ export class Migration< /** * @internal */ - assets: Map = new Map() + _assets: Map = new Map() createAsset( asset: Asset | FilledImageFieldImage | FilledLinkToMediaField, @@ -226,11 +226,11 @@ export class Migration< validateAssetMetadata(asset) - const maybeAsset = this.assets.get(asset.id) + const maybeAsset = this._assets.get(asset.id) if (maybeAsset) { // Consolidate existing asset with new asset value if possible - this.assets.set(asset.id, { + this._assets.set(asset.id, { ...maybeAsset, notes: asset.notes || maybeAsset.notes, credits: asset.credits || maybeAsset.credits, @@ -240,7 +240,7 @@ export class Migration< ), }) } else { - this.assets.set(asset.id, asset) + this._assets.set(asset.id, asset) } return { @@ -262,7 +262,7 @@ export class Migration< documentTitle: PrismicMigrationDocumentParams["documentTitle"], params: Omit = {}, ): ExtractMigrationDocumentType { - this.documents.push({ + this._documents.push({ document, params: { documentTitle, ...params }, }) diff --git a/src/WriteClient.ts b/src/WriteClient.ts index 25b07a3c..be5934e8 100644 --- a/src/WriteClient.ts +++ b/src/WriteClient.ts @@ -1,5 +1,4 @@ import { devMsg } from "./lib/devMsg" -import { isAssetTagID } from "./lib/isAssetTagID" import { pLimit } from "./lib/pLimit" import type { AssetMap, DocumentMap } from "./lib/patchMigrationDocumentData" import { patchMigrationDocumentData } from "./lib/patchMigrationDocumentData" @@ -232,20 +231,11 @@ export type WriteClientConfig = { writeToken: string /** - * An explicit Prismic Migration API key that allows working with the - * Migration API. If none is provided, the client will pick a random one to - * authenticate your requests. + * A Prismic Migration API key that allows working with the Migration API. * * @remarks - * These keys are the same for all Prismic users. They are only useful while - * the Migration API is in beta to reduce load. It should be one of: - * - * - `cSaZlfkQlF9C6CEAM2Del6MNX9WonlV86HPbeEJL` - * - `pZCexCajUQ4jriYwIGSxA1drZrFxDyFf1S0D1K0P` - * - `Yc0mfrkGDw8gaaGKTrzwC3QUZDajv6k73DA99vWN` - * - `ySzSEbVMAb5S1oSCQfbVG4mbh9Cb8wlF7BCvKI0L` - * - `g2DA3EKWvx8uxVYcNFrmT5nJpon1Vi9V4XcOibJD` - * - `CCNIlI0Vz41J66oFwsHUXaZa6NYFIY6z7aDF62Bc` + * If no key is provided, the client will use one of the demo key available + * which has stricter rate limiting rules enforced. */ migrationAPIKey?: string @@ -345,8 +335,8 @@ export class WriteClient< type: "start", data: { pending: { - documents: migration.documents.length, - assets: migration.assets.size, + documents: migration._documents.length, + assets: migration._assets.size, }, }, }) @@ -361,8 +351,8 @@ export class WriteClient< type: "end", data: { migrated: { - documents: migration.documents.length, - assets: migration.assets.size, + documents: migration._documents.length, + assets: migration._assets.size, }, }, }) @@ -414,7 +404,7 @@ export class WriteClient< // Create assets let i = 0 let created = 0 - for (const [_, migrationAsset] of migration.assets) { + for (const [_, migrationAsset] of migration._assets) { if ( typeof migrationAsset.id === "string" && assets.has(migrationAsset.id) @@ -424,8 +414,8 @@ export class WriteClient< data: { reason: "already exists", current: ++i, - remaining: migration.assets.size - i, - total: migration.assets.size, + remaining: migration._assets.size - i, + total: migration._assets.size, asset: migrationAsset, }, }) @@ -435,8 +425,8 @@ export class WriteClient< type: "assets:creating", data: { current: ++i, - remaining: migration.assets.size - i, - total: migration.assets.size, + remaining: migration._assets.size - i, + total: migration._assets.size, asset: migrationAsset, }, }) @@ -538,7 +528,7 @@ export class WriteClient< // We create an array with non-master locale documents last because // we need their master locale document to be created first. - for (const { document, params } of migration.documents) { + for (const { document, params } of migration._documents) { if (document.lang === masterLocale) { sortedDocuments.unshift({ document, params }) } else { @@ -660,13 +650,13 @@ export class WriteClient< }: { reporter?: (event: MigrateReporterEvents) => void } & FetchParams = {}, ): Promise { let i = 0 - for (const { document, params } of migration.documents) { + for (const { document, params } of migration._documents) { reporter?.({ type: "documents:updating", data: { current: ++i, - remaining: migration.documents.length - i, - total: migration.documents.length, + remaining: migration._documents.length - i, + total: migration._documents.length, document, documentParams: params, }, @@ -691,7 +681,7 @@ export class WriteClient< reporter?.({ type: "documents:updated", data: { - updated: migration.documents.length, + updated: migration._documents.length, }, }) } @@ -703,19 +693,20 @@ export class WriteClient< * * @returns A paginated response containing the result of the query. */ - async getAssets({ + private async getAssets({ pageSize, cursor, - assetType, - keyword, - ids, - tags, + // assetType, + // keyword, + // ids, + // tags, ...params - }: GetAssetsParams & FetchParams = {}): Promise { + }: Pick & + FetchParams = {}): Promise { // Resolve tags if any - if (tags && tags.length) { - tags = await this.resolveAssetTagIDs(tags, params) - } + // if (tags && tags.length) { + // tags = await this.resolveAssetTagIDs(tags, params) + // } const url = new URL("assets", this.assetAPIEndpoint) @@ -727,26 +718,26 @@ export class WriteClient< url.searchParams.set("cursor", cursor) } - if (assetType) { - url.searchParams.set("assetType", assetType) - } + // if (assetType) { + // url.searchParams.set("assetType", assetType) + // } - if (keyword) { - url.searchParams.set("keyword", keyword) - } + // if (keyword) { + // url.searchParams.set("keyword", keyword) + // } - if (ids) { - ids.forEach((id) => url.searchParams.append("ids", id)) - } + // if (ids) { + // ids.forEach((id) => url.searchParams.append("ids", id)) + // } - if (tags) { - tags.forEach((tag) => url.searchParams.append("tags", tag)) - } + // if (tags) { + // tags.forEach((tag) => url.searchParams.append("tags", tag)) + // } const { items, total, - missing_ids, + // missing_ids, cursor: nextCursor, } = await this.fetch( url.toString(), @@ -756,16 +747,16 @@ export class WriteClient< return { results: items, total_results_size: total, - missing_ids: missing_ids || [], + // missing_ids: missing_ids || [], next: nextCursor ? () => this.getAssets({ pageSize, cursor: nextCursor, - assetType, - keyword, - ids, - tags, + // assetType, + // keyword, + // ids, + // tags, ...params, }) : undefined, @@ -965,16 +956,16 @@ export class WriteClient< private _resolveAssetTagIDsLimit = pLimit() /** - * Resolves asset tag IDs from tag names or IDs. + * Resolves asset tag IDs from tag names. * - * @param tagNamesOrIDs - An array of tag names or IDs to resolve. + * @param tagNames - An array of tag names to resolve. * @param params - Whether or not missing tags should be created and * additional fetch parameters. * * @returns An array of resolved tag IDs. */ private async resolveAssetTagIDs( - tagNamesOrIDs: string[] = [], + tagNames: string[] = [], { createTags, ...params }: { createTags?: boolean } & FetchParams = {}, ): Promise { return this._resolveAssetTagIDsLimit(async () => { @@ -985,23 +976,15 @@ export class WriteClient< } const resolvedTagIDs = [] - for (const tagNameOrID of tagNamesOrIDs) { - if (isAssetTagID(tagNameOrID)) { - resolvedTagIDs.push(tagNameOrID) - continue - } - + for (const tagName of tagNames) { // Tag does not exists yet, we create it if `createTags` is set - if (!existingTagMap[tagNameOrID] && createTags) { - existingTagMap[tagNameOrID] = await this.createAssetTag( - tagNameOrID, - params, - ) + if (!existingTagMap[tagName] && createTags) { + existingTagMap[tagName] = await this.createAssetTag(tagName, params) } // Add tag if found - if (existingTagMap[tagNameOrID]) { - resolvedTagIDs.push(existingTagMap[tagNameOrID].id) + if (existingTagMap[tagName]) { + resolvedTagIDs.push(existingTagMap[tagName].id) } } diff --git a/src/createClient.ts b/src/createClient.ts index 4d64b703..a3c16f9c 100644 --- a/src/createClient.ts +++ b/src/createClient.ts @@ -26,8 +26,7 @@ export interface CreateClient { * createClient("https://qwerty.cdn.prismic.io/api/v2") * ``` * - * @typeParam TDocuments - A map of Prismic document type IDs mapped to their - * TypeScript type. + * @typeParam TDocuments - A union of Prismic document types for the repository. * * @param repositoryNameOrEndpoint - The Prismic repository name or full Rest * API V2 endpoint for the repository. diff --git a/src/createMigration.ts b/src/createMigration.ts index 3fc63125..b12df736 100644 --- a/src/createMigration.ts +++ b/src/createMigration.ts @@ -22,8 +22,7 @@ export interface CreateMigration { * createMigration() * ``` * - * @typeParam TDocuments - A map of Prismic document type IDs mapped to their - * TypeScript type. + * @typeParam TDocuments - A union of Prismic document types for the repository. * * @returns A migration instance to prepare your migration. */ diff --git a/src/createWriteClient.ts b/src/createWriteClient.ts index f1e61a95..d713050f 100644 --- a/src/createWriteClient.ts +++ b/src/createWriteClient.ts @@ -27,8 +27,7 @@ export interface CreateWriteClient { * createWriteClient("qwerty", { writeToken: "***" }) * ``` * - * @typeParam TDocuments - A map of Prismic document type IDs mapped to their - * TypeScript type. + * @typeParam TDocuments - A union of Prismic document types for the repository. * * @param repositoryName - The Prismic repository name for the repository. * @param options - Configuration that determines how content will be queried diff --git a/src/getRepositoryName.ts b/src/getRepositoryName.ts index a78c19f9..74d04ce8 100644 --- a/src/getRepositoryName.ts +++ b/src/getRepositoryName.ts @@ -1,31 +1,31 @@ import { PrismicError } from "./errors/PrismicError" /** - * Get a Prismic repository's name from its standard Prismic Rest API V2 or + * Get a Prismic repository's name from its standard Prismic Document API or * GraphQL endpoint. * - * @typeParam RepositoryEndpoint - Prismic Rest API V2 endpoint for the - * repository. - * - * @param repositoryEndpoint - Prismic Rest API V2 endpoint for the repository. + * @param repositoryEndpoint - Prismic Document API endpoint for the repository. * * @returns The Prismic repository's name. * - * @throws {@link Error} Thrown if an invalid Prismic Rest API V2 endpoint is + * @throws {@link Error} Thrown if an invalid Prismic Document API endpoint is * provided. */ export const getRepositoryName = (repositoryEndpoint: string): string => { try { - const parts = new URL(repositoryEndpoint).hostname.split(".") + const hostname = new URL(repositoryEndpoint).hostname - // [subdomain, domain, tld] - if (parts.length > 2) { - return parts[0] + if ( + hostname.endsWith("prismic.io") || // Production + hostname.endsWith("wroom.io") || // Staging + hostname.endsWith("wroom.test") // Dev + ) { + return hostname.split(".")[0] } } catch {} throw new PrismicError( - `An invalid Prismic Rest API V2 endpoint was provided: ${repositoryEndpoint}`, + `An invalid Prismic Document API endpoint was provided: ${repositoryEndpoint}`, undefined, undefined, ) diff --git a/src/lib/isAssetTagID.ts b/src/lib/isAssetTagID.ts deleted file mode 100644 index ef4d5234..00000000 --- a/src/lib/isAssetTagID.ts +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Checks if a string is an asset tag ID. - * - * @param maybeAssetTagID - A string that's maybe an asset tag ID. - * - * @returns `true` if the string is an asset tag ID, `false` otherwise. - */ -export const isAssetTagID = (maybeAssetTagID: string): boolean => { - // Taken from @sinclair/typebox which is the uuid type checker of the Asset API - // See: https://github.com/sinclairzx81/typebox/blob/e36f5658e3a56d8c32a711aa616ec8bb34ca14b4/test/runtime/compiler/validate.ts#L15 - // Tag is already a tag ID - return /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i.test( - maybeAssetTagID, - ) -} diff --git a/src/lib/validateAssetMetadata.ts b/src/lib/validateAssetMetadata.ts index e5ee272b..19720f8d 100644 --- a/src/lib/validateAssetMetadata.ts +++ b/src/lib/validateAssetMetadata.ts @@ -2,8 +2,6 @@ import { PrismicError } from "../errors/PrismicError" import type { CreateAssetParams } from "../WriteClient" -import { isAssetTagID } from "./isAssetTagID" - /** * Max length for asset notes accepted by the API. */ @@ -66,9 +64,7 @@ export const validateAssetMetadata = ({ tags.length && tags.some( (tag) => - !isAssetTagID(tag) && - (tag.length < ASSET_TAG_MIN_LENGTH || - tag.length > ASSET_TAG_MAX_LENGTH), + tag.length < ASSET_TAG_MIN_LENGTH || tag.length > ASSET_TAG_MAX_LENGTH, ) ) { errors.push( diff --git a/test/__snapshots__/writeClient-migrateUpdateDocuments-image.test.ts.snap b/test/__snapshots__/writeClient-migrate-patch-image.test.ts.snap similarity index 100% rename from test/__snapshots__/writeClient-migrateUpdateDocuments-image.test.ts.snap rename to test/__snapshots__/writeClient-migrate-patch-image.test.ts.snap diff --git a/test/__snapshots__/writeClient-migrateUpdateDocuments-link.test.ts.snap b/test/__snapshots__/writeClient-migrate-patch-link.test.ts.snap similarity index 100% rename from test/__snapshots__/writeClient-migrateUpdateDocuments-link.test.ts.snap rename to test/__snapshots__/writeClient-migrate-patch-link.test.ts.snap diff --git a/test/__snapshots__/writeClient-migrateUpdateDocuments-linkToMedia.test.ts.snap b/test/__snapshots__/writeClient-migrate-patch-linkToMedia.test.ts.snap similarity index 100% rename from test/__snapshots__/writeClient-migrateUpdateDocuments-linkToMedia.test.ts.snap rename to test/__snapshots__/writeClient-migrate-patch-linkToMedia.test.ts.snap diff --git a/test/__snapshots__/writeClient-migrateUpdateDocuments-images.test.ts.snap b/test/__snapshots__/writeClient-migrateUpdateDocuments-images.test.ts.snap deleted file mode 100644 index daa3e7f0..00000000 --- a/test/__snapshots__/writeClient-migrateUpdateDocuments-images.test.ts.snap +++ /dev/null @@ -1,1800 +0,0 @@ -// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html - -exports[`patches image fields > existing > group 1`] = ` -{ - "group": [ - { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "url": "https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?w=2560&h=1440&fit=crop", - }, - }, - ], -} -`; - -exports[`patches image fields > existing > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - }, - }, - ], - "primary": { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - }, - "group": [ - { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - }, - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches image fields > existing > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", - }, - }, - ], - "primary": { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", - }, - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches image fields > existing > static zone 1`] = ` -{ - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?w=4335&h=6502&fit=crop", - }, -} -`; - -exports[`patches image fields > new > group 1`] = ` -{ - "group": [ - { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "ef95d5daa4d", - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?w=2560&h=1705&fit=crop", - }, - }, - ], -} -`; - -exports[`patches image fields > new > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "f2c3f8bfc90", - "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", - }, - }, - ], - "primary": { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "f2c3f8bfc90", - "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", - }, - "group": [ - { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "f2c3f8bfc90", - "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", - }, - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches image fields > new > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "45306297c5e", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - }, - }, - ], - "primary": { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "45306297c5e", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - }, - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches image fields > new > static zone 1`] = ` -{ - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "c5c95f8d3ac", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - }, -} -`; - -exports[`patches image fields > otherRepository > group 1`] = ` -{ - "group": [ - { - "field": { - "alt": "Odio ut enim blandit volutpat maecenas volutpat blandit", - "copyright": null, - "dimensions": { - "height": 1705, - "width": 2560, - }, - "edit": { - "background": "#fdb338", - "x": 349, - "y": 115, - "zoom": 1.5951464943576479, - }, - "id": "4dcf07cfc26", - "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b", - }, - }, - ], -} -`; - -exports[`patches image fields > otherRepository > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": { - "alt": "Vitae et leo duis ut diam quam nulla porttitor massa id neque aliquam vestibulum", - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#61adcf", - "x": -1586, - "y": -2819, - "zoom": 1.9393723758262054, - }, - "id": "f5dbf0d9ed2", - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e", - }, - }, - ], - "primary": { - "field": { - "alt": "Ut consequat semper viverra nam libero justo laoreet", - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#8efd5a", - "x": 466, - "y": -571, - "zoom": 1.394663850868741, - }, - "id": "f5dbf0d9ed2", - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e", - }, - "group": [ - { - "field": { - "alt": "Porttitor massa id neque aliquam vestibulum morbi blandit", - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#bc3178", - "x": 1156, - "y": -2223, - "zoom": 1.602826201962965, - }, - "id": "f5dbf0d9ed2", - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e", - }, - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches image fields > otherRepository > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": { - "alt": "Vitae sapien pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis", - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#c361cc", - "x": -751, - "y": -404, - "zoom": 1.6465649620272602, - }, - "id": "4f4a69ff72d", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c", - }, - }, - ], - "primary": { - "field": { - "alt": "Nunc pulvinar sapien et ligula ullamcorper malesuada proin libero nunc consequat interdum varius sit", - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#55e3be", - "x": -762, - "y": 991, - "zoom": 1.0207384987782544, - }, - "id": "4f4a69ff72d", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c", - }, - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches image fields > otherRepository > static zone 1`] = ` -{ - "field": { - "alt": "Vulputate sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum curabitur vitae nunc sed", - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#97acc6", - "x": 1922, - "y": -967, - "zoom": 1.9765406541937358, - }, - "id": "9abffab1d17", - "url": "https://images.unsplash.com/photo-1446329813274-7c9036bd9a1f", - }, -} -`; - -exports[`patches image fields > otherRepositoryEmpty > group 1`] = ` -{ - "group": [ - { - "field": {}, - }, - ], -} -`; - -exports[`patches image fields > otherRepositoryEmpty > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": {}, - }, - ], - "primary": { - "field": {}, - "group": [ - { - "field": {}, - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches image fields > otherRepositoryEmpty > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": {}, - }, - ], - "primary": { - "field": {}, - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches image fields > otherRepositoryEmpty > static zone 1`] = ` -{ - "field": {}, -} -`; - -exports[`patches image fields > otherRepositoryWithThumbnails > group 1`] = ` -{ - "group": [ - { - "field": { - "alt": "Odio ut enim blandit volutpat maecenas volutpat blandit", - "copyright": null, - "dimensions": { - "height": 1705, - "width": 2560, - }, - "edit": { - "background": "#fdb338", - "x": 349, - "y": 115, - "zoom": 1.5951464943576479, - }, - "id": "4dcf07cfc26", - "square": { - "alt": "Odio ut enim blandit volutpat maecenas volutpat blandit", - "copyright": null, - "dimensions": { - "height": 1705, - "width": 2560, - }, - "edit": { - "background": "#fdb338", - "x": 349, - "y": 115, - "zoom": 1.5951464943576479, - }, - "id": "4dcf07cfc26", - "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?some=query", - }, - }, - ], -} -`; - -exports[`patches image fields > otherRepositoryWithThumbnails > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": { - "alt": "Vitae et leo duis ut diam quam nulla porttitor massa id neque aliquam vestibulum", - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#61adcf", - "x": -1586, - "y": -2819, - "zoom": 1.9393723758262054, - }, - "id": "f5dbf0d9ed2", - "square": { - "alt": "Vitae et leo duis ut diam quam nulla porttitor massa id neque aliquam vestibulum", - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#61adcf", - "x": -1586, - "y": -2819, - "zoom": 1.9393723758262054, - }, - "id": "f5dbf0d9ed2", - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=query", - }, - }, - ], - "primary": { - "field": { - "alt": "Ut consequat semper viverra nam libero justo laoreet", - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#8efd5a", - "x": 466, - "y": -571, - "zoom": 1.394663850868741, - }, - "id": "f5dbf0d9ed2", - "square": { - "alt": "Ut consequat semper viverra nam libero justo laoreet", - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#8efd5a", - "x": 466, - "y": -571, - "zoom": 1.394663850868741, - }, - "id": "f5dbf0d9ed2", - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=query", - }, - "group": [ - { - "field": { - "alt": "Porttitor massa id neque aliquam vestibulum morbi blandit", - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#bc3178", - "x": 1156, - "y": -2223, - "zoom": 1.602826201962965, - }, - "id": "f5dbf0d9ed2", - "square": { - "alt": "Porttitor massa id neque aliquam vestibulum morbi blandit", - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#bc3178", - "x": 1156, - "y": -2223, - "zoom": 1.602826201962965, - }, - "id": "f5dbf0d9ed2", - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=query", - }, - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches image fields > otherRepositoryWithThumbnails > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": { - "alt": "Vitae sapien pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis", - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#c361cc", - "x": -751, - "y": -404, - "zoom": 1.6465649620272602, - }, - "id": "4f4a69ff72d", - "square": { - "alt": "Vitae sapien pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis", - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#c361cc", - "x": -751, - "y": -404, - "zoom": 1.6465649620272602, - }, - "id": "4f4a69ff72d", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?some=query", - }, - }, - ], - "primary": { - "field": { - "alt": "Nunc pulvinar sapien et ligula ullamcorper malesuada proin libero nunc consequat interdum varius sit", - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#55e3be", - "x": -762, - "y": 991, - "zoom": 1.0207384987782544, - }, - "id": "4f4a69ff72d", - "square": { - "alt": "Nunc pulvinar sapien et ligula ullamcorper malesuada proin libero nunc consequat interdum varius sit", - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#55e3be", - "x": -762, - "y": 991, - "zoom": 1.0207384987782544, - }, - "id": "4f4a69ff72d", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?some=query", - }, - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches image fields > otherRepositoryWithThumbnails > static zone 1`] = ` -{ - "field": { - "alt": "Vulputate sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum curabitur vitae nunc sed", - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#97acc6", - "x": 1922, - "y": -967, - "zoom": 1.9765406541937358, - }, - "id": "9abffab1d17", - "square": { - "alt": "Vulputate sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum curabitur vitae nunc sed", - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#97acc6", - "x": 1922, - "y": -967, - "zoom": 1.9765406541937358, - }, - "id": "9abffab1d17", - "url": "https://images.unsplash.com/photo-1446329813274-7c9036bd9a1f?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1446329813274-7c9036bd9a1f?some=query", - }, -} -`; - -exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > group 1`] = ` -{ - "group": [ - { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1705, - "width": 2560, - }, - "edit": { - "background": "#fdb338", - "x": 349, - "y": 115, - "zoom": 1.5951464943576479, - }, - "id": "4dcf07cfc26", - "square": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1705, - "width": 2560, - }, - "edit": { - "background": "#fdb338", - "x": 349, - "y": 115, - "zoom": 1.5951464943576479, - }, - "id": "4dcf07cfc26", - "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?some=query", - }, - }, - ], -} -`; - -exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#61adcf", - "x": -1586, - "y": -2819, - "zoom": 1.9393723758262054, - }, - "id": "f5dbf0d9ed2", - "square": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#61adcf", - "x": -1586, - "y": -2819, - "zoom": 1.9393723758262054, - }, - "id": "f5dbf0d9ed2", - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=query", - }, - }, - ], - "primary": { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#8efd5a", - "x": 466, - "y": -571, - "zoom": 1.394663850868741, - }, - "id": "f5dbf0d9ed2", - "square": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#8efd5a", - "x": 466, - "y": -571, - "zoom": 1.394663850868741, - }, - "id": "f5dbf0d9ed2", - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=query", - }, - "group": [ - { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#bc3178", - "x": 1156, - "y": -2223, - "zoom": 1.602826201962965, - }, - "id": "f5dbf0d9ed2", - "square": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#bc3178", - "x": 1156, - "y": -2223, - "zoom": 1.602826201962965, - }, - "id": "f5dbf0d9ed2", - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?some=query", - }, - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#c361cc", - "x": -751, - "y": -404, - "zoom": 1.6465649620272602, - }, - "id": "4f4a69ff72d", - "square": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#c361cc", - "x": -751, - "y": -404, - "zoom": 1.6465649620272602, - }, - "id": "4f4a69ff72d", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?some=query", - }, - }, - ], - "primary": { - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#55e3be", - "x": -762, - "y": 991, - "zoom": 1.0207384987782544, - }, - "id": "4f4a69ff72d", - "square": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#55e3be", - "x": -762, - "y": 991, - "zoom": 1.0207384987782544, - }, - "id": "4f4a69ff72d", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?some=query", - }, - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches image fields > otherRepositoryWithThumbnailsNoAlt > static zone 1`] = ` -{ - "field": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#97acc6", - "x": 1922, - "y": -967, - "zoom": 1.9765406541937358, - }, - "id": "9abffab1d17", - "square": { - "alt": null, - "copyright": null, - "dimensions": { - "height": 2832, - "width": 4240, - }, - "edit": { - "background": "#97acc6", - "x": 1922, - "y": -967, - "zoom": 1.9765406541937358, - }, - "id": "9abffab1d17", - "url": "https://images.unsplash.com/photo-1446329813274-7c9036bd9a1f?some=other&query=params", - }, - "url": "https://images.unsplash.com/photo-1446329813274-7c9036bd9a1f?some=query", - }, -} -`; - -exports[`patches image fields > richTextExisting > group 1`] = ` -{ - "group": [ - { - "field": [ - { - "spans": [], - "text": "Nulla Aliquet Porttitor Lacus Luctus", - "type": "heading2", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?w=2560&h=1440&fit=crop", - }, - ], - }, - ], -} -`; - -exports[`patches image fields > richTextExisting > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Eu Mi", - "type": "heading3", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", - "type": "heading3", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - }, - ], - "group": [ - { - "field": [ - { - "spans": [], - "text": "Arcu", - "type": "heading1", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - }, - ], - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches image fields > richTextExisting > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Habitasse platea dictumst quisque sagittis purus sit", - "type": "o-list-item", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Ac Orci Phasellus Egestas Tellus", - "type": "heading5", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=4240&h=2832&fit=crop", - }, - ], - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches image fields > richTextExisting > static zone 1`] = ` -{ - "field": [ - { - "spans": [], - "text": "Elementum Integer", - "type": "heading6", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "id-existing", - "type": "image", - "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b?w=4335&h=6502&fit=crop", - }, - ], -} -`; - -exports[`patches image fields > richTextNew > group 1`] = ` -{ - "group": [ - { - "field": [ - { - "spans": [], - "text": "Nulla Aliquet Porttitor Lacus Luctus", - "type": "heading2", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "cdfdd322ca9", - "type": "image", - "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c?w=6000&h=4000&fit=crop", - }, - ], - }, - ], -} -`; - -exports[`patches image fields > richTextNew > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Eu Mi", - "type": "heading3", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "bc31787853a", - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", - "type": "heading3", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "bc31787853a", - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - "group": [ - { - "field": [ - { - "spans": [], - "text": "Arcu", - "type": "heading1", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "bc31787853a", - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches image fields > richTextNew > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Habitasse platea dictumst quisque sagittis purus sit", - "type": "o-list-item", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "7963dc361cc", - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Ac Orci Phasellus Egestas Tellus", - "type": "heading5", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "7963dc361cc", - "type": "image", - "url": "https://images.unsplash.com/photo-1470770903676-69b98201ea1c?w=4554&h=3036&fit=crop", - }, - ], - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches image fields > richTextNew > static zone 1`] = ` -{ - "field": [ - { - "spans": [], - "text": "Elementum Integer", - "type": "heading6", - }, - { - "alt": null, - "copyright": null, - "dimensions": { - "height": 1, - "width": 1, - }, - "edit": { - "background": "transparent", - "x": 0, - "y": 0, - "zoom": 0, - }, - "id": "0bbad670dad", - "type": "image", - "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", - }, - ], -} -`; - -exports[`patches image fields > richTextOtherRepository > group 1`] = ` -{ - "group": [ - { - "field": [ - { - "spans": [], - "text": "Nulla Aliquet Porttitor Lacus Luctus", - "type": "heading2", - }, - { - "alt": "Donec enim diam vulputate ut pharetra sit amet aliquam id diam", - "copyright": null, - "dimensions": { - "height": 4000, - "width": 6000, - }, - "edit": { - "background": "#7cfc26", - "x": 2977, - "y": -1163, - "zoom": 1.0472585898934068, - }, - "id": "e8d0985c099", - "type": "image", - "url": "https://images.unsplash.com/photo-1553531384-397c80973a0b", - }, - ], - }, - ], -} -`; - -exports[`patches image fields > richTextOtherRepository > shared slice 1`] = ` -{ - "slices": [ - { - "id": "2ff58c741ba", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Facilisis", - "type": "heading3", - }, - { - "alt": "Nullam vehicula ipsum a arcu cursus vitae congue mauris rhoncus aenean vel elit scelerisque", - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#37ae3f", - "x": -1505, - "y": 902, - "zoom": 1.8328975606320652, - }, - "id": "3fc0dfa9fe9", - "type": "image", - "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Fringilla Phasellus Faucibus Scelerisque Eleifend Donec Pretium", - "type": "heading3", - }, - { - "alt": "Proin libero nunc consequat interdum varius", - "copyright": null, - "dimensions": { - "height": 3036, - "width": 4554, - }, - "edit": { - "background": "#904f4f", - "x": 1462, - "y": 1324, - "zoom": 1.504938844941775, - }, - "id": "3fc0dfa9fe9", - "type": "image", - "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", - }, - ], - "group": [ - { - "field": [ - { - "spans": [], - "text": "Egestas Integer Eget Aliquet Nibh", - "type": "heading5", - }, - { - "alt": "Arcu cursus vitae congue mauris", - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#5f7a9b", - "x": -119, - "y": -2667, - "zoom": 1.9681315715350518, - }, - "id": "3fc0dfa9fe9", - "type": "image", - "url": "https://images.unsplash.com/photo-1431794062232-2a99a5431c6c", - }, - ], - }, - ], - }, - "slice_label": null, - "slice_type": "at", - "variation": "tortor", - "version": "a79b9dd", - }, - ], -} -`; - -exports[`patches image fields > richTextOtherRepository > slice 1`] = ` -{ - "slices": [ - { - "id": "e93cc3e4f28", - "items": [ - { - "field": [ - { - "spans": [], - "text": "Amet", - "type": "heading5", - }, - { - "alt": "Auctor neque vitae tempus quam", - "copyright": null, - "dimensions": { - "height": 1440, - "width": 2560, - }, - "edit": { - "background": "#5bc5aa", - "x": -1072, - "y": -281, - "zoom": 1.3767766101231744, - }, - "id": "f70ca27104d", - "type": "image", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff", - }, - ], - }, - ], - "primary": { - "field": [ - { - "spans": [], - "text": "Ac Orci Phasellus Egestas Tellus", - "type": "heading5", - }, - { - "alt": "Urna id volutpat lacus laoreet non curabitur gravida arcu ac tortor", - "copyright": null, - "dimensions": { - "height": 3036, - "width": 4554, - }, - "edit": { - "background": "#4860cb", - "x": 280, - "y": -379, - "zoom": 1.2389796902982004, - }, - "id": "f70ca27104d", - "type": "image", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff", - }, - ], - }, - "slice_label": "Pellentesque", - "slice_type": "nulla_posuere", - }, - ], -} -`; - -exports[`patches image fields > richTextOtherRepository > static zone 1`] = ` -{ - "field": [ - { - "spans": [], - "text": "Elementum Integer", - "type": "heading6", - }, - { - "alt": "Interdum velit euismod in pellentesque", - "copyright": null, - "dimensions": { - "height": 4392, - "width": 7372, - }, - "edit": { - "background": "#ab1d17", - "x": 3605, - "y": 860, - "zoom": 1.9465488211593005, - }, - "id": "04a95cc61c3", - "type": "image", - "url": "https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5", - }, - ], -} -`; diff --git a/test/__testutils__/mockPrismicAssetAPI.ts b/test/__testutils__/mockPrismicAssetAPI.ts index 23df9904..02187d2b 100644 --- a/test/__testutils__/mockPrismicAssetAPI.ts +++ b/test/__testutils__/mockPrismicAssetAPI.ts @@ -22,7 +22,6 @@ type MockPrismicAssetAPIArgs = { client: WriteClient writeToken?: string requiredHeaders?: Record - requiredGetParams?: Record existingAssets?: Asset[][] | number[] newAssets?: Asset[] existingTags?: AssetTag[] @@ -107,18 +106,6 @@ export const mockPrismicAssetAPI = ( return res(ctx.status(401), ctx.json({ error: "unauthorized" })) } - if (args.requiredGetParams) { - for (const paramKey in args.requiredGetParams) { - const requiredValue = args.requiredGetParams[paramKey] - - args.ctx - .expect(req.url.searchParams.getAll(paramKey)) - .toStrictEqual( - Array.isArray(requiredValue) ? requiredValue : [requiredValue], - ) - } - } - validateHeaders(req) const index = Number.parseInt(req.url.searchParams.get("cursor") ?? "0") diff --git a/test/__testutils__/mockPrismicMigrationAPI.ts b/test/__testutils__/mockPrismicMigrationAPI.ts index f81914cd..25e1d275 100644 --- a/test/__testutils__/mockPrismicMigrationAPI.ts +++ b/test/__testutils__/mockPrismicMigrationAPI.ts @@ -98,6 +98,9 @@ export const mockPrismicMigrationAPI = ( const body = await req.json() + // New document is used when we need to have predictable document ID + // It's also used when we need to test for correct `alternate_language_id` + // because the API doesn't return it in the response. const newDocument = args.newDocuments?.shift() const id = newDocument?.id || args.ctx.mock.value.document().id diff --git a/test/getRepositoryName.test.ts b/test/getRepositoryName.test.ts index 8f97db0a..6341f5e0 100644 --- a/test/getRepositoryName.test.ts +++ b/test/getRepositoryName.test.ts @@ -2,7 +2,7 @@ import { expect, it } from "vitest" import * as prismic from "../src" -it("returns the repository name from a valid Prismic Rest API V2 endpoint", () => { +it("returns the repository name from a valid Prismic Document API endpoint", () => { const repositoryName = prismic.getRepositoryName( "https://qwerty.cdn.prismic.io/api/v2", ) @@ -10,11 +10,22 @@ it("returns the repository name from a valid Prismic Rest API V2 endpoint", () = expect(repositoryName).toBe("qwerty") }) +it("throws if the input is not a valid Document API endpoint", () => { + expect(() => { + prismic.getRepositoryName("https://example.com") + }).toThrowError( + /An invalid Prismic Document API endpoint was provided: https:\/\/example\.com/i, + ) + expect(() => { + prismic.getRepositoryName("https://example.com") + }).toThrowError(prismic.PrismicError) +}) + it("throws if the input is not a valid URL", () => { expect(() => { prismic.getRepositoryName("qwerty") }).toThrowError( - /An invalid Prismic Rest API V2 endpoint was provided: qwerty/i, + /An invalid Prismic Document API endpoint was provided: qwerty/i, ) expect(() => { prismic.getRepositoryName("qwerty") diff --git a/test/migration-createAsset.test.ts b/test/migration-createAsset.test.ts index 16c42ec8..ce3749e5 100644 --- a/test/migration-createAsset.test.ts +++ b/test/migration-createAsset.test.ts @@ -17,7 +17,7 @@ it("creates an asset from a url", () => { migration.createAsset(file, filename) - expect(migration.assets.get(file)).toEqual({ + expect(migration._assets.get(file)).toEqual({ id: file, file, filename, @@ -32,7 +32,7 @@ it("creates an asset from a file", () => { migration.createAsset(file, filename) - expect(migration.assets.get(file)).toEqual({ + expect(migration._assets.get(file)).toEqual({ id: file, file, filename, @@ -69,7 +69,7 @@ it("creates an asset from an existing asset", () => { migration.createAsset(asset) - expect(migration.assets.get(asset.id)).toStrictEqual({ + expect(migration._assets.get(asset.id)).toStrictEqual({ id: asset.id, file: asset.url, filename: asset.filename, @@ -94,7 +94,7 @@ it("creates an asset from an image field", () => { migration.createAsset(image) - expect(migration.assets.get(image.id)).toEqual({ + expect(migration._assets.get(image.id)).toEqual({ id: image.id, file: image.url, filename: image.url.split("/").pop(), @@ -119,7 +119,7 @@ it("creates an asset from a link to media field", () => { migration.createAsset(link) - expect(migration.assets.get(link.id)).toEqual({ + expect(migration._assets.get(link.id)).toEqual({ id: link.id, file: link.url, filename: link.name, @@ -157,14 +157,7 @@ it("throws if asset has invalid metadata", () => { ) expect(() => { - migration.createAsset(file, filename, { - tags: [ - // Tag name - "012", - // Tag ID - "123e4567-e89b-12d3-a456-426614174000", - ], - }) + migration.createAsset(file, filename, { tags: ["012"] }) }, "tags").not.toThrowError() }) @@ -176,7 +169,7 @@ it("consolidates existing assets with additional metadata", () => { migration.createAsset(file, filename) - expect(migration.assets.get(file)).toStrictEqual({ + expect(migration._assets.get(file)).toStrictEqual({ id: file, file, filename, @@ -193,7 +186,7 @@ it("consolidates existing assets with additional metadata", () => { tags: ["tag"], }) - expect(migration.assets.get(file)).toStrictEqual({ + expect(migration._assets.get(file)).toStrictEqual({ id: file, file, filename, @@ -210,7 +203,7 @@ it("consolidates existing assets with additional metadata", () => { tags: ["tag", "tag 2"], }) - expect(migration.assets.get(file)).toStrictEqual({ + expect(migration._assets.get(file)).toStrictEqual({ id: file, file, filename, @@ -222,7 +215,7 @@ it("consolidates existing assets with additional metadata", () => { migration.createAsset(file, filename) - expect(migration.assets.get(file)).toStrictEqual({ + expect(migration._assets.get(file)).toStrictEqual({ id: file, file, filename, diff --git a/test/migration-createDocument.test.ts b/test/migration-createDocument.test.ts index 311bd8f9..df7b98dc 100644 --- a/test/migration-createDocument.test.ts +++ b/test/migration-createDocument.test.ts @@ -18,7 +18,7 @@ it("creates a document", () => { migration.createDocument(document, documentTitle) - expect(migration.documents[0]).toStrictEqual({ + expect(migration._documents[0]).toStrictEqual({ document, params: { documentTitle }, }) @@ -32,7 +32,7 @@ it("creates a document from an existing Prismic document", (ctx) => { migration.createDocument(document, documentTitle) - expect(migration.documents[0]).toStrictEqual({ + expect(migration._documents[0]).toStrictEqual({ document, params: { documentTitle }, }) @@ -193,11 +193,11 @@ describe.each<{ } const documentTitle = "documentTitle" - expect(migration.assets.size).toBe(0) + expect(migration._assets.size).toBe(0) migration.createDocument(document, documentTitle) - expect(migration.assets.get(id)).toStrictEqual(expected) + expect(migration._assets.get(id)).toStrictEqual(expected) }) it("group fields", ({ mock }) => { @@ -213,11 +213,11 @@ describe.each<{ } const documentTitle = "documentTitle" - expect(migration.assets.size).toBe(0) + expect(migration._assets.size).toBe(0) migration.createDocument(document, documentTitle) - expect(migration.assets.get(id)).toStrictEqual(expected) + expect(migration._assets.get(id)).toStrictEqual(expected) }) it("slice's primary zone", ({ mock }) => { @@ -243,11 +243,11 @@ describe.each<{ } const documentTitle = "documentTitle" - expect(migration.assets.size).toBe(0) + expect(migration._assets.size).toBe(0) migration.createDocument(document, documentTitle) - expect(migration.assets.get(id)).toStrictEqual(expected) + expect(migration._assets.get(id)).toStrictEqual(expected) }) it("slice's repeatable zone", ({ mock }) => { @@ -273,10 +273,10 @@ describe.each<{ } const documentTitle = "documentTitle" - expect(migration.assets.size).toBe(0) + expect(migration._assets.size).toBe(0) migration.createDocument(document, documentTitle) - expect(migration.assets.get(id)).toStrictEqual(expected) + expect(migration._assets.get(id)).toStrictEqual(expected) }) }) diff --git a/test/writeClient-createAsset.test.ts b/test/writeClient-createAsset.test.ts index 5e8fb3b1..cc05bdd0 100644 --- a/test/writeClient-createAsset.test.ts +++ b/test/writeClient-createAsset.test.ts @@ -49,29 +49,10 @@ it.concurrent("creates an asset with metadata", async (ctx) => { notes: "baz", }) + // TODO: Check that data are properly passed when we update MSW and get FormData support expect(asset.id).toBeTypeOf("string") }) -it.concurrent("creates an asset with an existing tag ID", async (ctx) => { - const client = createTestWriteClient({ ctx }) - - const tag: AssetTag = { - id: "00000000-4444-4444-4444-121212121212", - name: "foo", - created_at: 0, - last_modified: 0, - } - - mockPrismicAssetAPI({ ctx, client, existingTags: [tag] }) - - // @ts-expect-error - testing purposes - const asset = await client.createAsset("file", "foo.jpg", { - tags: [tag.id], - }) - - expect(asset.tags?.[0].id).toEqual(tag.id) -}) - it.concurrent("creates an asset with an existing tag name", async (ctx) => { const client = createTestWriteClient({ ctx }) diff --git a/test/writeClient-getAssets.test.ts b/test/writeClient-getAssets.test.ts index e16dd854..34186fe4 100644 --- a/test/writeClient-getAssets.test.ts +++ b/test/writeClient-getAssets.test.ts @@ -13,141 +13,139 @@ const it = _it.skipIf(isNode16) it.concurrent("get assets", async (ctx) => { const client = createTestWriteClient({ ctx }) - mockPrismicAssetAPI({ ctx, client }) + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + existingAssets: [2], + }) + // @ts-expect-error - testing purposes const { results } = await client.getAssets() - expect(results).toBeInstanceOf(Array) + expect(results).toStrictEqual(assetsDatabase[0]) }) it.concurrent("supports `pageSize` parameter", async (ctx) => { const client = createTestWriteClient({ ctx }) - mockPrismicAssetAPI({ + const { assetsDatabase } = mockPrismicAssetAPI({ ctx, client, - requiredGetParams: { - pageSize: "10", - }, + existingAssets: [2], }) + ctx.server.events.on("request:start", (req) => { + if (req.url.hostname.startsWith(client.repositoryName)) { + ctx.expect(req.url.searchParams.get("pageSize")).toBe("10") + } + }) + + // @ts-expect-error - testing purposes const { results } = await client.getAssets({ pageSize: 10, }) - ctx.expect(results).toBeInstanceOf(Array) + ctx.expect(results).toStrictEqual(assetsDatabase[0]) ctx.expect.assertions(2) }) it.concurrent("supports `cursor` parameter", async (ctx) => { const client = createTestWriteClient({ ctx }) - const requiredGetParams = { - cursor: "foo", - } - - mockPrismicAssetAPI({ ctx, client, requiredGetParams }) - - const { results } = await client.getAssets(requiredGetParams) - - ctx.expect(results).toBeInstanceOf(Array) - ctx.expect.assertions(2) -}) - -it.concurrent("supports `assetType` parameter", async (ctx) => { - const client = createTestWriteClient({ ctx }) - - const requiredGetParams = { - assetType: AssetType.Image, - } + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + existingAssets: [2, 2], + }) - mockPrismicAssetAPI({ ctx, client, requiredGetParams }) + ctx.server.events.on("request:start", (req) => { + if (req.url.hostname.startsWith(client.repositoryName)) { + ctx.expect(req.url.searchParams.get("cursor")).toBe("1") + } + }) - const { results } = await client.getAssets(requiredGetParams) + // @ts-expect-error - testing purposes + const { results } = await client.getAssets({ cursor: "1" }) - ctx.expect(results).toBeInstanceOf(Array) + ctx.expect(results).toStrictEqual(assetsDatabase[1]) ctx.expect.assertions(2) }) -it.concurrent("supports `keyword` parameter", async (ctx) => { +it.concurrent.skip("supports `assetType` parameter", async (ctx) => { const client = createTestWriteClient({ ctx }) - const requiredGetParams = { - keyword: "foo", - } + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + existingAssets: [2], + }) - mockPrismicAssetAPI({ ctx, client, requiredGetParams }) + ctx.server.events.on("request:start", (req) => { + if (req.url.hostname.startsWith(client.repositoryName)) { + ctx.expect(req.url.searchParams.get("assetType")).toBe(AssetType.Image) + } + }) - const { results } = await client.getAssets(requiredGetParams) + // @ts-expect-error - testing purposes + const { results } = await client.getAssets({ assetType: AssetType.Image }) - ctx.expect(results).toBeInstanceOf(Array) + ctx.expect(results).toStrictEqual(assetsDatabase[0]) ctx.expect.assertions(2) }) -it.concurrent("supports `ids` parameter", async (ctx) => { +it.concurrent.skip("supports `keyword` parameter", async (ctx) => { const client = createTestWriteClient({ ctx }) - const requiredGetParams = { - ids: ["foo", "bar"], - } + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + existingAssets: [2], + }) - mockPrismicAssetAPI({ ctx, client, requiredGetParams }) + ctx.server.events.on("request:start", (req) => { + if (req.url.hostname.startsWith(client.repositoryName)) { + ctx.expect(req.url.searchParams.get("keyword")).toBe("foo") + } + }) - const { results } = await client.getAssets(requiredGetParams) + // @ts-expect-error - testing purposes + const { results } = await client.getAssets({ keyword: "foo" }) - ctx.expect(results).toBeInstanceOf(Array) + ctx.expect(results).toStrictEqual(assetsDatabase[0]) ctx.expect.assertions(2) }) -it.concurrent("supports `tags` parameter (id)", async (ctx) => { +it.concurrent.skip("supports `ids` parameter", async (ctx) => { const client = createTestWriteClient({ ctx }) - const requiredGetParams = { - tags: [ - "00000000-4444-4444-4444-121212121212", - "10000000-4444-4444-4444-121212121212", - ], - } - - mockPrismicAssetAPI({ + const { assetsDatabase } = mockPrismicAssetAPI({ ctx, client, - existingTags: [ - { - id: "00000000-4444-4444-4444-121212121212", - name: "foo", - created_at: 0, - last_modified: 0, - }, - { - id: "10000000-4444-4444-4444-121212121212", - name: "bar", - created_at: 0, - last_modified: 0, - }, - ], - requiredGetParams, + existingAssets: [2], + }) + + const ids = ["foo", "bar"] + + ctx.server.events.on("request:start", (req) => { + if (req.url.hostname.startsWith(client.repositoryName)) { + ctx.expect(req.url.searchParams.getAll("ids")).toStrictEqual(ids) + } }) - const { results } = await client.getAssets(requiredGetParams) + // @ts-expect-error - testing purposes + const { results } = await client.getAssets({ ids }) - ctx.expect(results).toBeInstanceOf(Array) + ctx.expect(results).toStrictEqual(assetsDatabase[0]) ctx.expect.assertions(2) }) -it.concurrent("supports `tags` parameter (name)", async (ctx) => { +it.concurrent.skip("supports `tags` parameter", async (ctx) => { const client = createTestWriteClient({ ctx }) - const requiredGetParams = { - tags: [ - "00000000-4444-4444-4444-121212121212", - "10000000-4444-4444-4444-121212121212", - ], - } - - mockPrismicAssetAPI({ + const { assetsDatabase } = mockPrismicAssetAPI({ ctx, client, + existingAssets: [2], existingTags: [ { id: "00000000-4444-4444-4444-121212121212", @@ -162,25 +160,33 @@ it.concurrent("supports `tags` parameter (name)", async (ctx) => { last_modified: 0, }, ], - requiredGetParams, }) + ctx.server.events.on("request:start", (req) => { + if (req.url.hostname.startsWith(client.repositoryName)) { + ctx + .expect(req.url.searchParams.getAll("tags")) + .toStrictEqual([ + "00000000-4444-4444-4444-121212121212", + "10000000-4444-4444-4444-121212121212", + ]) + } + }) + + // @ts-expect-error - testing purposes const { results } = await client.getAssets({ tags: ["foo", "bar"] }) - ctx.expect(results).toBeInstanceOf(Array) + ctx.expect(results).toStrictEqual(assetsDatabase[0]) ctx.expect.assertions(2) }) -it.concurrent("supports `tags` parameter (missing)", async (ctx) => { +it.concurrent.skip("supports `tags` parameter (missing)", async (ctx) => { const client = createTestWriteClient({ ctx }) - const requiredGetParams = { - tags: ["00000000-4444-4444-4444-121212121212"], - } - - mockPrismicAssetAPI({ + const { assetsDatabase } = mockPrismicAssetAPI({ ctx, client, + existingAssets: [2], existingTags: [ { id: "00000000-4444-4444-4444-121212121212", @@ -189,12 +195,20 @@ it.concurrent("supports `tags` parameter (missing)", async (ctx) => { last_modified: 0, }, ], - requiredGetParams, }) + ctx.server.events.on("request:start", (req) => { + if (req.url.hostname.startsWith(client.repositoryName)) { + ctx + .expect(req.url.searchParams.getAll("tags")) + .toStrictEqual(["00000000-4444-4444-4444-121212121212"]) + } + }) + + // @ts-expect-error - testing purposes const { results } = await client.getAssets({ tags: ["foo", "bar"] }) - ctx.expect(results).toBeInstanceOf(Array) + ctx.expect(results).toStrictEqual(assetsDatabase[0]) ctx.expect.assertions(2) }) @@ -203,20 +217,36 @@ it.concurrent("returns `next` when next `cursor` is available", async (ctx) => { mockPrismicAssetAPI({ ctx, client }) + // @ts-expect-error - testing purposes const { next: next1 } = await client.getAssets() ctx.expect(next1).toBeUndefined() - mockPrismicAssetAPI({ ctx, client, existingAssets: [[], []] }) + mockPrismicAssetAPI({ + ctx, + client, + existingAssets: [2, 2], + }) + // @ts-expect-error - testing purposes const { next: next2 } = await client.getAssets() ctx.expect(next2).toBeInstanceOf(Function) - mockPrismicAssetAPI({ ctx, client, requiredGetParams: { cursor: "1" } }) + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + existingAssets: [2, 2], + }) + + ctx.server.events.on("request:start", (req) => { + if (req.url.hostname.startsWith(client.repositoryName)) { + ctx.expect(req.url.searchParams.get("cursor")).toBe("1") + } + }) const { results } = await next2!() - ctx.expect(results).toBeInstanceOf(Array) + ctx.expect(results).toStrictEqual(assetsDatabase[1]) ctx.expect.assertions(4) }) @@ -225,6 +255,7 @@ it.concurrent("throws forbidden error on invalid credentials", async (ctx) => { mockPrismicAssetAPI({ ctx, client, writeToken: "invalid" }) + // @ts-expect-error - testing purposes await expect(() => client.getAssets()).rejects.toThrow(ForbiddenError) }) @@ -237,6 +268,7 @@ it.concurrent("is abortable with an AbortController", async (ctx) => { mockPrismicAssetAPI({ ctx, client }) await expect(() => + // @ts-expect-error - testing purposes client.getAssets({ fetchOptions: { signal: controller.signal }, }), @@ -248,10 +280,16 @@ it.concurrent("supports custom headers", async (ctx) => { const headers = { "x-custom": "foo" } - mockPrismicAssetAPI({ ctx, client, requiredHeaders: headers }) + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + existingAssets: [2], + requiredHeaders: headers, + }) + // @ts-expect-error - testing purposes const { results } = await client.getAssets({ fetchOptions: { headers } }) - ctx.expect(results).toBeInstanceOf(Array) + ctx.expect(results).toStrictEqual(assetsDatabase[0]) ctx.expect.assertions(2) }) diff --git a/test/writeClient-migrateCreateAssets.test.ts b/test/writeClient-migrate-assets.test.ts similarity index 88% rename from test/writeClient-migrateCreateAssets.test.ts rename to test/writeClient-migrate-assets.test.ts index 39b3d3b1..298fe6e5 100644 --- a/test/writeClient-migrateCreateAssets.test.ts +++ b/test/writeClient-migrate-assets.test.ts @@ -101,7 +101,11 @@ it.concurrent("creates new asset from string file data", async (ctx) => { const dummyFileData = "foo" mockPrismicRestAPIV2({ ctx }) - mockPrismicAssetAPI({ ctx, client, newAssets: [asset] }) + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + newAssets: [asset], + }) mockPrismicMigrationAPI({ ctx, client }) const migration = prismic.createMigration() @@ -109,6 +113,8 @@ it.concurrent("creates new asset from string file data", async (ctx) => { const reporter = vi.fn() + expect(assetsDatabase.flat()).toHaveLength(0) + await client.migrate(migration, { reporter }) expect(reporter).toHaveBeenCalledWith({ @@ -123,6 +129,7 @@ it.concurrent("creates new asset from string file data", async (ctx) => { }), }, }) + expect(assetsDatabase.flat()).toHaveLength(1) }) it.concurrent("creates new asset from a File instance", async (ctx) => { @@ -132,7 +139,11 @@ it.concurrent("creates new asset from a File instance", async (ctx) => { const dummyFile = new File(["foo"], asset.filename) mockPrismicRestAPIV2({ ctx }) - mockPrismicAssetAPI({ ctx, client, newAssets: [asset] }) + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + newAssets: [asset], + }) mockPrismicMigrationAPI({ ctx, client }) const migration = prismic.createMigration() @@ -140,6 +151,8 @@ it.concurrent("creates new asset from a File instance", async (ctx) => { const reporter = vi.fn() + expect(assetsDatabase.flat()).toHaveLength(0) + await client.migrate(migration, { reporter }) expect(reporter).toHaveBeenCalledWith({ @@ -154,6 +167,7 @@ it.concurrent("creates new asset from a File instance", async (ctx) => { }), }, }) + expect(assetsDatabase.flat()).toHaveLength(1) }) it.concurrent( @@ -164,7 +178,11 @@ it.concurrent( const asset = mockAsset(ctx) mockPrismicRestAPIV2({ ctx }) - mockPrismicAssetAPI({ ctx, client, newAssets: [asset] }) + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + newAssets: [asset], + }) mockPrismicMigrationAPI({ ctx, client }) ctx.server.use( @@ -178,6 +196,8 @@ it.concurrent( const reporter = vi.fn() + expect(assetsDatabase.flat()).toHaveLength(0) + await client.migrate(migration, { reporter }) expect(reporter).toHaveBeenCalledWith({ @@ -192,6 +212,7 @@ it.concurrent( }), }, }) + expect(assetsDatabase.flat()).toHaveLength(1) }, ) @@ -203,7 +224,11 @@ it.concurrent( const asset = mockAsset(ctx) mockPrismicRestAPIV2({ ctx }) - mockPrismicAssetAPI({ ctx, client, newAssets: [asset] }) + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + newAssets: [asset], + }) mockPrismicMigrationAPI({ ctx, client }) ctx.server.use( @@ -217,6 +242,8 @@ it.concurrent( const reporter = vi.fn() + expect(assetsDatabase.flat()).toHaveLength(0) + await client.migrate(migration, { reporter }) expect(reporter).toHaveBeenCalledWith({ @@ -231,6 +258,7 @@ it.concurrent( }), }, }) + expect(assetsDatabase.flat()).toHaveLength(1) }, ) @@ -242,7 +270,11 @@ it.concurrent( const asset = mockAsset(ctx) mockPrismicRestAPIV2({ ctx }) - mockPrismicAssetAPI({ ctx, client, newAssets: [asset] }) + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + newAssets: [asset], + }) mockPrismicMigrationAPI({ ctx, client }) ctx.server.use( @@ -256,6 +288,8 @@ it.concurrent( const reporter = vi.fn() + expect(assetsDatabase.flat()).toHaveLength(0) + await client.migrate(migration, { reporter }) expect(reporter).toHaveBeenCalledWith({ @@ -270,6 +304,7 @@ it.concurrent( }), }, }) + expect(assetsDatabase.flat()).toHaveLength(1) }, ) @@ -281,7 +316,11 @@ it.concurrent( const asset = mockAsset(ctx) mockPrismicRestAPIV2({ ctx }) - mockPrismicAssetAPI({ ctx, client, newAssets: [asset] }) + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + newAssets: [asset], + }) mockPrismicMigrationAPI({ ctx, client }) ctx.server.use( @@ -295,6 +334,8 @@ it.concurrent( const reporter = vi.fn() + expect(assetsDatabase.flat()).toHaveLength(0) + await client.migrate(migration, { reporter }) expect(reporter).toHaveBeenCalledWith({ @@ -309,6 +350,7 @@ it.concurrent( }), }, }) + expect(assetsDatabase.flat()).toHaveLength(1) }, ) diff --git a/test/writeClient-migrateCreateDocuments.test.ts b/test/writeClient-migrate-documents.test.ts similarity index 100% rename from test/writeClient-migrateCreateDocuments.test.ts rename to test/writeClient-migrate-documents.test.ts diff --git a/test/writeClient-migrateUpdateDocuments-image.test.ts b/test/writeClient-migrate-patch-image.test.ts similarity index 100% rename from test/writeClient-migrateUpdateDocuments-image.test.ts rename to test/writeClient-migrate-patch-image.test.ts diff --git a/test/writeClient-migrateUpdateDocuments-link.test.ts b/test/writeClient-migrate-patch-link.test.ts similarity index 100% rename from test/writeClient-migrateUpdateDocuments-link.test.ts rename to test/writeClient-migrate-patch-link.test.ts diff --git a/test/writeClient-migrateUpdateDocuments-linkToMedia.test.ts b/test/writeClient-migrate-patch-linkToMedia.test.ts similarity index 100% rename from test/writeClient-migrateUpdateDocuments-linkToMedia.test.ts rename to test/writeClient-migrate-patch-linkToMedia.test.ts diff --git a/test/writeClient-migrateUpdateDocuments-simpleField.test.ts b/test/writeClient-migrate-patch-simpleField.test.ts similarity index 100% rename from test/writeClient-migrateUpdateDocuments-simpleField.test.ts rename to test/writeClient-migrate-patch-simpleField.test.ts diff --git a/test/writeClient-migrate.test.ts b/test/writeClient-migrate.test.ts index 896fd005..db2a5137 100644 --- a/test/writeClient-migrate.test.ts +++ b/test/writeClient-migrate.test.ts @@ -1,16 +1,122 @@ import { it as _it, expect, vi } from "vitest" import { createTestWriteClient } from "./__testutils__/createWriteClient" -import { mockPrismicAssetAPI } from "./__testutils__/mockPrismicAssetAPI" +import { + mockAsset, + mockPrismicAssetAPI, +} from "./__testutils__/mockPrismicAssetAPI" +import { mockPrismicMigrationAPI } from "./__testutils__/mockPrismicMigrationAPI" import { mockPrismicRestAPIV2 } from "./__testutils__/mockPrismicRestAPIV2" import * as prismic from "../src" +import type { + AssetMap, + DocumentMap, +} from "../src/lib/patchMigrationDocumentData" // Skip test on Node 16 and 18 (File and FormData support) const isNode16 = process.version.startsWith("v16") const isNode18 = process.version.startsWith("v18") const it = _it.skipIf(isNode16 || isNode18) +it.concurrent("performs migration", async (ctx) => { + const client = createTestWriteClient({ ctx }) + + const asset = mockAsset(ctx) + const newDocuments = [{ id: "foo" }, { id: "bar" }] + + mockPrismicRestAPIV2({ ctx }) + const { assetsDatabase } = mockPrismicAssetAPI({ + ctx, + client, + newAssets: [asset], + }) + const { documentsDatabase } = mockPrismicMigrationAPI({ + ctx, + client, + newDocuments, + }) + + const migration = prismic.createMigration() + + const documentFoo: prismic.PrismicMigrationDocument = + ctx.mock.value.document() + documentFoo.data = { + image: migration.createAsset("foo", "foo.png"), + link: () => migration.getByUID("bar", "bar"), + } + + const documentBar = ctx.mock.value.document() + documentBar.type = "bar" + documentBar.uid = "bar" + + migration.createDocument(documentFoo, "foo") + migration.createDocument(documentBar, "bar") + + let documents: DocumentMap | undefined + let assets: AssetMap | undefined + const reporter = vi.fn<(event: prismic.MigrateReporterEvents) => void>( + (event) => { + if (event.type === "assets:created") { + assets = event.data.assets + } else if (event.type === "documents:created") { + documents = event.data.documents + } + }, + ) + + await client.migrate(migration, { reporter }) + + expect(assets?.size).toBe(1) + expect(assetsDatabase.flat()).toHaveLength(1) + // Documents are indexed twice, on ID, and on reference + expect(documents?.size).toBe(4) + expect(Object.keys(documentsDatabase)).toHaveLength(2) + + expect(reporter).toHaveBeenCalledWith({ + type: "start", + data: { + pending: { + documents: 2, + assets: 1, + }, + }, + }) + + expect(reporter).toHaveBeenCalledWith({ + type: "assets:created", + data: { + created: 1, + assets: expect.any(Map), + }, + }) + + expect(reporter).toHaveBeenCalledWith({ + type: "documents:created", + data: { + created: 2, + documents: expect.any(Map), + }, + }) + + expect(reporter).toHaveBeenCalledWith({ + type: "documents:updated", + data: { + updated: 2, + }, + }) + + expect(reporter).toHaveBeenCalledWith({ + type: "end", + data: { + migrated: { + documents: 2, + assets: 1, + }, + }, + }) +}) + it.concurrent("migrates nothing when migration is empty", async (ctx) => { const client = createTestWriteClient({ ctx }) diff --git a/test/writeClient.test.ts b/test/writeClient.test.ts index b599e30b..966d96a6 100644 --- a/test/writeClient.test.ts +++ b/test/writeClient.test.ts @@ -11,7 +11,7 @@ it("creates a write client with `createWriteClient`", () => { expect(client).toBeInstanceOf(prismic.WriteClient) }) -it("warns constructor in if running in a browser-like environment", () => { +it("warns in constructor if running in a browser-like environment", () => { const originalWindow = globalThis.window globalThis.window = {} as Window & typeof globalThis