Skip to content

Commit

Permalink
refactor: update with review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
levimykel committed Aug 27, 2024
1 parent 20c870c commit 7fcafdc
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 74 deletions.
31 changes: 12 additions & 19 deletions src/model/contentRelationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,35 @@ import * as changeCase from "change-case";

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

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

type MockContentRelationshipModel<
CustomTypeIDs extends string = string,
Tags extends string = string,
Text extends boolean = boolean,
WithText extends boolean = boolean,
> = prismic.CustomTypeModelContentRelationshipField<CustomTypeIDs, Tags> & {
config: Text extends true
? {
text: LinkText;
}
: {
text?: undefined;
};
config: WithText extends true
? { text: prismic.CustomTypeModelKeyTextField }
: { text?: undefined };
};

export type MockContentRelationshipModelConfig<
CustomTypeIDs extends string = string,
Tags extends string = string,
Text extends boolean = boolean,
WithText extends boolean = boolean,
> = {
customTypeIDs?: readonly CustomTypeIDs[];
tags?: readonly Tags[];
text?: Text;
withText?: WithText;
} & MockModelConfig;

export const contentRelationship = <
CustomTypeIDs extends string,
Tags extends string,
Text extends boolean = boolean,
WithText extends boolean = boolean,
>(
config: MockContentRelationshipModelConfig<CustomTypeIDs, Tags, Text>,
): MockContentRelationshipModel<CustomTypeIDs, Tags, Text> => {
config: MockContentRelationshipModelConfig<CustomTypeIDs, Tags, WithText>,
): MockContentRelationshipModel<CustomTypeIDs, Tags, WithText> => {
const faker = config.faker || createFaker(config.seed);

return {
Expand All @@ -47,11 +42,9 @@ export const contentRelationship = <
select: prismic.CustomTypeModelLinkSelectType.Document,
customtypes: config.customTypeIDs,
tags: config.tags,
text: config.text
? {
type: prismic.CustomTypeModelFieldType.Text,
}
text: config.withText
? { type: prismic.CustomTypeModelFieldType.Text }
: undefined,
},
} as MockContentRelationshipModel<CustomTypeIDs, Tags, Text>;
} as MockContentRelationshipModel<CustomTypeIDs, Tags, WithText>;
};
40 changes: 14 additions & 26 deletions src/model/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,33 @@ import { createFaker } from "../lib/createFaker";

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

export type LinkText = prismic.CustomTypeModelKeyTextField;

type MockLinkModel<
AllowTargetBlank extends boolean = boolean,
Text extends boolean = boolean,
WithText extends boolean = boolean,
> = prismic.CustomTypeModelLinkField & {
config: AllowTargetBlank extends true
? {
allowTargetBlank: true;
}
: {
allowTargetBlank?: undefined;
};
? { allowTargetBlank: true }
: { allowTargetBlank?: undefined };
} & {
config: Text extends true
? {
text: LinkText;
}
: {
text?: undefined;
};
config: WithText extends true
? { text: prismic.CustomTypeModelKeyTextField }
: { text?: undefined };
};

export type MockLinkModelConfig<
AllowTargetBlank extends boolean = boolean,
Text extends boolean = boolean,
WithText extends boolean = boolean,
> = {
allowTargetBlank?: AllowTargetBlank;
text?: Text;
withText?: WithText;
} & MockModelConfig;

export const link = <
AllowTargetBlank extends boolean = boolean,
Text extends boolean = boolean,
WithText extends boolean = boolean,
>(
config: MockLinkModelConfig<AllowTargetBlank, Text>,
): MockLinkModel<AllowTargetBlank, Text> => {
config: MockLinkModelConfig<AllowTargetBlank, WithText>,
): MockLinkModel<AllowTargetBlank, WithText> => {
const faker = config.faker || createFaker(config.seed);

return {
Expand All @@ -54,11 +44,9 @@ export const link = <
("allowTargetBlank" in config
? config.allowTargetBlank
: faker.boolean()) || undefined,
text: config.text
? {
type: prismic.CustomTypeModelFieldType.Text,
}
text: config.withText
? { type: prismic.CustomTypeModelFieldType.Text }
: undefined,
},
} as MockLinkModel<AllowTargetBlank, Text>;
} as MockLinkModel<AllowTargetBlank, WithText>;
};
27 changes: 11 additions & 16 deletions src/model/linkToMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@ import * as changeCase from "change-case";

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

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

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

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

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

return {
Expand All @@ -32,11 +27,11 @@ export const linkToMedia = <Text extends boolean = boolean>(
label: changeCase.capitalCase(faker.word()),
placeholder: changeCase.sentenceCase(faker.words(3)),
select: prismic.CustomTypeModelLinkSelectType.Media,
text: config.text
text: config.withText
? {
type: prismic.CustomTypeModelFieldType.Text,
}
: undefined,
},
} as MockLinkToMediaModel<Text>;
} as MockLinkToMediaModel<WithText>;
};
2 changes: 2 additions & 0 deletions src/value/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ export const link = <
faker,
state: config.state,
linkableDocuments: config.linkableDocuments,
withText: config.withText,
}) as unknown as MockLinkValue<LinkType, State>;
}

case prismic.LinkType.Media: {
return linkToMedia({
faker,
state: config.state,
withText: config.withText,
}) as MockLinkValue<LinkType, State>;
}

Expand Down
4 changes: 2 additions & 2 deletions test/model-contentRelationship.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ test("can be configured to constrain by tags", (t) => {
test("can be configured to explicitly support the text property", (t) => {
const actualTrue = model.contentRelationship({
seed: t.title,
text: true,
withText: true,
});
t.is(actualTrue.config?.text?.type, "Text");

const actualFalse = model.contentRelationship({
seed: t.title,
text: false,
withText: false,
});
t.is(actualFalse.config.text, undefined);
});
4 changes: 2 additions & 2 deletions test/model-link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ test("can be configured to explicitly support blank target", (t) => {
test("can be configured to explicitly support the text property", (t) => {
const actualTrue = model.link({
seed: t.title,
text: true,
withText: true,
});
t.is(actualTrue.config.text.type, "Text");

const actualFalse = model.link({
seed: t.title,
text: false,
withText: false,
});
t.is(actualFalse.config.text, undefined);
});
4 changes: 2 additions & 2 deletions test/model-linkToMedia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ test("supports number seed", snapshotTwiceMacro, () =>
test("can be configured to explicitly support the text property", (t) => {
const actualTrue = model.linkToMedia({
seed: t.title,
text: true,
withText: true,
});
t.is(actualTrue.config.text.type, "Text");

const actualFalse = model.linkToMedia({
seed: t.title,
text: false,
withText: false,
});
t.is(actualFalse.config.text, undefined);
});
6 changes: 3 additions & 3 deletions test/value-contentRelationship.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test("supports custom model", (t) => {
seed: t.title,
customTypeIDs: ["type"],
tags: ["tag"],
text: true,
withText: true,
});

const actual = value.contentRelationship({
Expand All @@ -33,7 +33,7 @@ test("supports custom model", (t) => {

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
t.true(customModel.config?.tags!.every((tag) => actual.tags.includes(tag)));
t.is(actual.text, "Aliquam etiam");
t.is(typeof actual.text, "string");
});

test("can be configured to return an empty value", (t) => {
Expand Down Expand Up @@ -120,7 +120,7 @@ test("can be configured to return a value with display text", (t) => {
linkableDocuments,
withText: true,
});
t.is(actualTrue.text, "Neque laoreet");
t.is(typeof actualTrue.text, "string");

const actualFalse = value.contentRelationship({
seed: t.title,
Expand Down
6 changes: 3 additions & 3 deletions test/value-link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test("supports custom model", (t) => {
config: {
...customModelBase.config,
allowTargetBlank: true as const,
text: { type: "Text" as const },
text: model.keyText({ seed: t.title }),
},
};

Expand All @@ -31,7 +31,7 @@ test("supports custom model", (t) => {
});

t.is(actual.target, "_blank");
t.is(actual.text, "Est ultricies");
t.is(typeof actual.text, "string");
});

test("can be configured to return an empty link value", (t) => {
Expand Down Expand Up @@ -66,7 +66,7 @@ test("can be configured to return a value with display text", (t) => {
type: prismic.LinkType.Web,
withText: true,
});
t.is(actualTrue.text, "Ullamcorper morbi");
t.is(typeof actualTrue.text, "string");

const actualFalse = value.link({
seed: t.title,
Expand Down
2 changes: 1 addition & 1 deletion test/value-linkToMedia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test("can be configured to return a value with display text", (t) => {
seed: t.title,
withText: true,
});
t.is(actualTrue.text, "Tincidunt vitae");
t.is(typeof actualTrue.text, "string");

const actualFalse = value.linkToMedia({
seed: t.title,
Expand Down

0 comments on commit 7fcafdc

Please sign in to comment.