Skip to content

Commit

Permalink
fix: restore types (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore authored Jun 13, 2023
1 parent c3e11d3 commit bd7620f
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 56 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"release:alpha:dry": "standard-version --release-as major --prerelease alpha --dry-run",
"release:dry": "standard-version --dry-run",
"size": "size-limit",
"test": "npm run lint && npm run unit && npm run build && npm run size",
"test": "npm run lint && npm run types && npm run unit && npm run build && npm run size",
"types": "tsc --noEmit",
"unit": "nyc --reporter=lcovonly --reporter=text --exclude-after-remap=false ava"
},
"dependencies": {
Expand All @@ -62,6 +63,7 @@
"devDependencies": {
"@prismicio/client": "^7.0.1",
"@size-limit/preset-small-lib": "^8.2.4",
"@types/node": "^20.3.0",
"@typescript-eslint/eslint-plugin": "^5.59.8",
"@typescript-eslint/parser": "^5.59.8",
"ava": "^5.3.0",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/valueForModelMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const getValueConfigType = <Model extends prismic.CustomTypeModelField>(
case prismic.CustomTypeModelFieldType.UID:
return "uid";

case prismic.CustomTypeModelFieldType.IntegrationField:
case prismic.CustomTypeModelFieldType.Integration:
return "integration";

case prismic.CustomTypeModelFieldType.Slices:
Expand Down
16 changes: 8 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,18 @@ export type ModelValue<
: T extends prismic.CustomTypeModelSliceZoneField
? prismic.SliceZone<
ValueOf<{
[P in keyof NonNullable<T["config"]>["choices"] as P extends string
? P
: never]: NonNullable<
T["config"]
>["choices"][P] extends prismic.CustomTypeModelSlice
[P in keyof NonNullable<
NonNullable<T["config"]>["choices"]
> as P extends string ? P : never]: NonNullable<
NonNullable<T["config"]>["choices"]
>[P] extends prismic.CustomTypeModelSlice
? CustomTypeModelSliceValue<
NonNullable<T["config"]>["choices"][P],
NonNullable<NonNullable<T["config"]>["choices"]>[P],
P extends string ? P : string
>
: NonNullable<
T["config"]
>["choices"][P] extends prismic.CustomTypeModelSharedSlice
NonNullable<T["config"]>["choices"]
>[P] extends prismic.CustomTypeModelSharedSlice
? prismic.SharedSlice<P extends string ? P : string>
: never;
}>,
Expand Down
2 changes: 1 addition & 1 deletion src/value/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type MockLinkValue<
: LinkType extends typeof prismic.LinkType.Media
? prismic.FilledLinkToMediaField
: LinkType extends typeof prismic.LinkType.Document
? prismic.FilledLinkToDocumentField
? prismic.FilledContentRelationshipField
: never;

export const link = <
Expand Down
4 changes: 2 additions & 2 deletions test/model-contentRelationship.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test("can be configured to constrain by custom type", (t) => {
customTypeIDs,
});

t.is(actual.config.customtypes, customTypeIDs);
t.is(actual.config?.customtypes, customTypeIDs);
});

test("can be configured to constrain by tags", (t) => {
Expand All @@ -31,5 +31,5 @@ test("can be configured to constrain by tags", (t) => {
tags,
});

t.is(actual.config.tags, tags);
t.is(actual.config?.tags, tags);
});
2 changes: 1 addition & 1 deletion test/model-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test("can be configured for specific fields", (t) => {
});

t.is(
actual.config.fields.boolean.type,
actual.config?.fields?.boolean.type,
prismic.CustomTypeModelFieldType.Boolean,
);
});
8 changes: 4 additions & 4 deletions test/model-image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ test("can be configured to include constraints", (t) => {
withConstraint: true,
});

t.is(typeof actual.config.constraint.width, "number");
t.is(typeof actual.config.constraint.height, "number");
t.is(typeof actual.config?.constraint?.width, "number");
t.is(typeof actual.config?.constraint?.height, "number");
});

test("can be configured for a specific thumbnails", (t) => {
Expand All @@ -28,6 +28,6 @@ test("can be configured for a specific thumbnails", (t) => {
thumbnailNames: ["Foo"],
});

t.is(actual.config.thumbnails.length, 1);
t.is(actual.config.thumbnails[0].name, "Foo");
t.is(actual.config?.thumbnails?.length, 1);
t.is(actual.config?.thumbnails?.[0].name, "Foo");
});
4 changes: 2 additions & 2 deletions test/model-select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test("can be configured for a specific options", (t) => {
options,
});

t.is(actual.config.options, options);
t.is(actual.config?.options, options);
});

test("can be configured for a specific default value", (t) => {
Expand All @@ -29,5 +29,5 @@ test("can be configured for a specific default value", (t) => {
defaultValue: "foo",
});

t.is(actual.config.default_value, "foo");
t.is(actual.config?.default_value, "foo");
});
4 changes: 2 additions & 2 deletions test/model-sharedSliceVariation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ test("can be configured for specific primary and items fields", (t) => {
},
});

t.is(actual.primary.boolean.type, prismic.CustomTypeModelFieldType.Boolean);
t.is(actual.items.keyText.type, prismic.CustomTypeModelFieldType.Text);
t.is(actual.primary?.boolean.type, prismic.CustomTypeModelFieldType.Boolean);
t.is(actual.items?.keyText.type, prismic.CustomTypeModelFieldType.Text);
});
4 changes: 2 additions & 2 deletions test/model-slice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ test("can be configured for specific non-repeat and repeat fields", (t) => {
});

t.is(
actual["non-repeat"].boolean.type,
actual["non-repeat"]?.boolean.type,
prismic.CustomTypeModelFieldType.Boolean,
);
t.is(actual.repeat.keyText.type, prismic.CustomTypeModelFieldType.Text);
t.is(actual.repeat?.keyText.type, prismic.CustomTypeModelFieldType.Text);
});
2 changes: 1 addition & 1 deletion test/model-sliceZone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ test("can be configured to use specific choices", (t) => {
choices,
});

t.deepEqual(actual.config.choices, choices);
t.deepEqual(actual.config?.choices, choices);
});
16 changes: 10 additions & 6 deletions test/value-contentRelationship.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ test("supports custom model", (t) => {
});

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
t.true(customModel.config.customtypes!.includes(actual.type));
t.true(customModel.config?.customtypes!.includes(actual.type));

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
t.true(customModel.config.tags!.every((tag) => actual.tags.includes(tag)));
t.true(customModel.config?.tags!.every((tag) => actual.tags.includes(tag)));
});

test("can be configured to return an empty value", (t) => {
Expand Down Expand Up @@ -67,8 +67,10 @@ test("can be configured to return a link from a given list of documents with con
];

const customModel = model.contentRelationship({ seed: t.title });
customModel.config.customtypes = ["foo"];
customModel.config.tags = ["bar"];
if (customModel.config) {
customModel.config.customtypes = ["foo"];
customModel.config.tags = ["bar"];
}

const actual = value.contentRelationship({
seed: t.title,
Expand All @@ -89,8 +91,10 @@ test("throws if a linkable document cannot be found within constraints", (t) =>
];

const customModel = model.contentRelationship({ seed: t.title });
customModel.config.customtypes = ["foo"];
customModel.config.tags = ["bar"];
if (customModel.config) {
customModel.config.customtypes = ["foo"];
customModel.config.tags = ["bar"];
}

t.throws(
() =>
Expand Down
45 changes: 24 additions & 21 deletions test/value-image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,34 @@ test("supports number seed", snapshotTwiceMacro, () =>

test("can be configured to return an empty value", (t) => {
const customModel = model.image({ seed: t.title });
customModel.config.thumbnails = [
{
name: "Foo",
height: null,
width: null,
},
];
if (customModel.config) {
customModel.config.thumbnails = [
{
name: "Foo",
height: null,
width: null,
},
];
}

const actual = value.image({
seed: t.title,
model: customModel,
state: "empty",
});

t.deepEqual(
actual,
// TODO: Resolve the following type error
// @ts-expect-error - Unsure how to fix this type mismatch
{
t.deepEqual(actual, {
url: null,
alt: null,
copyright: null,
dimensions: null,
Foo: {
url: null,
alt: null,
copyright: null,
dimensions: null,
Foo: {
url: null,
alt: null,
copyright: null,
dimensions: null,
},
},
);
});
});

test("supports custom model", (t) => {
Expand All @@ -60,7 +57,13 @@ test("supports custom model", (t) => {
model: customModel,
});

t.is(actual.dimensions.width, customModel.config.constraint.width);
t.is(actual.dimensions.height, customModel.config.constraint.height);
t.is(
actual.dimensions.width,
customModel.config?.constraint?.width as number,
);
t.is(
actual.dimensions.height,
customModel.config?.constraint?.height as number,
);
t.true("Foo" in actual);
});
13 changes: 10 additions & 3 deletions test/value-richText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,29 @@ test("supports custom model", (t) => {
seed: t.title,
withMultipleBlocks: false,
});
customModel.config.single = "paragraph";
if (customModel.config) {
customModel.config.single = "paragraph";
}

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

t.is(actual[0].type, customModel.config.single);
t.is(
actual[0]?.type,
customModel.config?.single as (typeof actual)[number]["type"],
);
});

test("models without multiple blocks returns one block", (t) => {
const customModel = model.richText({
seed: t.title,
withMultipleBlocks: false,
});
customModel.config.single = "paragraph";
if (customModel.config) {
customModel.config.single = "paragraph";
}

const actual = value.richText({
seed: t.title,
Expand Down
2 changes: 1 addition & 1 deletion test/value-select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ test("supports custom model", (t) => {

const actual = value.select({ seed, model: customModel });

t.true(customModel.config.options.includes(actual));
t.true(customModel.config?.options?.includes(actual));
});

0 comments on commit bd7620f

Please sign in to comment.