Skip to content

Commit

Permalink
Update auto component error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
alanko0511 committed Sep 16, 2024
1 parent 8fe1cf5 commit 0e00432
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 12 deletions.
10 changes: 5 additions & 5 deletions packages/react/spec/auto/PolarisAutoForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { UserEvent } from "@testing-library/user-event";
import { userEvent } from "@testing-library/user-event";
import type { ReactNode } from "react";
import React from "react";
import { TriggerableActionRequiredErrorMessage } from "../../src/auto/AutoFormActionValidators.js";
import { MissingApiTriggerErrorMessage } from "../../src/auto/AutoFormActionValidators.js";
import { PolarisAutoForm } from "../../src/auto/polaris/PolarisAutoForm.js";
import { PolarisAutoInput } from "../../src/auto/polaris/inputs/PolarisAutoInput.js";
import { PolarisAutoSubmit } from "../../src/auto/polaris/submit/PolarisAutoSubmit.js";
Expand Down Expand Up @@ -676,28 +676,28 @@ describe("PolarisAutoForm", () => {
it("throws an error when a model action without triggers", () => {
expect(() => {
render(<PolarisAutoForm action={api.autoTableTest.noTriggerAction as any} />, { wrapper: PolarisMockedProviders });
}).toThrow(TriggerableActionRequiredErrorMessage);
}).toThrow(MissingApiTriggerErrorMessage);
});

it("throws an error when a global action without triggers", () => {
expect(() => {
render(<PolarisAutoForm action={api.noTriggerGlobalAction as any} />, { wrapper: PolarisMockedProviders });
}).toThrow(TriggerableActionRequiredErrorMessage);
}).toThrow(MissingApiTriggerErrorMessage);
});
});
describe("Has triggers in api client but no triggers in action metadata", () => {
it("throws an error when a model action without triggers", () => {
expect(() => {
render(<PolarisAutoForm action={api.widget.create as any} />, { wrapper: PolarisMockedProviders });
loadMockWidgetCreateMetadata({ triggers: [{ specID: "non/api/trigger", __typename: "GadgetTrigger" }] });
}).toThrow(TriggerableActionRequiredErrorMessage);
}).toThrow(MissingApiTriggerErrorMessage);
});

it("throws an error when a global action without triggers", () => {
expect(() => {
render(<PolarisAutoForm action={api.flipAll as any} />, { wrapper: PolarisMockedProviders });
loadMockFlipAllMetadata({ triggers: [{ specID: "non/api/trigger", __typename: "GadgetTrigger" }] });
}).toThrow(TriggerableActionRequiredErrorMessage);
}).toThrow(MissingApiTriggerErrorMessage);
});
});
});
Expand Down
10 changes: 10 additions & 0 deletions packages/react/spec/auto/form/PolarisAutoFormErrors.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ export default {
],
};

export const InvalidModelAction = {
args: {
action: api.autoTableTest.invalidAction,
},
};

export const MissingActionProp = {
args: {},
};

export const FieldNameCustomParamCollisionError = {
args: {
findBy: "1",
Expand Down
11 changes: 11 additions & 0 deletions packages/react/spec/auto/table/PolarisAutoTable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { act, render } from "@testing-library/react";
import type { UserEvent } from "@testing-library/user-event";
import { userEvent } from "@testing-library/user-event";
import React from "react";
import { InvalidModelErrorMessage } from "../../../src/auto/AutoTableValidators.js";
import { GadgetFieldType } from "../../../src/internal/gql/graphql.js";
import type { TableColumn } from "../../../src/use-table/types.js";
import { testApi as api } from "../../apis.js";
Expand Down Expand Up @@ -451,4 +452,14 @@ describe("PolarisAutoTable", () => {
expect(firstRowCells[1].innerHTML).not.toContain("...");
});
});

describe("invalid model", () => {
it("throws an error when the model is not valid", () => {
expect(() => {
render(<PolarisAutoTable model={(api as any).invalidModel} />, {
wrapper: PolarisMockedProviders,
});
}).toThrow(InvalidModelErrorMessage);
});
});
});
10 changes: 7 additions & 3 deletions packages/react/src/auto/AutoFormActionValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ export const validateNonBulkAction = (action: ActionFunction<any, any, any, any,
};

const GadgetApiTriggerSpecId = "gadget/trigger/graphql_api";
export const TriggerableActionRequiredErrorMessage = `"action" must be a valid Gadget action with an API trigger to be used in AutoForms`;

export const MissingActionPropErrorMessage = "Specify a valid Gagdet action to use AutoForm";
export const InvalidActionErrorMessage = `"action" is not a valid Gadget action`;
export const MissingApiTriggerErrorMessage = `"action" requires an API trigger to be used in AutoForm`;

const validActionTypes = ["globalAction", "action"];

export const validateTriggersFromApiClient = (action: ActionFunction<any, any, any, any, any> | GlobalActionFunction<any>) => {
if (!validActionTypes.includes(action.type)) {
// When the API client is built with an action without the API trigger, the type will be "stubbedAction"
// action.type === "globalAction" | "action" // Only when the action has the API trigger when the api client is built
throw new Error(TriggerableActionRequiredErrorMessage);
throw new Error(MissingApiTriggerErrorMessage);
}
};

Expand All @@ -32,7 +36,7 @@ export const validateTriggersFromMetadata = (metadata?: ActionMetadata | GlobalA

const hasApiTrigger = triggersAsArray.some((trigger) => trigger.specID === GadgetApiTriggerSpecId);
if (!hasApiTrigger) {
throw new Error(TriggerableActionRequiredErrorMessage);
throw new Error(MissingApiTriggerErrorMessage);
}
}
};
1 change: 1 addition & 0 deletions packages/react/src/auto/AutoTableValidators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const InvalidModelErrorMessage = `"model" is not a valid Gadget model`;
8 changes: 6 additions & 2 deletions packages/react/src/auto/mui/MUIAutoForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React from "react";
import { FormProvider } from "react-hook-form";
import { humanizeCamelCase, type OptionsType } from "../../utils.js";
import { useAutoForm, type AutoFormProps } from "../AutoForm.js";
import { TriggerableActionRequiredErrorMessage } from "../AutoFormActionValidators.js";
import { InvalidActionErrorMessage, MissingActionPropErrorMessage } from "../AutoFormActionValidators.js";
import { AutoFormMetadataContext } from "../AutoFormContext.js";
import { MUIAutoInput } from "./inputs/MUIAutoInput.js";
import { MUIAutoSubmit } from "./submit/MUIAutoSubmit.js";
Expand Down Expand Up @@ -36,8 +36,12 @@ export const MUIAutoForm = <
) => {
const { action, findBy } = props as MUIAutoFormProps<GivenOptions, SchemaT, ActionFunc> & { findBy: any };

if (!("action" in props)) {
throw new Error(MissingActionPropErrorMessage);
}

if (!action) {
throw new Error(TriggerableActionRequiredErrorMessage);
throw new Error(InvalidActionErrorMessage);
}

// Component key to force re-render when the action or findBy changes
Expand Down
8 changes: 6 additions & 2 deletions packages/react/src/auto/polaris/PolarisAutoForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FormProvider } from "react-hook-form";
import { humanizeCamelCase, type OptionsType } from "../../utils.js";
import type { AutoFormProps } from "../AutoForm.js";
import { useAutoForm } from "../AutoForm.js";
import { TriggerableActionRequiredErrorMessage } from "../AutoFormActionValidators.js";
import { InvalidActionErrorMessage, MissingActionPropErrorMessage } from "../AutoFormActionValidators.js";
import { AutoFormMetadataContext } from "../AutoFormContext.js";
import { PolarisAutoInput } from "./inputs/PolarisAutoInput.js";
import { PolarisAutoSubmit } from "./submit/PolarisAutoSubmit.js";
Expand All @@ -33,8 +33,12 @@ export const PolarisAutoForm = <
const { action, findBy } = props as AutoFormProps<GivenOptions, SchemaT, ActionFunc> &
Omit<Partial<FormProps>, "action"> & { findBy: any };

if (!("action" in props)) {
throw new Error(MissingActionPropErrorMessage);
}

if (!action) {
throw new Error(TriggerableActionRequiredErrorMessage);
throw new Error(InvalidActionErrorMessage);
}

// Component key to force re-render when the action or findBy changes
Expand Down
6 changes: 6 additions & 0 deletions packages/react/src/auto/polaris/PolarisAutoTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useTable } from "../../useTable.js";
import type { ColumnValueType, OptionsType } from "../../utils.js";
import type { AutoTableProps } from "../AutoTable.js";
import { AutoTableContext } from "../AutoTableContext.js";
import { InvalidModelErrorMessage } from "../AutoTableValidators.js";
import type { BulkActionOption } from "../hooks/useTableBulkActions.js";
import { useTableBulkActions } from "../hooks/useTableBulkActions.js";
import { PolarisAutoBulkActionModal } from "./PolarisAutoBulkActionModal.js";
Expand Down Expand Up @@ -65,6 +66,11 @@ export const PolarisAutoTable = <
props: AutoTableProps<GivenOptions, SchemaT, FinderFunction, Options>
) => {
const { model } = props;

if (!model) {
throw new Error(InvalidModelErrorMessage);
}

const componentKey = `${[model.findMany.namespace, model.findMany.modelApiIdentifier].join("_")}AutoTable`;

return <PolarisAutoTableComponent key={componentKey} {...props} />;
Expand Down

0 comments on commit 0e00432

Please sign in to comment.