Skip to content

Commit

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

* test: verify groups are supported in slices
  • Loading branch information
angeloashmore authored Apr 18, 2024
1 parent 63d1e17 commit 56e7670
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/model/createModelMockFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import * as prismic from "@prismicio/client";

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

import { GroupFieldModelMap, Seed, WithoutFakerConfig } from "../types";
import {
GroupFieldModelMap,
Seed,
SlicePrimaryFieldModelMap,
WithoutFakerConfig,
} from "../types";

import { boolean, MockBooleanModelConfig } from "./boolean";
import {
Expand Down Expand Up @@ -164,7 +169,7 @@ export class ModelMockFactory {

sharedSliceVariation<
ID extends string,
PrimaryFields extends GroupFieldModelMap,
PrimaryFields extends SlicePrimaryFieldModelMap,
ItemsFields extends GroupFieldModelMap,
>(
config?: WithoutFakerConfig<
Expand Down
10 changes: 7 additions & 3 deletions src/model/sharedSliceVariation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import * as changeCase from "change-case";
import { createFaker } from "../lib/createFaker";
import { getMockImageData } from "../lib/getMockImageData";

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

export type MockSharedSliceVariationModelConfig<
ID extends string = string,
PrimaryFields extends GroupFieldModelMap = GroupFieldModelMap,
PrimaryFields extends SlicePrimaryFieldModelMap = SlicePrimaryFieldModelMap,
ItemsFields extends GroupFieldModelMap = GroupFieldModelMap,
> = {
id?: ID;
Expand All @@ -19,7 +23,7 @@ export type MockSharedSliceVariationModelConfig<

export const sharedSliceVariation = <
ID extends string,
PrimaryFields extends GroupFieldModelMap,
PrimaryFields extends SlicePrimaryFieldModelMap,
ItemsFields extends GroupFieldModelMap,
>(
config: MockSharedSliceVariationModelConfig<ID, PrimaryFields, ItemsFields>,
Expand Down
5 changes: 5 additions & 0 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 SlicePrimaryFieldModelMap = Record<
string,
prismic.CustomTypeModelFieldForSlicePrimary
>;

export type MockValueConfig<Model extends PrismicModel = PrismicModel> = {
model?: Model;
} & (
Expand Down
11 changes: 11 additions & 0 deletions test/model-sharedSliceVariation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,23 @@ test("can be configured for specific primary and items fields", (t) => {
seed: t.title,
primaryFields: {
boolean: prismicM.model.boolean({ seed: t.title }),
group: prismicM.model.group({
seed: t.title,
fields: {
boolean: prismicM.model.boolean({ seed: t.title }),
},
}),
},
itemsFields: {
keyText: prismicM.model.keyText({ seed: t.title }),
},
});

t.is(actual.primary?.boolean.type, prismic.CustomTypeModelFieldType.Boolean);
t.is(actual.primary?.group.type, prismic.CustomTypeModelFieldType.Group);
t.is(
actual.primary?.group.config?.fields?.boolean.type,
prismic.CustomTypeModelFieldType.Boolean,
);
t.is(actual.items?.keyText.type, prismic.CustomTypeModelFieldType.Text);
});
10 changes: 10 additions & 0 deletions test/value-sharedSliceVariation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ test("supports custom model", (t) => {
seed: t.title,
primaryFields: {
boolean: model.boolean({ seed: t.title }),
group: model.group({
seed: t.title,
fields: {
boolean: model.boolean({ seed: t.title }),
},
}),
},
itemsFields: {
keyText: model.keyText({ seed: t.title }),
Expand All @@ -32,6 +38,10 @@ test("supports custom model", (t) => {
});

t.is(typeof actual.primary.boolean, "boolean");
t.is(Array.isArray(actual.primary.group), true);
for (const item of actual.primary.group) {
t.is(typeof item.boolean, "boolean");
}

for (const item of actual.items) {
t.is(typeof item.keyText, "string");
Expand Down

0 comments on commit 56e7670

Please sign in to comment.