-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add remaining field model generators
- Loading branch information
1 parent
f20e12c
commit 66b5ee5
Showing
19 changed files
with
2,000 additions
and
203 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import * as prismicT from "@prismicio/types"; | ||
|
||
import { MockModelConfig } from "../types"; | ||
|
||
import { boolean } from "../model/boolean"; | ||
import { color } from "../model/color"; | ||
import { contentRelationship } from "../model/contentRelationship"; | ||
import { date } from "../model/date"; | ||
import { embed } from "../model/embed"; | ||
import { geoPoint } from "../model/geoPoint"; | ||
import { image } from "../model/image"; | ||
import { keyText } from "../model/keyText"; | ||
import { link } from "../model/link"; | ||
import { linkToMedia } from "../model/linkToMedia"; | ||
import { number } from "../model/number"; | ||
import { richText } from "../model/richText"; | ||
import { select } from "../model/select"; | ||
import { timestamp } from "../model/timestamp"; | ||
import { title } from "../model/title"; | ||
|
||
import { createFaker } from "./createFaker"; | ||
import { generateFieldId } from "./generateFieldId"; | ||
|
||
const mockModelFns = { | ||
boolean, | ||
color, | ||
contentRelationship, | ||
date, | ||
embed, | ||
geoPoint, | ||
image, | ||
keyText, | ||
link, | ||
linkToMedia, | ||
number, | ||
richText, | ||
select, | ||
timestamp, | ||
title, | ||
} as const; | ||
|
||
type MockModelFns = typeof mockModelFns; | ||
type MockModelTypes = keyof MockModelFns; | ||
|
||
export type BuildMockGroupFieldMapConfig = { | ||
configs?: { | ||
[P in keyof MockModelFns]?: { | ||
count?: number; | ||
config?: Parameters<MockModelFns[P]>[0]; | ||
}; | ||
}; | ||
} & MockModelConfig; | ||
|
||
export const buildMockGroupFieldMap = ( | ||
config: BuildMockGroupFieldMapConfig = {}, | ||
): Record<string, prismicT.CustomTypeModelFieldForGroup> => { | ||
const faker = createFaker(config.seed); | ||
|
||
const configs = | ||
config.configs || | ||
({} as NonNullable<BuildMockGroupFieldMapConfig["configs"]>); | ||
|
||
const fields: Record<string, prismicT.CustomTypeModelFieldForGroup> = {}; | ||
|
||
for (const mockModelType in mockModelFns) { | ||
const mockModelFn = mockModelFns[mockModelType as MockModelTypes]; | ||
const mockModelMapConfig = configs[mockModelType as MockModelTypes] || {}; | ||
const count = | ||
mockModelMapConfig.count ?? faker.random.arrayElement([0, 0, 0, 1]); | ||
|
||
for (let i = 0; i < count; i++) { | ||
const fieldId = generateFieldId({ seed: config.seed }); | ||
|
||
fields[fieldId] = mockModelFn({ | ||
seed: config.seed, | ||
...mockModelMapConfig.config, | ||
}); | ||
} | ||
} | ||
|
||
return fields; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as prismicT from "@prismicio/types"; | ||
import * as changeCase from "change-case"; | ||
|
||
import { createFaker } from "../lib/createFaker"; | ||
|
||
import { MockModelConfig } from "../types"; | ||
|
||
export type MockRichTextModelConfig = { | ||
withMultipleBlocks?: boolean; | ||
} & MockModelConfig; | ||
|
||
export const richText = ( | ||
config: MockRichTextModelConfig = {}, | ||
): prismicT.CustomTypeModelRichTextField => { | ||
const faker = createFaker(config.seed); | ||
|
||
const blockTypes = faker.random | ||
.arrayElements([ | ||
"heading1", | ||
"heading2", | ||
"heading3", | ||
"heading4", | ||
"heading5", | ||
"heading6", | ||
"paragraph", | ||
"preformatted", | ||
"strong", | ||
"em", | ||
"list-item", | ||
"o-list-item", | ||
"image", | ||
"embed", | ||
"hyperlink", | ||
]) | ||
.join(","); | ||
|
||
const blockTypeConfig = | ||
config.withMultipleBlocks ?? faker.datatype.boolean() | ||
? { multi: blockTypes } | ||
: { single: blockTypes }; | ||
|
||
return { | ||
type: prismicT.CustomTypeModelFieldType.StructuredText, | ||
config: { | ||
label: changeCase.capitalCase(faker.company.bsNoun()), | ||
placeholder: changeCase.sentenceCase(faker.lorem.words(3)), | ||
allowTargetBlank: faker.datatype.boolean() ? true : undefined, | ||
...blockTypeConfig, | ||
}, | ||
} as prismicT.CustomTypeModelRichTextField; | ||
}; |
Oops, something went wrong.