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 5d2ac2f
Show file tree
Hide file tree
Showing 29 changed files with 4,235 additions and 2,833 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
16 changes: 4 additions & 12 deletions packages/react/spec/auto/PolarisAutoForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("PolarisAutoForm", () => {
describe("when used as a one liner", () => {
describe("for widget create", () => {
test("it renders the name input", async () => {
render(<PolarisAutoForm action={api.widget.create} />, { wrapper: PolarisMockedProviders });
render(<PolarisAutoForm action={api.widget.create} exclude={["section", "gizmos"]} />, { wrapper: PolarisMockedProviders });
loadMockWidgetCreateMetadata();
expect(await screen.findByLabelText("Name")).toBeInTheDocument();
});
Expand Down Expand Up @@ -69,7 +69,7 @@ describe("PolarisAutoForm", () => {
test("it includes the record ID when submitting a form that updates a record", async () => {
const user = userEvent.setup();

const result = render(<PolarisAutoForm action={api.widget.update} exclude={["gizmos"]} findBy="1145" />, {
const result = render(<PolarisAutoForm action={api.widget.update} exclude={["section", "gizmos"]} findBy="1145" />, {
wrapper: PolarisMockedProviders,
});
const { getByLabelText, queryAllByText } = result;
Expand Down Expand Up @@ -266,7 +266,7 @@ describe("PolarisAutoForm", () => {
test("it should include fields that are not dirty when submitting an update form", async () => {
const user = userEvent.setup();

const result = render(<PolarisAutoForm action={api.widget.update} exclude={["gizmos"]} findBy="1145" />, {
const result = render(<PolarisAutoForm action={api.widget.update} exclude={["section", "gizmos"]} findBy="1145" />, {
wrapper: PolarisMockedProviders,
});
const { getByLabelText, queryAllByText } = result;
Expand Down Expand Up @@ -311,9 +311,6 @@ describe("PolarisAutoForm", () => {
name: "Test Widget",
roles: [],
secretKey: null,
section: {
_link: undefined,
},
startsAt: null,
});
expect(recordId).toEqual("1145");
Expand Down Expand Up @@ -619,7 +616,7 @@ describe("PolarisAutoForm", () => {
beforeEach(() => {
user = userEvent.setup();

renderResult = render(<PolarisAutoForm action={api.widget.update} exclude={["gizmos"]} findBy="1145" />, {
renderResult = render(<PolarisAutoForm action={api.widget.update} exclude={["section", "gizmos"]} findBy="1145" />, {
wrapper: PolarisMockedProviders,
});

Expand Down Expand Up @@ -708,7 +705,6 @@ function loadMockGizmoCreateMetadata() {
modelApiIdentifier: "gizmo",
modelNamespace: null,
action: "create",
includeRelatedFields: false,
});

mockUrqlClient.executeQuery.pushResponse("ModelActionMetadata", {
Expand All @@ -727,7 +723,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 +778,6 @@ const mockWidgetUpdateHelperFunctions = {
modelApiIdentifier: "widget",
modelNamespace: null,
action: "update",
includeRelatedFields: false,
});
},
};
Expand All @@ -793,7 +787,6 @@ function loadMockWidgetDeleteMetadata() {
modelApiIdentifier: "widget",
modelNamespace: null,
action: "delete",
includeRelatedFields: false,
});

mockUrqlClient.executeQuery.pushResponse("ModelActionMetadata", {
Expand All @@ -814,7 +807,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 5d2ac2f

Please sign in to comment.