Skip to content

Commit

Permalink
feat: support groups in groups (#21)
Browse files Browse the repository at this point in the history
* feat: support groups in groups

* chore: relese alpha as patch

* fix: use correct group type

* chore(release): 0.3.7-alpha.0

* fix: update `buildMockGroupFieldMap` to return nested group fields

* chore(release): 0.3.7-alpha.1

* chore(deps): update @prismicio/client

---------

Co-authored-by: Xavier Rutayisire <[email protected]>
  • Loading branch information
angeloashmore and xrutayisire authored Jun 20, 2024
1 parent 2a7f0cb commit 301f0dd
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 64 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@

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.3.7-alpha.1](https://github.com/prismicio/prismic-mock/compare/v0.3.7-alpha.0...v0.3.7-alpha.1) (2024-06-05)


### Bug Fixes

* update `buildMockGroupFieldMap` to return nested group fields ([4830095](https://github.com/prismicio/prismic-mock/commit/4830095cbf208c5f1608cb49287db6f940598e88))

### [0.3.7-alpha.0](https://github.com/prismicio/prismic-mock/compare/v0.3.6...v0.3.7-alpha.0) (2024-06-05)


### Features

* support groups in groups ([42b0811](https://github.com/prismicio/prismic-mock/commit/42b081195a1e56d0cdaaa9b29958d27401aa300d))


### Bug Fixes

* use correct group type ([72b9a39](https://github.com/prismicio/prismic-mock/commit/72b9a3975c87ad1f9ca845a40f4a27d9e81e3b9c))


### Chore

* relese alpha as patch ([c7cb3d8](https://github.com/prismicio/prismic-mock/commit/c7cb3d8c1af9347cb8482d47a09a423880fca09a))

### [0.3.6](https://github.com/prismicio/prismic-mock/compare/v0.3.5...v0.3.6) (2024-04-26)


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.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prismicio/mock",
"version": "0.3.6",
"version": "0.3.7-alpha.1",
"description": "Generate mock Prismic documents, fields, Slices, and models for development and testing environments",
"keywords": [
"typescript",
Expand Down Expand Up @@ -49,8 +49,8 @@
"lint": "eslint --ext .js,.ts .",
"prepare": "npm run build",
"release": "npm run test && standard-version && git push --follow-tags && npm run build && npm publish",
"release:alpha": "npm run test && standard-version --release-as major --prerelease alpha && git push --follow-tags && npm run build && npm publish --tag alpha",
"release:alpha:dry": "standard-version --release-as major --prerelease alpha --dry-run",
"release:alpha": "npm run test && standard-version --release-as patch --prerelease alpha && git push --follow-tags && npm run build && npm publish --tag alpha",
"release:alpha:dry": "standard-version --release-as patch --prerelease alpha --dry-run",
"release:dry": "standard-version --dry-run",
"size": "size-limit",
"test": "npm run lint && npm run types && npm run unit && npm run build && npm run size",
Expand All @@ -61,7 +61,7 @@
"change-case": "^5.4.4"
},
"devDependencies": {
"@prismicio/client": "^7.5.0-alpha.1",
"@prismicio/client": "^7.6.0",
"@size-limit/preset-small-lib": "^11.1.2",
"@types/node": "^20.12.7",
"@typescript-eslint/eslint-plugin": "^7.6.0",
Expand Down
8 changes: 4 additions & 4 deletions src/model/buildMockGroupFieldMap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GroupFieldModelMap, MockModelConfig, ValueOf } from "../types";
import { MockModelConfig, NestedGroupFieldModelMap, ValueOf } from "../types";

import { boolean } from "../model/boolean";
import { color } from "../model/color";
Expand Down Expand Up @@ -43,10 +43,10 @@ export type BuildMockGroupFieldMapConfig = MockModelConfig;

export const buildMockGroupFieldMap = (
config: BuildMockGroupFieldMapConfig,
): GroupFieldModelMap => {
): NestedGroupFieldModelMap => {
const faker = config.faker || createFaker(config.seed);

const fields: GroupFieldModelMap = {};
const fields: NestedGroupFieldModelMap = {};

const fieldTypes = faker.randomElements(
Object.keys(mockModelFns) as (keyof typeof mockModelFns)[],
Expand All @@ -56,7 +56,7 @@ export const buildMockGroupFieldMap = (
const fieldId = generateFieldId({ faker });
const mockModelFn = mockModelFns[fieldType] as (
config: MockModelConfig,
) => ValueOf<GroupFieldModelMap>;
) => ValueOf<NestedGroupFieldModelMap>;

fields[fieldId] = mockModelFn({ faker });
}
Expand Down
7 changes: 4 additions & 3 deletions src/model/createModelMockFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createFaker, Faker } from "../lib/createFaker";

import {
GroupFieldModelMap,
NestedGroupFieldModelMap,
Seed,
SlicePrimaryFieldModelMap,
WithoutFakerConfig,
Expand Down Expand Up @@ -170,7 +171,7 @@ export class ModelMockFactory {
sharedSliceVariation<
ID extends string,
PrimaryFields extends SlicePrimaryFieldModelMap,
ItemsFields extends GroupFieldModelMap,
ItemsFields extends NestedGroupFieldModelMap,
>(
config?: WithoutFakerConfig<
MockSharedSliceVariationModelConfig<ID, PrimaryFields, ItemsFields>
Expand All @@ -180,8 +181,8 @@ export class ModelMockFactory {
}

slice<
NonRepeatFields extends GroupFieldModelMap,
RepeatFields extends GroupFieldModelMap,
NonRepeatFields extends NestedGroupFieldModelMap,
RepeatFields extends NestedGroupFieldModelMap,
>(
config?: WithoutFakerConfig<
MockSliceModelConfig<NonRepeatFields, RepeatFields>
Expand Down
6 changes: 3 additions & 3 deletions src/model/sharedSliceVariation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { createFaker } from "../lib/createFaker";
import { getMockImageData } from "../lib/getMockImageData";

import {
GroupFieldModelMap,
MockModelConfig,
NestedGroupFieldModelMap,
SlicePrimaryFieldModelMap,
} from "../types";

export type MockSharedSliceVariationModelConfig<
ID extends string = string,
PrimaryFields extends SlicePrimaryFieldModelMap = SlicePrimaryFieldModelMap,
ItemsFields extends GroupFieldModelMap = GroupFieldModelMap,
ItemsFields extends NestedGroupFieldModelMap = NestedGroupFieldModelMap,
> = {
id?: ID;
name?: string;
Expand All @@ -24,7 +24,7 @@ export type MockSharedSliceVariationModelConfig<
export const sharedSliceVariation = <
ID extends string,
PrimaryFields extends SlicePrimaryFieldModelMap,
ItemsFields extends GroupFieldModelMap,
ItemsFields extends NestedGroupFieldModelMap,
>(
config: MockSharedSliceVariationModelConfig<ID, PrimaryFields, ItemsFields>,
): prismic.SharedSliceModelVariation<ID, PrimaryFields, ItemsFields> => {
Expand Down
10 changes: 5 additions & 5 deletions src/model/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import * as changeCase from "change-case";

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

import { GroupFieldModelMap, MockModelConfig } from "../types";
import { MockModelConfig, NestedGroupFieldModelMap } from "../types";

export type MockSliceModelConfig<
NonRepeatFields extends GroupFieldModelMap = GroupFieldModelMap,
RepeatFields extends GroupFieldModelMap = GroupFieldModelMap,
NonRepeatFields extends NestedGroupFieldModelMap = NestedGroupFieldModelMap,
RepeatFields extends NestedGroupFieldModelMap = NestedGroupFieldModelMap,
> = {
nonRepeatFields?: NonRepeatFields;
repeatFields?: RepeatFields;
} & MockModelConfig;

export const slice = <
NonRepeatFields extends GroupFieldModelMap,
RepeatFields extends GroupFieldModelMap,
NonRepeatFields extends NestedGroupFieldModelMap,
RepeatFields extends NestedGroupFieldModelMap,
>(
config: MockSliceModelConfig<NonRepeatFields, RepeatFields>,
): prismic.CustomTypeModelSlice<NonRepeatFields, RepeatFields> => {
Expand Down
98 changes: 59 additions & 39 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export type GroupFieldModelMap = Record<
prismic.CustomTypeModelFieldForGroup
>;

export type NestedGroupFieldModelMap = Record<
string,
prismic.CustomTypeModelFieldForNestedGroup
>;

export type SlicePrimaryFieldModelMap = Record<
string,
prismic.CustomTypeModelFieldForSlicePrimary
Expand Down Expand Up @@ -225,45 +230,52 @@ type CustomTypeModelValue<T extends prismic.CustomTypeModel> =
type CustomTypeModelFieldForGroupValue<
T extends prismic.CustomTypeModelFieldForGroup,
State extends prismic.FieldState = prismic.FieldState,
> = T extends prismic.CustomTypeModelBooleanField
? prismic.BooleanField
: T extends prismic.CustomTypeModelColorField
? prismic.ColorField<State>
: T extends prismic.CustomTypeModelRichTextField
? prismic.RichTextField<State>
: T extends prismic.CustomTypeModelTitleField
? prismic.TitleField<State>
: T extends prismic.CustomTypeModelImageField<infer TThumbnailNames>
? prismic.ImageField<TThumbnailNames, State>
: T extends prismic.CustomTypeModelLinkField
? prismic.LinkField<string, string, never, State>
: T extends prismic.CustomTypeModelLinkToMediaField
? prismic.LinkToMediaField<State>
: T extends prismic.CustomTypeModelContentRelationshipField
? prismic.ContentRelationshipField<string, string, never, State>
: T extends prismic.CustomTypeModelDateField
? prismic.DateField<State>
: T extends prismic.CustomTypeModelTimestampField
? prismic.TimestampField<State>
: T extends prismic.CustomTypeModelNumberField
? prismic.NumberField<State>
: T extends prismic.CustomTypeModelKeyTextField
? prismic.KeyTextField<State>
: T extends prismic.CustomTypeModelSelectField
? prismic.SelectField<string, State>
: T extends prismic.CustomTypeModelEmbedField
? prismic.EmbedField<
prismic.AnyOEmbed & prismic.OEmbedExtra,
State
>
: T extends prismic.CustomTypeModelGeoPointField
? prismic.GeoPointField<State>
: T extends prismic.CustomTypeModelIntegrationField
? prismic.IntegrationField<
Record<string, unknown>,
State
>
: never;
> = T extends prismic.CustomTypeModelNestedGroupField
? CustomTypeModelNestedGroupFieldValue<T, State>
: T extends prismic.CustomTypeModelBooleanField
? prismic.BooleanField
: T extends prismic.CustomTypeModelColorField
? prismic.ColorField<State>
: T extends prismic.CustomTypeModelRichTextField
? prismic.RichTextField<State>
: T extends prismic.CustomTypeModelTitleField
? prismic.TitleField<State>
: T extends prismic.CustomTypeModelImageField<infer TThumbnailNames>
? prismic.ImageField<TThumbnailNames, State>
: T extends prismic.CustomTypeModelLinkField
? prismic.LinkField<string, string, never, State>
: T extends prismic.CustomTypeModelLinkToMediaField
? prismic.LinkToMediaField<State>
: T extends prismic.CustomTypeModelContentRelationshipField
? prismic.ContentRelationshipField<
string,
string,
never,
State
>
: T extends prismic.CustomTypeModelDateField
? prismic.DateField<State>
: T extends prismic.CustomTypeModelTimestampField
? prismic.TimestampField<State>
: T extends prismic.CustomTypeModelNumberField
? prismic.NumberField<State>
: T extends prismic.CustomTypeModelKeyTextField
? prismic.KeyTextField<State>
: T extends prismic.CustomTypeModelSelectField
? prismic.SelectField<string, State>
: T extends prismic.CustomTypeModelEmbedField
? prismic.EmbedField<
prismic.AnyOEmbed & prismic.OEmbedExtra,
State
>
: T extends prismic.CustomTypeModelGeoPointField
? prismic.GeoPointField<State>
: T extends prismic.CustomTypeModelIntegrationField
? prismic.IntegrationField<
Record<string, unknown>,
State
>
: never;

type CustomTypeModelGroupFieldValue<
T extends prismic.CustomTypeModelGroupField,
Expand All @@ -273,6 +285,14 @@ type CustomTypeModelGroupFieldValue<
State
>;

type CustomTypeModelNestedGroupFieldValue<
T extends prismic.CustomTypeModelNestedGroupField,
State extends prismic.FieldState = prismic.FieldState,
> = prismic.NestedGroupField<
ModelValueMap<NonNullable<NonNullable<T["config"]>["fields"]>>,
State
>;

type CustomTypeModelSliceValue<
T extends prismic.CustomTypeModelSlice,
SliceType = string,
Expand Down
23 changes: 23 additions & 0 deletions test/model-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,26 @@ test("can be configured for specific fields", (t) => {
prismic.CustomTypeModelFieldType.Boolean,
);
});

test("supports nested groups", (t) => {
const actual = model.group({
seed: t.title,
fields: {
group: model.group({
seed: t.title,
fields: {
boolean: model.boolean({ seed: t.title }),
},
}),
},
});

t.is(
actual.config?.fields?.group.type,
prismic.CustomTypeModelFieldType.Group,
);
t.is(
actual.config?.fields?.group.config?.fields?.boolean.type,
prismic.CustomTypeModelFieldType.Boolean,
);
});
25 changes: 25 additions & 0 deletions test/value-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ test("supports custom model", (t) => {
}
});

test("supports nested groups", (t) => {
const customModel = model.group({
seed: t.title,
fields: {
group: model.group({
seed: t.title,
fields: {
boolean: model.boolean({ seed: t.title }),
},
}),
},
});

const actual = value.group({
seed: t.title,
model: customModel,
});

for (const item of actual) {
for (const nestedItem of item.group) {
t.is(typeof nestedItem.boolean, "boolean");
}
}
});

test("can be customized with a specific number of items", (t) => {
const actual = value.group({
seed: t.title,
Expand Down

0 comments on commit 301f0dd

Please sign in to comment.