Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/components/form/form.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import { describe, expect, it, vi, beforeEach } from "vitest";
import { describe, expect, it, vi, beforeEach, type Mock } from "vitest";
import { BrowserRouter } from "react-router-dom";
import { Form } from "./form";
import { ResourceSchema, PropertySchema } from "@/state/openapi";
import { Schema } from "@aep_dev/aep-lib-ts";
import { ResourceInstance } from "@/state/fetch";
import fs from "fs";
import { parseOpenAPI } from "@/state/openapi";
import "@testing-library/jest-dom/vitest";

// Mock ResourceSchema for testing different property types
const createMockResourceSchema = (
Expand Down Expand Up @@ -1206,4 +1208,49 @@ describe("Form", () => {
});
});
});

describe("ReadOnly properties", () => {
it("does not render readOnly properties in the form", () => {
const properties = [
new PropertySchema("id", "string", {
readOnly: true,
} as unknown as Schema),
new PropertySchema("name", "string"),
];
const resource = createMockResourceSchema(properties);
renderForm(resource);

expect(screen.queryByLabelText("id")).not.toBeInTheDocument();
expect(screen.getByLabelText("name")).toBeInTheDocument();
});

it("excludes readOnly properties from the validation schema and submission", async () => {
const properties = [
new PropertySchema("id", "string", {
readOnly: true,
} as unknown as Schema),
new PropertySchema("name", "string"),
];
const resource = createMockResourceSchema(properties);
renderForm(resource);

fireEvent.change(screen.getByLabelText("name"), {
target: { value: "Test Name" },
});
fireEvent.click(screen.getByRole("button", { name: "Submit" }));

await waitFor(() => {
expect(resource.create).toHaveBeenCalledWith(
{
name: "Test Name",
},
"",
);
});

// Ensure id was NOT included
const call = (resource.create as Mock).mock.calls[0];
expect(call[0]).not.toHaveProperty("id");
});
});
});
6 changes: 5 additions & 1 deletion src/components/form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type FormProps = {
onError: (error: unknown) => void;
// Current resource state used to fill in the form's default values for updating (optional)
resourceInstance?: ResourceInstance;
onSubmitOperation: (value: Record<string, unknown>) => Promise<void>;
onSubmitOperation: (value: Record<string, unknown>) => Promise<unknown>;
};

// Form is responsible for rendering a form based on the resource schema.
Expand Down Expand Up @@ -142,6 +142,10 @@ export function Form(props: FormProps) {
return <Spinner key="loading" />;
}

if (p.readOnly) {
return null;
}

const fieldPath = parentPath ? `${parentPath}.${p.name}` : p.name;

if (p.type === "object") {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function createValidationSchema(
const schemaObject: Record<string, z.ZodTypeAny> = {};

for (const property of properties) {
if (!property) continue; // Skip null properties
if (!property || property.readOnly) continue; // Skip null or readOnly properties
let fieldSchema: z.ZodTypeAny;
const isRequired = requiredFields.includes(property.name);

Expand Down Expand Up @@ -105,6 +105,7 @@ export function createValidationSchemaFromRawSchema(
string,
Record<string, unknown>,
][]) {
if (propSchema.readOnly) continue; // Skip readOnly properties
const isFieldRequired = required.includes(name);
let fieldSchema: z.ZodTypeAny;

Expand Down
10 changes: 7 additions & 3 deletions src/state/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ class ResourceSchema {
return Get(url, this, headers);
}

create(body: object, headers: string = ""): Promise {
create(body: Record<string, unknown>, headers: string = ""): Promise<void> {
const baseUrl = this.base_url();
let url = `${this.server_url}${baseUrl}`;
if (this.properties().find((prop) => prop.name === "id")) {
url += `?id=${body.id}`;
url += `?id=${(body as Record<string, unknown>).id}`;
}
url = this.substituteUrlParameters(url);
return Create(url, body, headers);
Expand Down Expand Up @@ -124,7 +124,7 @@ class PropertySchema {
type: string;
schema: Schema;

constructor(name: string, type: string, schema: Schema) {
constructor(name: string, type: string, schema: Schema = {}) {
this.name = name;
this.type = type;
this.schema = schema;
Expand All @@ -149,6 +149,10 @@ class PropertySchema {
}
return [];
}

get readOnly(): boolean {
return !!(this.schema as Record<string, unknown>)?.readOnly;
}
}

// Adapter class that wraps aep-lib-ts APIClient with UI-specific functionality
Expand Down