Skip to content

Commit

Permalink
feat: add remaining field model generators
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Aug 6, 2021
1 parent f20e12c commit 66b5ee5
Show file tree
Hide file tree
Showing 19 changed files with 2,000 additions and 203 deletions.
97 changes: 0 additions & 97 deletions src/buildRandomGroupFieldMap.ts

This file was deleted.

82 changes: 82 additions & 0 deletions src/lib/buildMockGroupFieldMap.ts
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;
};
10 changes: 8 additions & 2 deletions src/lib/generateFieldId.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as faker from "faker/locale/en_US";
import * as changeCase from "change-case";

export const generateFieldId = (): string => {
import { createFaker } from "../lib/createFaker";
import { MockModelConfig } from "../types";

type GenerateFieldIdConfig = Pick<MockModelConfig, "seed">;

export const generateFieldId = (config: GenerateFieldIdConfig = {}): string => {
const faker = createFaker(config.seed);

return changeCase.snakeCase(
faker.lorem.words(faker.datatype.number({ min: 1, max: 3 })),
);
Expand Down
47 changes: 28 additions & 19 deletions src/model/customType.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
import * as prismicT from "@prismicio/types";
import * as faker from "faker/locale/en_US";
import * as changeCase from "change-case";

import { generateFieldId } from "../lib/generateFieldId";
import { buildRandomGroupFieldMap } from "../buildRandomGroupFieldMap";
import { buildMockGroupFieldMap } from "../lib/buildMockGroupFieldMap";
import { createFaker } from "../lib/createFaker";

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

import { uid } from "./uid";
import { sliceZone } from "./sliceZone";

type CustomTypeArgs = {
type MockCustomTypeModelConfig = {
tabsCount?: number;
withUID?: boolean;
withSliceZones?: boolean;
withSharedSlices?: boolean;
};
} & (
| {
withSliceZones?: false;
}
| {
withSliceZones: true;
withSharedSlices?: boolean;
}
) &
MockModelConfig;

export const customType = (
args: CustomTypeArgs = {},
config: MockCustomTypeModelConfig = {},
): prismicT.CustomTypeModel => {
const tabsCount = args.tabsCount ?? faker.datatype.number({ min: 1, max: 3 });
const faker = createFaker(config.seed);

const json = {} as prismicT.CustomTypeModelDefinition;
for (let i = 0; i < tabsCount; i++) {
const tabName = changeCase.capitalCase(
faker.lorem.words(faker.datatype.number({ min: 1, max: 3 })),
);
const tabsCount =
config.tabsCount ?? faker.datatype.number({ min: 1, max: 3 });

const tabFields = buildRandomGroupFieldMap() as prismicT.CustomTypeModelTab;
const json: prismicT.CustomTypeModelDefinition = {};
for (let i = 0; i < tabsCount; i++) {
const tabName = generateFieldId({ seed: config.seed });
const tabFields: prismicT.CustomTypeModelTab = buildMockGroupFieldMap();

if (i === 0 && args.withUID) {
const fieldId = generateFieldId();
if (i === 0 && config.withUID) {
const fieldId = generateFieldId({ seed: config.seed });

tabFields[fieldId] = uid();
}

if (args.withSliceZones) {
const sliceZoneId = generateFieldId();
if (config.withSliceZones) {
const sliceZoneId = generateFieldId({ seed: config.seed });

tabFields[sliceZoneId] = sliceZone({
withSharedSlices: args.withSharedSlices,
withSharedSlices: config.withSharedSlices,
});
}

Expand Down
23 changes: 14 additions & 9 deletions src/model/group.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import * as prismicT from "@prismicio/types";
import * as faker from "faker/locale/en_US";
import * as changeCase from "change-case";

import {
buildRandomGroupFieldMap,
BuildRandomGroupFieldMapArgs,
} from "../buildRandomGroupFieldMap";
buildMockGroupFieldMap,
BuildMockGroupFieldMapConfig,
} from "../lib/buildMockGroupFieldMap";
import { createFaker } from "../lib/createFaker";

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

type GroupArgs = BuildRandomGroupFieldMapArgs;
type MockGroupModelConfig = BuildMockGroupFieldMapConfig & MockModelConfig;

export const group = (
args: GroupArgs = {},
config: MockGroupModelConfig = {},
): prismicT.CustomTypeModelGroupField => {
const fields = buildRandomGroupFieldMap({
fieldArgs: args.fieldArgs,
fieldCounts: args.fieldCounts,
const faker = createFaker(config.seed);

const fields = buildMockGroupFieldMap({
seed: config.seed,
configs: config.configs,
});

return {
Expand Down
4 changes: 3 additions & 1 deletion src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ export { keyText } from "./keyText";
export { link } from "./link";
export { linkToMedia } from "./linkToMedia";
export { number } from "./number";
export { richText } from "./richText";
export { select } from "./select";
export { sharedSlice } from "./sharedSlice";
export { sharedSliceChoice } from "./sharedSliceChoice";
export { sharedSliceVariation } from "./sharedSliceVariation";
export { sliceChoice } from "./sliceChoice";
export { slice } from "./slice";
export { sliceZone } from "./sliceZone";
export { timestamp } from "./timestamp";
export { title } from "./title";
export { uid } from "./uid";
51 changes: 51 additions & 0 deletions src/model/richText.ts
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;
};
Loading

0 comments on commit 66b5ee5

Please sign in to comment.