Skip to content

Commit

Permalink
typesafe recursive metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
infiton committed Oct 18, 2024
1 parent fce894e commit c582c62
Show file tree
Hide file tree
Showing 29 changed files with 4,228 additions and 2,826 deletions.
4 changes: 2 additions & 2 deletions packages/react/spec/auto/MockForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { FormProvider, useForm } from "../../src/useActionForm.js";
import { testApi as api } from "../apis.js";
import { MockClientProvider } from "../testWrappers.js";

export const MockForm = ({ submit, metadata, submitResult, resolver }: AutoFormMetadataContext & { resolver?: any }) => {
export const MockForm = ({ submit, metadata, submitResult, resolver }: Partial<AutoFormMetadataContext> & { resolver?: any }) => {
return (props: { children: ReactNode }) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const methods = useForm({ resolver });

return (
<MockClientProvider api={api}>
<FormProvider {...methods}>
<AutoFormMetadataContext.Provider value={{ submit, metadata, submitResult }}>
<AutoFormMetadataContext.Provider value={{ submit: submit!, metadata, submitResult }}>
<AppProvider i18n={translations}>
{/* eslint-disable-next-line @typescript-eslint/no-misused-promises */}
<form onSubmit={methods.handleSubmit(submit as any)}>
Expand Down
5 changes: 0 additions & 5 deletions packages/react/spec/auto/PolarisAutoForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,6 @@ function loadMockGizmoCreateMetadata() {
modelApiIdentifier: "gizmo",
modelNamespace: null,
action: "create",
includeRelatedFields: false,
});

mockUrqlClient.executeQuery.pushResponse("ModelActionMetadata", {
Expand All @@ -727,7 +726,6 @@ function loadMockWidgetCreateMetadata(opts?: { inputFields?: any[]; triggers?: a
modelApiIdentifier: "widget",
modelNamespace: null,
action: "create",
includeRelatedFields: false,
});

mockUrqlClient.executeQuery.pushResponse("ModelActionMetadata", {
Expand Down Expand Up @@ -783,7 +781,6 @@ const mockWidgetUpdateHelperFunctions = {
modelApiIdentifier: "widget",
modelNamespace: null,
action: "update",
includeRelatedFields: false,
});
},
};
Expand All @@ -793,7 +790,6 @@ function loadMockWidgetDeleteMetadata() {
modelApiIdentifier: "widget",
modelNamespace: null,
action: "delete",
includeRelatedFields: false,
});

mockUrqlClient.executeQuery.pushResponse("ModelActionMetadata", {
Expand All @@ -814,7 +810,6 @@ function loadMockFlipAllMetadata(opts?: { triggers?: any[] }) {
expect(mockUrqlClient.executeQuery.mock.calls[0][0].variables).toEqual({
namespace: null,
apiIdentifier: "flipAll",
includeRelatedFields: false,
});

mockUrqlClient.executeQuery.pushResponse("GlobalActionMetadata", {
Expand Down
8 changes: 4 additions & 4 deletions packages/react/spec/auto/hooks/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GadgetRecord } from "@gadgetinc/api-client-core";
import type { FieldMetadataFragment } from "../../../src/internal/gql/graphql.js";
import type { GadgetRecord } from "@gadgetinc/api-client-core";
import { GadgetFieldType } from "../../../src/internal/gql/graphql.js";
import type { FieldMetadata } from "../../../src/metadata.js";
import {
fieldMetadataArrayToFieldMetadataTree,
getTableColumns,
Expand Down Expand Up @@ -511,7 +511,7 @@ const getSimpleFieldMetadata = (name: string, apiIdentifier: string, fieldType:
fieldType,
configuration: gadgetGenericFieldConfig,
sortable: sortable ?? true,
} as FieldMetadataFragment;
} as FieldMetadata;
};

const getRelationshipFieldMetadata = (props: {
Expand Down Expand Up @@ -539,7 +539,7 @@ const getRelationshipFieldMetadata = (props: {
defaultDisplayField,
},
},
} as FieldMetadataFragment;
} as FieldMetadata;
};

const fieldMetadataTreeWithRelationship = fieldMetadataArrayToFieldMetadataTree([
Expand Down
12 changes: 6 additions & 6 deletions packages/react/spec/auto/hooks/useFieldMetadata.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { jest } from "@jest/globals";
import { renderHook } from "@testing-library/react";
import { useFieldMetadata } from "../../../src/auto/hooks/useFieldMetadata.js";
import type { ActionMetadata } from "../../../src/metadata.js";
import { GadgetFieldType } from "../../../src/internal/gql/graphql.js";
import { MockForm } from "../MockForm.js";

describe("useFieldMetadata hook", () => {
const getUseFieldMetadataResult = (fieldApiId: string) => {
const { result } = renderHook(() => useFieldMetadata(fieldApiId), {
wrapper: MockForm({ submit: jest.fn<any>(), metadata }),
wrapper: MockForm({ submit: jest.fn<any>(), metadata: metadata as any }),
});

return result.current;
Expand Down Expand Up @@ -62,7 +62,7 @@ describe("useFieldMetadata hook", () => {
});
});

const metadata: ActionMetadata = {
const metadata = {
name: "Widget",
apiIdentifier: "widget",
action: {
Expand All @@ -72,11 +72,11 @@ const metadata: ActionMetadata = {
{
name: "Widget",
apiIdentifier: "widget",
fieldType: "Object",
fieldType: GadgetFieldType.Object,
requiredArgumentForInput: false,
configuration: {
__typename: "GadgetObjectFieldConfig",
fieldType: "Object",
fieldType: GadgetFieldType.Object,
name: null,
fields: [
{
Expand Down Expand Up @@ -115,4 +115,4 @@ const metadata: ActionMetadata = {
__typename: "GadgetAction",
},
__typename: "GadgetModel",
} as ActionMetadata;
};
6 changes: 3 additions & 3 deletions packages/react/spec/auto/hooks/useFormFields.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { jest } from "@jest/globals";
import { renderHook } from "@testing-library/react";
import { useFormFields } from "../../../src/auto/AutoForm.js";
import { useAutoFormMetadata } from "../../../src/auto/AutoFormContext.js";
import type { ActionMetadata } from "../../../src/metadata.js";
import type { ModelWithOneActionMetadata } from "../../../src/metadata.js";
import { MockForm } from "../MockForm.js";

const shopifyShopModelKey = "DataModel-Shopify-Shop";
Expand Down Expand Up @@ -96,7 +96,7 @@ describe("useFormFields hook", () => {
});
});

const metadata: ActionMetadata = {
const metadata: ModelWithOneActionMetadata = {
name: "Widget",
apiIdentifier: "widget",
action: {
Expand Down Expand Up @@ -239,7 +239,7 @@ const metadata: ActionMetadata = {
__typename: "GadgetAction",
},
__typename: "GadgetModel",
} as ActionMetadata;
} as ModelWithOneActionMetadata;

const autoFormMetadataWithDuplicateInputFields = {
name: "Auto table test",
Expand Down
Loading

0 comments on commit c582c62

Please sign in to comment.