Skip to content

Commit

Permalink
feat: return alternative language documents for mock documents
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Aug 26, 2021
1 parent 52d8119 commit 4444df2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
18 changes: 18 additions & 0 deletions src/lib/buildAlternativeLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as prismicT from "@prismicio/types";

type BuildEmbedFieldConfig<Document extends prismicT.PrismicDocument> = {
document: Document;
};

export const buildAlternativeLanguage = <
Document extends prismicT.PrismicDocument,
>(
config: BuildEmbedFieldConfig<Document>,
): prismicT.AlternateLanguage<Document["type"], Document["lang"]> => {
return {
id: config.document.id,
type: config.document.type,
lang: config.document.lang,
uid: config.document.uid || undefined,
};
};
12 changes: 7 additions & 5 deletions src/lib/buildContentRelationshipField.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as prismicT from "@prismicio/types";

type BuildEmbedFieldConfig = {
document: prismicT.PrismicDocument;
type BuildEmbedFieldConfig<Document extends prismicT.PrismicDocument> = {
document: Document;
};

export const buildContentRelationshipField = (
config: BuildEmbedFieldConfig,
): prismicT.FilledLinkToDocumentField => {
export const buildContentRelationshipField = <
Document extends prismicT.PrismicDocument,
>(
config: BuildEmbedFieldConfig<Document>,
): prismicT.FilledLinkToDocumentField<Document["type"], Document["lang"]> => {
return {
link_type: prismicT.LinkType.Document,
id: config.document.id,
Expand Down
12 changes: 10 additions & 2 deletions src/value/customType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import { MockValueConfig, ModelValue } from "../types";
import * as modelGen from "../model";

import { timestamp } from "./timestamp";
import { buildAlternativeLanguage } from "../lib/buildAlternativeLanguage";

export type MockCustomTypeValueConfig<
Model extends prismicT.CustomTypeModel = prismicT.CustomTypeModel,
> = {
withURL?: boolean;
sharedSliceModels?: prismicT.SharedSliceModel[];
alternateLanguages?: prismicT.PrismicDocument[];
configs?: ValueForModelMapConfigs;
} & MockValueConfig<Model>;

Expand Down Expand Up @@ -53,6 +54,13 @@ export const customType = <

const withURL = config.withURL ?? true;

const alternateLanguages = (config.alternateLanguages || []).map(
(alternateLanguageDocument) =>
buildAlternativeLanguage({
document: alternateLanguageDocument,
}),
);

return {
type: model.id,
id: faker.git.shortSha(),
Expand All @@ -63,7 +71,7 @@ export const customType = <
tags: generateTags({ seed: config.seed }),
slugs: [] as prismicT.PrismicDocument["slugs"],
linked_documents: [] as prismicT.PrismicDocument["linked_documents"],
alternate_languages: [] as prismicT.PrismicDocument["alternate_languages"],
alternate_languages: alternateLanguages,
first_publication_date: timestamp({ seed: config.seed }),
last_publication_date: timestamp({ seed: config.seed }),
data: valueForModelMap({
Expand Down
18 changes: 18 additions & 0 deletions test/value-customType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,21 @@ test("uid field is not included in data field", (t) => {
t.is(typeof actual.uid, "string");
t.false(uidFieldName in actual.data);
});

test("can be configured to return value with alternative languages", (t) => {
const customModel = model.customType();
const alternateLanguages = [
value.customType({ model: customModel }),
value.customType({ model: customModel }),
];

const actual = value.customType({
model: customModel,
alternateLanguages,
});

t.deepEqual(
actual.alternate_languages.map((item) => item.id),
alternateLanguages.map((alternateLanguage) => alternateLanguage.id),
);
});

0 comments on commit 4444df2

Please sign in to comment.