Skip to content

Commit

Permalink
feat: support format option in custom type models (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore authored Apr 9, 2024
1 parent 1c7e800 commit 3b72c7f
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 106 deletions.
68 changes: 15 additions & 53 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"change-case": "^4.1.2"
},
"devDependencies": {
"@prismicio/client": "^7.0.1",
"@prismicio/client": "^7.4.0",
"@size-limit/preset-small-lib": "^8.2.4",
"@types/node": "^20.3.0",
"@typescript-eslint/eslint-plugin": "^5.59.8",
Expand Down
4 changes: 4 additions & 0 deletions src/model/customType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type MockCustomTypeModelConfig<
label?: string;
status?: boolean;
repeatable?: boolean;
format?: prismic.CustomTypeModel["format"];
} & (
| {
fields?: Definition;
Expand Down Expand Up @@ -57,6 +58,8 @@ export const customType = <
id = changeCase.snakeCase(config.label);
}

const format = config.format ?? faker.randomElement(["page", "custom"]);

let json = {} as MockCustomTypeModel<Definition>["json"];

if ("fields" in config && config.fields) {
Expand All @@ -70,6 +73,7 @@ export const customType = <
label,
status: config.status ?? faker.boolean(),
repeatable: config.repeatable ?? faker.boolean(),
format,
json,
} as MockCustomTypeModel<Definition>;
};
9 changes: 9 additions & 0 deletions test/model-customType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ test("can be configured for specific tabs", (t) => {

t.is(actual.json.Main.boolean.type, prismic.CustomTypeModelFieldType.Boolean);
});

test("can be configured for a specific format", (t) => {
const actual = prismicM.model.customType({
seed: t.title,
format: "page",
});

t.is(actual.format, "page");
});
8 changes: 6 additions & 2 deletions test/snapshots/model-customType.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ Generated by [AVA](https://avajs.dev).
[
{
format: 'page',
id: 'id',
json: {},
label: 'Id',
repeatable: true,
status: false,
status: true,
},
{
format: 'page',
id: 'id',
json: {},
label: 'Id',
repeatable: true,
status: false,
status: true,
},
]

Expand All @@ -31,13 +33,15 @@ Generated by [AVA](https://avajs.dev).
[
{
format: 'custom',
id: 'ut',
json: {},
label: 'Ut',
repeatable: true,
status: true,
},
{
format: 'custom',
id: 'ut',
json: {},
label: 'Ut',
Expand Down
Binary file modified test/snapshots/model-customType.test.ts.snap
Binary file not shown.
40 changes: 18 additions & 22 deletions test/snapshots/value-contentRelationship.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ Generated by [AVA](https://avajs.dev).
[
{
id: '8b62bcd',
id: 'f3c898f',
isBroken: false,
lang: 'non',
lang: 'accumsan',
link_type: 'Document',
slug: undefined,
tags: [],
type: 'morbi_tristique',
type: 'eget',
uid: undefined,
url: 'https://accumsan.example',
url: 'https://cursus.example',
},
{
id: '8b62bcd',
id: 'f3c898f',
isBroken: false,
lang: 'non',
lang: 'accumsan',
link_type: 'Document',
slug: undefined,
tags: [],
type: 'morbi_tristique',
type: 'eget',
uid: undefined,
url: 'https://accumsan.example',
url: 'https://cursus.example',
},
]

Expand All @@ -39,29 +39,25 @@ Generated by [AVA](https://avajs.dev).
[
{
id: '98a8a9e',
id: 'be1d2fe',
isBroken: false,
lang: 'tristique',
lang: 'sed',
link_type: 'Document',
slug: undefined,
tags: [
'Nisl',
],
type: 'leo_duis',
tags: [],
type: 'viverra',
uid: undefined,
url: 'https://nibh.example',
url: 'https://sit.example',
},
{
id: '98a8a9e',
id: 'be1d2fe',
isBroken: false,
lang: 'tristique',
lang: 'sed',
link_type: 'Document',
slug: undefined,
tags: [
'Nisl',
],
type: 'leo_duis',
tags: [],
type: 'viverra',
uid: undefined,
url: 'https://nibh.example',
url: 'https://sit.example',
},
]
Binary file modified test/snapshots/value-contentRelationship.test.ts.snap
Binary file not shown.
64 changes: 36 additions & 28 deletions test/snapshots/value-customType.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,36 @@ Generated by [AVA](https://avajs.dev).
{
alternate_languages: [],
data: {},
first_publication_date: '2023-06-16T08:11:27.583Z',
href: 'https://blandit.example',
id: 'ed1cced',
lang: 'id',
last_publication_date: '2020-04-28T02:02:17.347Z',
first_publication_date: '2020-05-15T13:19:44.171Z',
href: 'https://id.example',
id: 'da659ff',
lang: 'velit',
last_publication_date: '2024-03-26T12:40:04.647Z',
linked_documents: [],
slugs: [],
tags: [],
tags: [
'Eget',
],
type: 'viverra',
uid: null,
url: 'https://odio.example',
url: 'https://blandit.example',
},
{
alternate_languages: [],
data: {},
first_publication_date: '2023-06-16T08:11:27.583Z',
href: 'https://blandit.example',
id: 'ed1cced',
lang: 'id',
last_publication_date: '2020-04-28T02:02:17.347Z',
first_publication_date: '2020-05-15T13:19:44.171Z',
href: 'https://id.example',
id: 'da659ff',
lang: 'velit',
last_publication_date: '2024-03-26T12:40:04.647Z',
linked_documents: [],
slugs: [],
tags: [],
tags: [
'Eget',
],
type: 'viverra',
uid: null,
url: 'https://odio.example',
url: 'https://blandit.example',
},
]

Expand All @@ -49,31 +53,35 @@ Generated by [AVA](https://avajs.dev).
{
alternate_languages: [],
data: {},
first_publication_date: '2024-09-20T09:25:17.265Z',
href: 'https://nulla.example',
id: 'd98a8a9',
lang: 'nibh',
last_publication_date: '2019-05-24T09:29:21.573Z',
first_publication_date: '2020-06-01T20:23:59.940Z',
href: 'https://nibh.example',
id: 'bbe1d2f',
lang: 'sit',
last_publication_date: '2023-08-23T09:11:23.239Z',
linked_documents: [],
slugs: [],
tags: [],
tags: [
'Nec',
],
type: 'ut',
uid: null,
url: 'https://quam.example',
url: 'https://nulla.example',
},
{
alternate_languages: [],
data: {},
first_publication_date: '2024-09-20T09:25:17.265Z',
href: 'https://nulla.example',
id: 'd98a8a9',
lang: 'nibh',
last_publication_date: '2019-05-24T09:29:21.573Z',
first_publication_date: '2020-06-01T20:23:59.940Z',
href: 'https://nibh.example',
id: 'bbe1d2f',
lang: 'sit',
last_publication_date: '2023-08-23T09:11:23.239Z',
linked_documents: [],
slugs: [],
tags: [],
tags: [
'Nec',
],
type: 'ut',
uid: null,
url: 'https://quam.example',
url: 'https://nulla.example',
},
]
Binary file modified test/snapshots/value-customType.test.ts.snap
Binary file not shown.

0 comments on commit 3b72c7f

Please sign in to comment.