Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support link variant for migration client #365

Open
wants to merge 2 commits into
base: dani/link-variant
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,15 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
id: "_____broken_____",
isBroken: true,
text: input.text,
variant: input.variant,
}
}

return {
link_type: LinkType.Document,
id: () => this._getByOriginalID(input.id),
text: input.text,
variant: input.variant,
}
}

Expand All @@ -426,6 +428,7 @@ export class Migration<TDocuments extends PrismicDocument = PrismicDocument> {
link_type: LinkType.Media,
id: this.createAsset(input),
text: input.text,
variant: input.variant,
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/lib/resolveMigrationDocumentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ export async function resolveMigrationContentRelationship(
) {
return {
...(await resolveMigrationContentRelationship(relation.id)),
// TODO: Remove when link text PR is merged
// @ts-expect-error - Future-proofing for link text
text: relation.text,
...("text" in relation && relation.text && { text: relation.text }),
dani-mp marked this conversation as resolved.
Show resolved Hide resolved
...("variant" in relation &&
relation.variant && { variant: relation.variant }),
}
}

return { link_type: LinkType.Document, id: relation.id }
// This is only called when resolveMigrationContentRelationship recursively
// calls itself from the statement above and the resolved content relation
// is a Prismic document value.
return {
link_type: LinkType.Document,
id: relation.id,
}
}

return { link_type: LinkType.Document }
Expand Down Expand Up @@ -164,6 +170,7 @@ export const resolveMigrationLinkToMedia = (
id: asset.id,
link_type: LinkType.Media,
text: linkToMedia.text,
variant: linkToMedia.variant,
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/types/migration/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export type MigrationLinkToMedia = Pick<
LinkToMediaField<"filled">,
"link_type"
> &
Partial<Pick<LinkToMediaField<"filled">, "text">> & {
Partial<Pick<LinkToMediaField<"filled">, "text" | "variant">> & {
/**
* A reference to the migration asset used to resolve the link to media
* field's value.
Expand All @@ -88,7 +88,7 @@ export type MigrationLinkToMedia = Pick<
* with the migration API.
*/
export type MigrationLinkToMediaField =
| Pick<LinkToMediaField<"filled">, "link_type" | "id" | "text">
| Pick<LinkToMediaField<"filled">, "link_type" | "id" | "text" | "variant">
| EmptyLinkField<"Media">

/**
Expand Down
2 changes: 1 addition & 1 deletion src/types/migration/ContentRelationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type MigrationContentRelationship<
> =
| ValueOrThunk<TDocuments | PrismicMigrationDocument<TDocuments> | undefined>
| (Pick<FilledContentRelationshipField, "link_type"> &
Partial<Pick<FilledContentRelationshipField, "text">> & {
Partial<Pick<FilledContentRelationshipField, "text" | "variant">> & {
id: ValueOrThunk<
TDocuments | PrismicMigrationDocument<TDocuments> | undefined
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,97 @@ exports[`patches content relationship fields (from Prismic) > withText > static
}
`;

exports[`patches content relationship fields (from Prismic) > withVariant > group 1`] = `
{
"group": [
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
],
}
`;

exports[`patches content relationship fields (from Prismic) > withVariant > shared slice 1`] = `
{
"slices": [
{
"id": "9b9dd0f2c3f",
"items": [
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
],
"primary": {
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
"group": [
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
],
},
"slice_label": null,
"slice_type": "nunc",
"variation": "ullamcorper",
"version": "8bfc905",
},
],
}
`;

exports[`patches content relationship fields (from Prismic) > withVariant > slice 1`] = `
{
"slices": [
{
"id": "5306297c5ed",
"items": [
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
],
"primary": {
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
"slice_label": "Vel",
"slice_type": "hac_habitasse",
},
],
}
`;

exports[`patches content relationship fields (from Prismic) > withVariant > static zone 1`] = `
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
}
`;

exports[`patches content relationship fields > existing > group 1`] = `
{
"group": [
Expand Down Expand Up @@ -525,6 +616,97 @@ exports[`patches content relationship fields > existingLongFormWithText > static
}
`;

exports[`patches content relationship fields > existingLongFormWithVariant > group 1`] = `
{
"group": [
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
],
}
`;

exports[`patches content relationship fields > existingLongFormWithVariant > shared slice 1`] = `
{
"slices": [
{
"id": "9b9dd0f2c3f",
"items": [
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
],
"primary": {
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
"group": [
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
],
},
"slice_label": null,
"slice_type": "nunc",
"variation": "ullamcorper",
"version": "8bfc905",
},
],
}
`;

exports[`patches content relationship fields > existingLongFormWithVariant > slice 1`] = `
{
"slices": [
{
"id": "5306297c5ed",
"items": [
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
],
"primary": {
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
"slice_label": "Vel",
"slice_type": "hac_habitasse",
},
],
}
`;

exports[`patches content relationship fields > existingLongFormWithVariant > static zone 1`] = `
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
}
`;

exports[`patches content relationship fields > lazyExisting > group 1`] = `
{
"group": [
Expand Down
27 changes: 23 additions & 4 deletions test/writeClient-migrate-patch-contentRelationship.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ testMigrationFieldPatching<
text: "foo",
}
},
existingLongFormWithVariant: ({ existingDocuments }) => {
return {
link_type: LinkType.Document,
id: existingDocuments[0],
variant: "Secondary",
}
},
otherCreate: ({ otherCreateDocument }) => otherCreateDocument,
lazyExisting: ({ existingDocuments }) => {
return () => existingDocuments[0]
Expand Down Expand Up @@ -86,11 +93,23 @@ testMigrationFieldPatching<ContentRelationshipField>(
contentRelationship.id =
otherFromPrismicDocument.originalPrismicDocument!.id

// TODO: Remove when link text PR is merged
// @ts-expect-error - Future-proofing for link text
contentRelationship.text = "foo"
return {
...contentRelationship,
text: "foo",
}
},
withVariant: ({ ctx, otherFromPrismicDocument }) => {
const contentRelationship = ctx.mock.value.link({
type: LinkType.Document,
})
// `migrationDocuments` contains documents from "another repository"
contentRelationship.id =
otherFromPrismicDocument.originalPrismicDocument!.id

return contentRelationship
return {
...contentRelationship,
variant: "Secondary",
}
},
broken: () => {
return {
Expand Down
Loading