Skip to content

Commit

Permalink
Merge pull request #23 from prismicio/lg/text-in-links
Browse files Browse the repository at this point in the history
feat: add support for text in link fields
  • Loading branch information
dani-mp authored Sep 26, 2024
2 parents 1af8b03 + aa0e1cc commit 4625b23
Show file tree
Hide file tree
Showing 22 changed files with 327 additions and 158 deletions.
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,42 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [0.4.0](https://github.com/prismicio/prismic-mock/compare/v0.3.9...v0.4.0) (2024-09-26)

### [0.3.8-alpha.1](https://github.com/prismicio/prismic-mock/compare/v0.3.7...v0.3.8-alpha.1) (2024-08-29)


### Features

* add text to content relationship value ([a50ac3d](https://github.com/prismicio/prismic-mock/commit/a50ac3d974201e46e67fbcb79bd03356126e3c04))
* add text to link model ([649b6af](https://github.com/prismicio/prismic-mock/commit/649b6af91af18520ab267cab700e4ffc059688f6))
* add text to link to media model ([6d730f3](https://github.com/prismicio/prismic-mock/commit/6d730f366bd0c0ea99ef622b7163d7a01ea72fef))
* add text to link to media value ([73bcab4](https://github.com/prismicio/prismic-mock/commit/73bcab49b0cf35b23464fb2192c3b48e54e9f1e3))
* add text to link value ([253515f](https://github.com/prismicio/prismic-mock/commit/253515fa285d1e3a116347845d4023ab3e77f231))
* text to content relationship model ([82f9b29](https://github.com/prismicio/prismic-mock/commit/82f9b2975e8efab2f9b158abdb214fff56f40a50))


### Bug Fixes

* remove unneeded script ([642528c](https://github.com/prismicio/prismic-mock/commit/642528c3c533e11174abe1f0ecb60b0e877bafa2))


### Refactor

* simplify the new script ([01f7cb0](https://github.com/prismicio/prismic-mock/commit/01f7cb008bcd8c5c27a1e368fcdfdf57d555d1e5))
* update with review suggestions ([7fcafdc](https://github.com/prismicio/prismic-mock/commit/7fcafdc88c3d1e35e9b2a1e1e5459af324106880))


### Chore

* add unit test watch script ([53b9cac](https://github.com/prismicio/prismic-mock/commit/53b9cac354f7f8373df9eb53fd1752bbc8330563))
* **release:** 0.3.8-alpha.0 ([ff1a750](https://github.com/prismicio/prismic-mock/commit/ff1a750c294ff0ad57f6cc08e20b403c31723091))
* **release:** 0.3.8-alpha.0 ([988a644](https://github.com/prismicio/prismic-mock/commit/988a644519fb64712739833e3338a46ffee9d8ee))
* **release:** 0.3.8-alpha.1 ([558c535](https://github.com/prismicio/prismic-mock/commit/558c53595af38e6fa07b84c9a71d2a91d3d4cc9d))
* remove script ([20c870c](https://github.com/prismicio/prismic-mock/commit/20c870ca19deac7147cec69dd1493d24b8ce9018))
* update alpha dep ([a018931](https://github.com/prismicio/prismic-mock/commit/a018931393b4ceb0d7c46841534a31d2c12658dc))
* update deps ([a26d51b](https://github.com/prismicio/prismic-mock/commit/a26d51b08a8e5baa4db61d3d13d821ac220c4c5c))

### [0.3.9](https://github.com/prismicio/prismic-mock/compare/v0.3.8...v0.3.9) (2024-09-06)


Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prismicio/mock",
"version": "0.3.9",
"version": "0.4.0",
"description": "Generate mock Prismic documents, fields, Slices, and models for development and testing environments",
"keywords": [
"typescript",
Expand Down Expand Up @@ -55,13 +55,14 @@
"size": "size-limit",
"test": "npm run lint && npm run types && npm run unit && npm run build && npm run size",
"types": "tsc --noEmit",
"unit": "nyc --reporter=lcovonly --reporter=text --exclude-after-remap=false ava"
"unit": "nyc --reporter=lcovonly --reporter=text --exclude-after-remap=false ava",
"unit:watch": "npm run unit -- --watch"
},
"dependencies": {
"change-case": "^5.4.4"
},
"devDependencies": {
"@prismicio/client": "^7.8.1",
"@prismicio/client": "7.11.0",
"@size-limit/preset-small-lib": "^11.1.2",
"@types/node": "^20.12.7",
"@typescript-eslint/eslint-plugin": "^7.6.0",
Expand Down
6 changes: 4 additions & 2 deletions src/lib/buildContentRelationshipField.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import * as prismic from "@prismicio/client";

type BuildEmbedFieldConfig<Document extends prismic.PrismicDocument> = {
type BuildContentRelationshipFieldConfig<
Document extends prismic.PrismicDocument,
> = {
document: Document;
};

export const buildContentRelationshipField = <
Document extends prismic.PrismicDocument,
>(
config: BuildEmbedFieldConfig<Document>,
config: BuildContentRelationshipFieldConfig<Document>,
): prismic.ContentRelationshipField<
Document["type"],
Document["lang"],
Expand Down
42 changes: 27 additions & 15 deletions src/model/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@ import { createFaker } from "../lib/createFaker";

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

export type MockLinkModel<AllowTargetBlank extends boolean = boolean> =
prismic.CustomTypeModelLinkField & {
config: AllowTargetBlank extends true
? {
allowTargetBlank: true;
}
: {
allowTargetBlank?: undefined;
};
};
type MockLinkModel<
AllowTargetBlank extends boolean = boolean,
AllowText extends boolean = boolean,
> = prismic.CustomTypeModelLinkField & {
config: AllowTargetBlank extends true
? { allowTargetBlank: true }
: { allowTargetBlank?: undefined };
} & {
config: AllowText extends true
? { allowText: true }
: { allowText?: undefined };
};

export type MockLinkModelConfig<AllowTargetBlank extends boolean = boolean> = {
export type MockLinkModelConfig<
AllowTargetBlank extends boolean = boolean,
AllowText extends boolean = boolean,
> = {
allowTargetBlank?: AllowTargetBlank;
allowText?: AllowText;
} & MockModelConfig;

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

return {
Expand All @@ -35,6 +44,9 @@ export const link = <AllowTargetBlank extends boolean = boolean>(
("allowTargetBlank" in config
? config.allowTargetBlank
: faker.boolean()) || undefined,
allowText:
("allowText" in config ? config.allowText : faker.boolean()) ||
undefined,
},
} as MockLinkModel<AllowTargetBlank>;
} as MockLinkModel<AllowTargetBlank, AllowText>;
};
22 changes: 17 additions & 5 deletions src/model/linkToMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ import { createFaker } from "../lib/createFaker";

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

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

export type MockLinkToMediaModelConfig<AllowText extends boolean = boolean> = {
allowText?: AllowText;
} & MockModelConfig;

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

return {
Expand All @@ -18,6 +27,9 @@ export const linkToMedia = (
label: changeCase.capitalCase(faker.word()),
placeholder: changeCase.sentenceCase(faker.words(3)),
select: prismic.CustomTypeModelLinkSelectType.Media,
allowText:
("allowText" in config ? config.allowText : faker.boolean()) ||
undefined,
},
};
} as MockLinkToMediaModel<AllowText>;
};
9 changes: 9 additions & 0 deletions src/value/link.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as prismic from "@prismicio/client";
import * as changeCase from "change-case";

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

Expand All @@ -22,6 +23,9 @@ export type MockLinkValueConfig<
>["allowTargetBlank"] extends undefined
? false
: boolean;
withText?: NonNullable<Model["config"]>["allowText"] extends undefined
? false
: boolean;
/**
* A list of potential documents to which the field can be linked.
*/
Expand Down Expand Up @@ -82,6 +86,7 @@ export const link = <
return linkToMedia({
faker,
state: config.state,
withText: config.withText,
}) as MockLinkValue<LinkType, State>;
}

Expand All @@ -97,6 +102,10 @@ export const link = <
(model.config?.allowTargetBlank && faker.boolean())
? "_blank"
: undefined,
text:
config.withText ?? (model.config?.allowText && faker.boolean())
? changeCase.sentenceCase(faker.words(2))
: undefined,
} as MockLinkValue<LinkType, State>;
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/value/linkToMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import { createFaker } from "../lib/createFaker";

import { MockValueStateConfig, MockValueConfig } from "../types";

import * as modelGen from "../model";

export type MockLinkToMediaValueConfig<
Model extends
prismic.CustomTypeModelLinkToMediaField = prismic.CustomTypeModelLinkToMediaField,
State extends prismic.FieldState = prismic.FieldState,
> = MockValueConfig<Model> & MockValueStateConfig<State>;
> = {
withText?: NonNullable<Model["config"]>["allowText"] extends undefined
? false
: boolean;
} & MockValueConfig<Model> &
MockValueStateConfig<State>;

type MockLinkToMediaValue<
State extends prismic.FieldState = prismic.FieldState,
Expand All @@ -29,6 +36,8 @@ export const linkToMedia = <
link_type: prismic.LinkType.Media,
} as MockLinkToMediaValue<State>;
} else {
const model = config.model || modelGen.link({ faker });

return {
link_type: prismic.LinkType.Media,
id: faker.hash(11),
Expand All @@ -38,6 +47,10 @@ export const linkToMedia = <
size: faker.range(500, 3000).toString(),
height: faker.range(500, 3000).toString(),
width: faker.range(500, 3000).toString(),
text:
config.withText ?? (model.config?.allowText && faker.boolean())
? changeCase.sentenceCase(faker.words(2))
: undefined,
} as MockLinkToMediaValue<State>;
}
};
14 changes: 14 additions & 0 deletions test/model-link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,17 @@ test("can be configured to explicitly support blank target", (t) => {
});
t.is(actualFalse.config.allowTargetBlank, undefined);
});

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

const actualFalse = model.link({
seed: t.title,
allowText: false,
});
t.is(actualFalse.config.allowText, undefined);
});
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,
allowText: true,
});
t.is(actualTrue.config.allowText, true);

const actualFalse = model.linkToMedia({
seed: t.title,
allowText: false,
});
t.is(actualFalse.config.allowText, undefined);
});
Loading

0 comments on commit 4625b23

Please sign in to comment.