From 9bab813255d99eafc5d4c0ce09cc8c1efe715691 Mon Sep 17 00:00:00 2001 From: lihbr Date: Wed, 18 Sep 2024 10:30:28 +0200 Subject: [PATCH] refactor: per review --- src/Migration.ts | 7 +- src/WriteClient.ts | 7 +- src/lib/resolveMigrationDocumentData.ts | 14 +- src/types/migration/Asset.ts | 16 + ...ate-patch-contentRelationship.test.ts.snap | 14 +- ...ent-migrate-patch-linkToMedia.test.ts.snap | 315 +++++------------- test/writeClient-migrate-documents.test.ts | 33 +- ...teClient-migrate-patch-linkToMedia.test.ts | 9 + 8 files changed, 140 insertions(+), 275 deletions(-) diff --git a/src/Migration.ts b/src/Migration.ts index d821641f..0f70372d 100644 --- a/src/Migration.ts +++ b/src/Migration.ts @@ -402,7 +402,8 @@ export class Migration { if (input.isBroken) { return { link_type: LinkType.Document, - id: "__broken__", + // ID needs to be 16 characters long to be considered valid by the API + id: "_____broken_____", isBroken: true, // TODO: Remove when link text PR is merged // @ts-expect-error - Future-proofing for link text @@ -412,7 +413,7 @@ export class Migration { return { link_type: LinkType.Document, - id: () => this.#getByOriginalID(input.id), + id: () => this.getByOriginalID(input.id), // TODO: Remove when link text PR is merged // @ts-expect-error - Future-proofing for link text text: input.text, @@ -507,7 +508,7 @@ export class Migration { * @returns The migration document instance for the original ID, if a matching * document is found. */ - #getByOriginalID( + getByOriginalID( id: string, ): | PrismicMigrationDocument> diff --git a/src/WriteClient.ts b/src/WriteClient.ts index 202f7ab8..1d642db2 100644 --- a/src/WriteClient.ts +++ b/src/WriteClient.ts @@ -440,10 +440,15 @@ export class WriteClient< masterLanguageDocumentID = "id" in masterLanguageDocument ? masterLanguageDocument.id : undefined } else if (doc.originalPrismicDocument) { - masterLanguageDocumentID = + const maybeOriginalID = doc.originalPrismicDocument.alternate_languages.find( ({ lang }) => lang === masterLocale, )?.id + + if (maybeOriginalID) { + masterLanguageDocumentID = + migration.getByOriginalID(maybeOriginalID)?.document.id + } } const { id } = await this.createDocument( diff --git a/src/lib/resolveMigrationDocumentData.ts b/src/lib/resolveMigrationDocumentData.ts index 8bfd9fbf..dded02c9 100644 --- a/src/lib/resolveMigrationDocumentData.ts +++ b/src/lib/resolveMigrationDocumentData.ts @@ -1,3 +1,4 @@ +import type { MigrationLinkToMediaField } from "../types/migration/Asset" import { type MigrationImage, type MigrationLinkToMedia, @@ -12,7 +13,6 @@ import { PrismicMigrationDocument } from "../types/migration/Document" import type { FilledImageFieldImage } from "../types/value/image" import type { LinkField } from "../types/value/link" import { LinkType } from "../types/value/link" -import type { LinkToMediaField } from "../types/value/linkToMedia" import type { RTImageNode } from "../types/value/richText" import { RichTextNodeType } from "../types/value/richText" @@ -156,24 +156,18 @@ export const resolveMigrationRTImageNode = async ( export const resolveMigrationLinkToMedia = ( linkToMedia: MigrationLinkToMedia, migration: Migration, -): LinkToMediaField<"filled"> | undefined => { +): MigrationLinkToMediaField => { const asset = migration._assets.get(linkToMedia.id.config.id)?.asset if (asset) { return { id: asset.id, link_type: LinkType.Media, - name: asset.filename, - kind: asset.kind, - url: asset.url, - size: `${asset.size}`, - height: typeof asset.height === "number" ? `${asset.height}` : undefined, - width: typeof asset.width === "number" ? `${asset.width}` : undefined, - // TODO: Remove when link text PR is merged - // @ts-expect-error - Future-proofing for link text text: linkToMedia.text, } } + + return { link_type: LinkType.Media } } /** diff --git a/src/types/migration/Asset.ts b/src/types/migration/Asset.ts index a6242e3a..8aa928a9 100644 --- a/src/types/migration/Asset.ts +++ b/src/types/migration/Asset.ts @@ -1,5 +1,6 @@ import type { Asset } from "../api/asset/asset" import type { FilledImageFieldImage } from "../value/image" +import type { EmptyLinkField } from "../value/link" import type { LinkToMediaField } from "../value/linkToMedia" import { type RTImageNode } from "../value/richText" @@ -11,6 +12,11 @@ import type { InjectMigrationSpecificTypes } from "./Document" export type MigrationAssetConfig = { /** * ID the assets is indexed with on the migration instance. + * + * @remarks + * This property's value is not necessarily the same as the as the one in the + * `file` property. It is mainly used for deduplication within a `Migration` + * instance. */ id: string | URL | File | NonNullable[0]>[0] @@ -84,6 +90,16 @@ export type MigrationLinkToMedia = Pick< id: PrismicMigrationAsset } +/** + * The minimum amount of information needed to represent a link to media field + * with the migration API. + */ +export type MigrationLinkToMediaField = + // TODO: Remove when link text PR is merged + // @ts-expect-error - Future-proofing for link text + | Pick, "link_type" | "id" | "text"> + | EmptyLinkField<"Media"> + /** * A rich text image node in a migration. */ diff --git a/test/__snapshots__/writeClient-migrate-patch-contentRelationship.test.ts.snap b/test/__snapshots__/writeClient-migrate-patch-contentRelationship.test.ts.snap index ae564060..337e410a 100644 --- a/test/__snapshots__/writeClient-migrate-patch-contentRelationship.test.ts.snap +++ b/test/__snapshots__/writeClient-migrate-patch-contentRelationship.test.ts.snap @@ -5,7 +5,7 @@ exports[`patches content relationship fields (from Prismic) > broken > group 1`] "group": [ { "field": { - "id": "__broken__", + "id": "_____broken_____", "isBroken": true, "link_type": "Document", }, @@ -22,7 +22,7 @@ exports[`patches content relationship fields (from Prismic) > broken > shared sl "items": [ { "field": { - "id": "__broken__", + "id": "_____broken_____", "isBroken": true, "link_type": "Document", }, @@ -30,14 +30,14 @@ exports[`patches content relationship fields (from Prismic) > broken > shared sl ], "primary": { "field": { - "id": "__broken__", + "id": "_____broken_____", "isBroken": true, "link_type": "Document", }, "group": [ { "field": { - "id": "__broken__", + "id": "_____broken_____", "isBroken": true, "link_type": "Document", }, @@ -61,7 +61,7 @@ exports[`patches content relationship fields (from Prismic) > broken > slice 1`] "items": [ { "field": { - "id": "__broken__", + "id": "_____broken_____", "isBroken": true, "link_type": "Document", }, @@ -69,7 +69,7 @@ exports[`patches content relationship fields (from Prismic) > broken > slice 1`] ], "primary": { "field": { - "id": "__broken__", + "id": "_____broken_____", "isBroken": true, "link_type": "Document", }, @@ -84,7 +84,7 @@ exports[`patches content relationship fields (from Prismic) > broken > slice 1`] exports[`patches content relationship fields (from Prismic) > broken > static zone 1`] = ` { "field": { - "id": "__broken__", + "id": "_____broken_____", "isBroken": true, "link_type": "Document", }, diff --git a/test/__snapshots__/writeClient-migrate-patch-linkToMedia.test.ts.snap b/test/__snapshots__/writeClient-migrate-patch-linkToMedia.test.ts.snap index ceb749b4..9275be7f 100644 --- a/test/__snapshots__/writeClient-migrate-patch-linkToMedia.test.ts.snap +++ b/test/__snapshots__/writeClient-migrate-patch-linkToMedia.test.ts.snap @@ -14,14 +14,8 @@ exports[`patches link to media fields (from Prismic) > inRichText > group 1`] = }, { "data": { - "height": "1", "id": "fc26fe8d098", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -54,14 +48,8 @@ exports[`patches link to media fields (from Prismic) > inRichText > shared slice }, { "data": { - "height": "1", "id": "961adcf1f5d", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5?w=2200&h=1467&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -85,14 +73,8 @@ exports[`patches link to media fields (from Prismic) > inRichText > shared slice }, { "data": { - "height": "1", "id": "961adcf1f5d", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5?w=2200&h=1467&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -115,14 +97,8 @@ exports[`patches link to media fields (from Prismic) > inRichText > shared slice }, { "data": { - "height": "1", "id": "961adcf1f5d", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5?w=2200&h=1467&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -162,14 +138,8 @@ exports[`patches link to media fields (from Prismic) > inRichText > slice 1`] = }, { "data": { - "height": "1", "id": "54f4a69ff72", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?w=2560&h=1440&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -193,14 +163,8 @@ exports[`patches link to media fields (from Prismic) > inRichText > slice 1`] = }, { "data": { - "height": "1", "id": "54f4a69ff72", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?w=2560&h=1440&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -231,14 +195,8 @@ exports[`patches link to media fields (from Prismic) > inRichText > static zone }, { "data": { - "height": "1", "id": "1d17b04a95c", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -257,14 +215,8 @@ exports[`patches link to media fields (from Prismic) > simple > group 1`] = ` "group": [ { "field": { - "height": "1", "id": "fc26fe8d098", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - "width": "1", }, }, ], @@ -279,39 +231,21 @@ exports[`patches link to media fields (from Prismic) > simple > shared slice 1`] "items": [ { "field": { - "height": "1", "id": "961adcf1f5d", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5?w=2200&h=1467&fit=crop", - "width": "1", }, }, ], "primary": { "field": { - "height": "1", "id": "961adcf1f5d", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5?w=2200&h=1467&fit=crop", - "width": "1", }, "group": [ { "field": { - "height": "1", "id": "961adcf1f5d", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5?w=2200&h=1467&fit=crop", - "width": "1", }, }, ], @@ -333,27 +267,15 @@ exports[`patches link to media fields (from Prismic) > simple > slice 1`] = ` "items": [ { "field": { - "height": "1", "id": "54f4a69ff72", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?w=2560&h=1440&fit=crop", - "width": "1", }, }, ], "primary": { "field": { - "height": "1", "id": "54f4a69ff72", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?w=2560&h=1440&fit=crop", - "width": "1", }, }, "slice_label": "Vel", @@ -366,14 +288,85 @@ exports[`patches link to media fields (from Prismic) > simple > slice 1`] = ` exports[`patches link to media fields (from Prismic) > simple > static zone 1`] = ` { "field": { - "height": "1", "id": "1d17b04a95c", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", - "width": "1", + }, +} +`; + +exports[`patches link to media fields > empty > group 1`] = ` +{ + "group": [ + { + "field": { + "link_type": "Media", + }, + }, + ], +} +`; + +exports[`patches link to media fields > empty > shared slice 1`] = ` +{ + "slices": [ + { + "id": "9b9dd0f2c3f", + "items": [ + { + "field": { + "link_type": "Media", + }, + }, + ], + "primary": { + "field": { + "link_type": "Media", + }, + "group": [ + { + "field": { + "link_type": "Media", + }, + }, + ], + }, + "slice_label": null, + "slice_type": "nunc", + "variation": "ullamcorper", + "version": "8bfc905", + }, + ], +} +`; + +exports[`patches link to media fields > empty > slice 1`] = ` +{ + "slices": [ + { + "id": "5306297c5ed", + "items": [ + { + "field": { + "link_type": "Media", + }, + }, + ], + "primary": { + "field": { + "link_type": "Media", + }, + }, + "slice_label": "Vel", + "slice_type": "hac_habitasse", + }, + ], +} +`; + +exports[`patches link to media fields > empty > static zone 1`] = ` +{ + "field": { + "link_type": "Media", }, } `; @@ -509,14 +502,8 @@ exports[`patches link to media fields > new > group 1`] = ` "group": [ { "field": { - "height": "1", "id": "fdd322ca9d5", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1587502537745-84b86da1204f?w=6550&h=4367&fit=crop", - "width": "1", }, }, ], @@ -531,39 +518,21 @@ exports[`patches link to media fields > new > shared slice 1`] = ` "items": [ { "field": { - "height": "1", "id": "c0eddbd0255", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", - "width": "1", }, }, ], "primary": { "field": { - "height": "1", "id": "c0eddbd0255", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", - "width": "1", }, "group": [ { "field": { - "height": "1", "id": "c0eddbd0255", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", - "width": "1", }, }, ], @@ -585,27 +554,15 @@ exports[`patches link to media fields > new > slice 1`] = ` "items": [ { "field": { - "height": "1", "id": "82beb55ec2f", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", - "width": "1", }, }, ], "primary": { "field": { - "height": "1", "id": "82beb55ec2f", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", - "width": "1", }, }, "slice_label": "Vel", @@ -618,14 +575,8 @@ exports[`patches link to media fields > new > slice 1`] = ` exports[`patches link to media fields > new > static zone 1`] = ` { "field": { - "height": "1", "id": "bad670dad77", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - "width": "1", }, } `; @@ -636,11 +587,7 @@ exports[`patches link to media fields > newNonImage > group 1`] = ` { "field": { "id": "fdd322ca9d5", - "kind": "document", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1587502537745-84b86da1204f?w=6550&h=4367&fit=crop", }, }, ], @@ -656,32 +603,20 @@ exports[`patches link to media fields > newNonImage > shared slice 1`] = ` { "field": { "id": "c0eddbd0255", - "kind": "document", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", }, }, ], "primary": { "field": { "id": "c0eddbd0255", - "kind": "document", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", }, "group": [ { "field": { "id": "c0eddbd0255", - "kind": "document", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", }, }, ], @@ -704,22 +639,14 @@ exports[`patches link to media fields > newNonImage > slice 1`] = ` { "field": { "id": "82beb55ec2f", - "kind": "document", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", }, }, ], "primary": { "field": { "id": "82beb55ec2f", - "kind": "document", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", }, }, "slice_label": "Vel", @@ -733,11 +660,7 @@ exports[`patches link to media fields > newNonImage > static zone 1`] = ` { "field": { "id": "bad670dad77", - "kind": "document", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", }, } `; @@ -747,15 +670,9 @@ exports[`patches link to media fields > newWithText > group 1`] = ` "group": [ { "field": { - "height": "1", "id": "fdd322ca9d5", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", "text": "foo", - "url": "https://images.unsplash.com/photo-1587502537745-84b86da1204f?w=6550&h=4367&fit=crop", - "width": "1", }, }, ], @@ -770,42 +687,24 @@ exports[`patches link to media fields > newWithText > shared slice 1`] = ` "items": [ { "field": { - "height": "1", "id": "c0eddbd0255", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", "text": "foo", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", - "width": "1", }, }, ], "primary": { "field": { - "height": "1", "id": "c0eddbd0255", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", "text": "foo", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", - "width": "1", }, "group": [ { "field": { - "height": "1", "id": "c0eddbd0255", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", "text": "foo", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", - "width": "1", }, }, ], @@ -827,29 +726,17 @@ exports[`patches link to media fields > newWithText > slice 1`] = ` "items": [ { "field": { - "height": "1", "id": "82beb55ec2f", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", "text": "foo", - "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", - "width": "1", }, }, ], "primary": { "field": { - "height": "1", "id": "82beb55ec2f", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", "text": "foo", - "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", - "width": "1", }, }, "slice_label": "Vel", @@ -862,15 +749,9 @@ exports[`patches link to media fields > newWithText > slice 1`] = ` exports[`patches link to media fields > newWithText > static zone 1`] = ` { "field": { - "height": "1", "id": "bad670dad77", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", "text": "foo", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - "width": "1", }, } `; @@ -1141,14 +1022,8 @@ exports[`patches link to media fields > richTextNew > group 1`] = ` }, { "data": { - "height": "1", "id": "fdd322ca9d5", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1587502537745-84b86da1204f?w=6550&h=4367&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -1181,14 +1056,8 @@ exports[`patches link to media fields > richTextNew > shared slice 1`] = ` }, { "data": { - "height": "1", "id": "c0eddbd0255", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -1212,14 +1081,8 @@ exports[`patches link to media fields > richTextNew > shared slice 1`] = ` }, { "data": { - "height": "1", "id": "c0eddbd0255", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -1242,14 +1105,8 @@ exports[`patches link to media fields > richTextNew > shared slice 1`] = ` }, { "data": { - "height": "1", "id": "c0eddbd0255", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=5616&h=3744&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -1289,14 +1146,8 @@ exports[`patches link to media fields > richTextNew > slice 1`] = ` }, { "data": { - "height": "1", "id": "82beb55ec2f", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -1320,14 +1171,8 @@ exports[`patches link to media fields > richTextNew > slice 1`] = ` }, { "data": { - "height": "1", "id": "82beb55ec2f", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=7372&h=4392&fit=crop", - "width": "1", }, "end": 5, "start": 0, @@ -1358,14 +1203,8 @@ exports[`patches link to media fields > richTextNew > static zone 1`] = ` }, { "data": { - "height": "1", "id": "bad670dad77", - "kind": "image", "link_type": "Media", - "name": "default.jpg", - "size": "1", - "url": "https://images.unsplash.com/photo-1604537466608-109fa2f16c3b?w=4240&h=2832&fit=crop", - "width": "1", }, "end": 5, "start": 0, diff --git a/test/writeClient-migrate-documents.test.ts b/test/writeClient-migrate-documents.test.ts index 50062671..ccfc6d49 100644 --- a/test/writeClient-migrate-documents.test.ts +++ b/test/writeClient-migrate-documents.test.ts @@ -331,29 +331,30 @@ it.concurrent( const client = createTestWriteClient({ ctx }) const { repository, masterLocale } = createRepository(ctx) - const queryResponse = createPagedQueryResponses({ - ctx, - pages: 1, - pageSize: 1, - }) - const masterLanguageDocument = queryResponse[0].results[0] + const masterLanguageDocument = ctx.mock.value.document() masterLanguageDocument.lang = masterLocale const document = ctx.mock.value.document({ alternateLanguages: [masterLanguageDocument], }) - const newDocument = { - id: "foo", - masterLanguageDocumentID: masterLanguageDocument.id, - } + const newDocuments = [ + { + id: "foo", + }, + { + id: "bar", + masterLanguageDocumentID: "foo", + }, + ] - mockPrismicRestAPIV2({ ctx, repositoryResponse: repository, queryResponse }) + mockPrismicRestAPIV2({ ctx, repositoryResponse: repository }) mockPrismicAssetAPI({ ctx, client }) - mockPrismicMigrationAPI({ ctx, client, newDocuments: [newDocument] }) + mockPrismicMigrationAPI({ ctx, client, newDocuments }) const migration = prismic.createMigration() - const doc = migration.createDocumentFromPrismic(document, "foo") + migration.createDocumentFromPrismic(masterLanguageDocument, "foo") + const doc = migration.createDocumentFromPrismic(document, "bar") const reporter = vi.fn() @@ -362,13 +363,13 @@ it.concurrent( ctx.expect(reporter).toHaveBeenCalledWith({ type: "documents:creating", data: { - current: 1, + current: 2, remaining: 0, - total: 1, + total: 2, document: doc, }, }) - ctx.expect(doc.document.id).toBe(newDocument.id) + ctx.expect(doc.document.id).toBe("bar") ctx.expect.assertions(3) }, ) diff --git a/test/writeClient-migrate-patch-linkToMedia.test.ts b/test/writeClient-migrate-patch-linkToMedia.test.ts index 3e394a1e..efdf0984 100644 --- a/test/writeClient-migrate-patch-linkToMedia.test.ts +++ b/test/writeClient-migrate-patch-linkToMedia.test.ts @@ -57,6 +57,15 @@ testMigrationFieldPatching< } }, existing: ({ existingAssets }) => assetToLinkToMedia(existingAssets[0]), + empty: ({ migration }) => { + const asset = migration.createAsset("foo", "foo.png") + asset.config.id = "empty" + + return { + link_type: LinkType.Media, + id: asset, + } + }, richTextNew: ({ migration }) => [ { type: RichTextNodeType.paragraph,