Skip to content

Commit

Permalink
feat: add text to link to media model
Browse files Browse the repository at this point in the history
  • Loading branch information
levimykel committed Aug 26, 2024
1 parent 82f9b29 commit 6d730f3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/model/linkToMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ import * as changeCase from "change-case";

import { createFaker } from "../lib/createFaker";

import { LinkText } from "./link";
import { MockModelConfig } from "../types";

export type MockLinkToMediaModelConfig = MockModelConfig;
type MockLinkToMediaModel<Text extends boolean = boolean> =
prismic.CustomTypeModelLinkToMediaField & {
config: Text extends true
? {
text: LinkText;
}
: {
text?: undefined;
};
};

export type MockLinkToMediaModelConfig<Text extends boolean = boolean> = {
text?: Text;
} & MockModelConfig;

export const linkToMedia = (
config: MockLinkToMediaModelConfig,
): prismic.CustomTypeModelLinkToMediaField => {
export const linkToMedia = <Text extends boolean = boolean>(
config: MockLinkToMediaModelConfig<Text>,
): MockLinkToMediaModel<Text> => {
const faker = config.faker || createFaker(config.seed);

return {
Expand All @@ -18,6 +32,11 @@ export const linkToMedia = (
label: changeCase.capitalCase(faker.word()),
placeholder: changeCase.sentenceCase(faker.words(3)),
select: prismic.CustomTypeModelLinkSelectType.Media,
text: config.text
? {
type: prismic.CustomTypeModelFieldType.Text,
}
: undefined,
},
};
} as MockLinkToMediaModel<Text>;
};
14 changes: 14 additions & 0 deletions test/model-linkToMedia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ test("creates a mock Link To Media field model", snapshotTwiceMacro, (t) =>
test("supports number seed", snapshotTwiceMacro, () =>
model.linkToMedia({ seed: 1 }),
);

test("can be configured to explicitly support the text property", (t) => {
const actualTrue = model.linkToMedia({
seed: t.title,
text: true,
});
t.is(actualTrue.config.text.type, "Text");

const actualFalse = model.linkToMedia({
seed: t.title,
text: false,
});
t.is(actualFalse.config.text, undefined);
});
4 changes: 4 additions & 0 deletions test/snapshots/model-linkToMedia.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Generated by [AVA](https://avajs.dev).
label: 'Pharetra',
placeholder: 'Tempor id eu',
select: 'media',
text: undefined,
},
type: 'Link',
},
Expand All @@ -22,6 +23,7 @@ Generated by [AVA](https://avajs.dev).
label: 'Pharetra',
placeholder: 'Tempor id eu',
select: 'media',
text: undefined,
},
type: 'Link',
},
Expand All @@ -37,6 +39,7 @@ Generated by [AVA](https://avajs.dev).
label: 'Erat',
placeholder: 'Ut porttitor leo',
select: 'media',
text: undefined,
},
type: 'Link',
},
Expand All @@ -45,6 +48,7 @@ Generated by [AVA](https://avajs.dev).
label: 'Erat',
placeholder: 'Ut porttitor leo',
select: 'media',
text: undefined,
},
type: 'Link',
},
Expand Down
Binary file modified test/snapshots/model-linkToMedia.test.ts.snap
Binary file not shown.

0 comments on commit 6d730f3

Please sign in to comment.