Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

add tests on ContactEdit

ThieryMichelb5be4737e731
diff --git a/registry.json b/registry.json
index 7b98a23d..806a7a49 100644
--- a/registry.json
+++ b/registry.json
@@ -653,6 +653,10 @@
"path": "src/components/atomic-crm/contacts/contactGender.ts",
"type": "registry:component"
},
+ {
+ "path": "src/components/atomic-crm/contacts/cleanupContact.ts",
+ "type": "registry:component"
+ },
{
"path": "src/components/atomic-crm/contacts/TagsListEdit.tsx",
"type": "registry:component"
diff --git a/src/components/atomic-crm/contacts/ContactEdit.integration.test.tsx b/src/components/atomic-crm/contacts/ContactEdit.integration.test.tsx
new file mode 100644
index 00000000..c7671bce
--- /dev/null
+++ b/src/components/atomic-crm/contacts/ContactEdit.integration.test.tsx
@@ -0,0 +1,187 @@
+import { render } from "vitest-browser-react";
+
+import {
+ buildContact,
+ ContactEditHarness,
+ createCrmDb,
+ createCrmScenario,
+ CrmTestProvider,
+} from "@/test/browser/atomic-crm/crmUiHarness";
+
+/**
+ * Create a scenario whose dataProvider.update is wrapped in a vi.fn(),
+ * so we can assert on what data the transform sends to the backend.
+ */
+const createScenarioWithUpdateMock = (
+ ...args: Parameters<typeof createCrmScenario>
+) => {
+ const scenario = createCrmScenario(...args);
+ const originalUpdate = scenario.dataProvider.update;
+ const updateMock = vi.fn(
+ async (...updateArgs: Parameters<typeof originalUpdate>) =>
+ originalUpdate(...updateArgs),
+ );
+ // Build a new dataProvider object so CoreAdminContext sees the mock
+ scenario.dataProvider = { ...scenario.dataProvider, update: updateMock };
+ return { scenario, updateMock };
+};
+
+describe("ContactEdit integration", () => {
+ it("shows empty email and phone inputs when the contact has none", async () => {
+ const scenario = createCrmScenario({
+ db: createCrmDb({
+ contacts: [
+ buildContact({
+ id: 1,
+ email_jsonb: [],
+ phone_jsonb: [],
+ }),
+ ] as any,
+ }),
+ });
+
+ const screen = await render(
+ <CrmTestProvider
+ scenario={scenario}
+ resource="contacts"
+ initialEntries={["/contacts/1"]}
+ >
+ <ContactEditHarness />
+ </CrmTestProvider>,
+ );
+
+ // The form should display one empty email placeholder input
+ await expect.element(screen.getByPlaceholder("Email")).toBeInTheDocument();
+
+ // The form should display one empty phone placeholder input
+ await expect
+ .element(screen.getByPlaceholder("Phone number"))
+ .toBeInTheDocument();
+ });
+
+ it("does not submit empty email and phone entries", async () => {
+ const { scenario, updateMock } = createScenarioWithUpdateMock({
+ db: createCrmDb({
+ contacts: [
+ buildContact({
+ id: 1,
+ email_jsonb: [],
+ phone_jsonb: [],
+ }),
+ ] as any,
+ }),
+ });
+
+ const screen = await render(
+ <CrmTestProvider
+ scenario={scenario}
+ resource="contacts"
+ initialEntries={["/contacts/1"]}
+ >
+ <ContactEditHarness />
+ </CrmTestProvider>,
+ );
+
+ // Wait for the form to load
+ await expect.element(screen.getByPlaceholder("Email")).toBeInTheDocument();
+
+ // Submit without filling anything
+ await screen.getByRole("button", { name: /^save$/i }).click();
+
+ // Wait for the update call to complete
+ await expect.poll(() => updateMock.mock.calls.length).toBe(1);
+
+ // Verify the transform cleaned up the empty arrays
+ const updateData = updateMock.mock.calls[0][1].data;
+ expect(updateData.email_jsonb).toBeNull();
+ expect(updateData.phone_jsonb).toBeNull();
+ });
+
+ it("submits only filled email and phone entries, stripping empty ones", async () => {
+ const { scenario, updateMock } = createScenarioWithUpdateMock({
+ db: createCrmDb({
+ contacts: [
+ buildContact({
+ id: 1,
+ email_jsonb: [],
+ phone_jsonb: [],
+ }),
+ ] as any,
+ }),
+ });
+
+ const screen = await render(
+ <CrmTestProvider
+ scenario={scenario}
+ resource="contacts"
+ initialEntries={["/contacts/1"]}
+ >
+ <ContactEditHarness />
+ </CrmTestProvider>,
+ );
+
+ // Wait for the form to load and fill in the email
+ const emailInput = screen.getByPlaceholder("Email");
+ await expect.element(emailInput).toBeInTheDocument();
+ await emailInput.fill("[email protected]");
+
+ // Leave the phone input empty and submit
+ await screen.getByRole("button", { name: /^save$/i }).click();
+
+ // Wait for the update call to complete
+ await expect.poll(() => updateMock.mock.calls.length).toBe(1);
+
+ // Verify: email saved, phone cleaned to null
+ const updateData = updateMock.mock.calls[0][1].data;
+ expect(updateData.email_jsonb).toEqual([
+ { email: "[email protected]", type: "Work" },
+ ]);
+ expect(updateData.phone_jsonb).toBeNull();
+ });
+
+ it("preserves existing email and phone entries on edit", async () => {
+ const { scenario, updateMock } = createScenarioWithUpdateMock({
+ db: createCrmDb({
+ contacts: [
+ buildContact({
+ id: 1,
+ email_jsonb: [{ email: "[email protected]", type: "Work" }],
+ phone_jsonb: [{ number: "+1234567890", type: "Home" }],
+ }),
+ ] as any,
+ }),
+ });
+
+ const screen = await render(
+ <CrmTestProvider
+ scenario={scenario}
+ resource="contacts"
+ initialEntries={["/contacts/1"]}
+ >
+ <ContactEditHarness />
+ </CrmTestProvider>,
+ );
+
+ // Wait for existing values to appear
+ const emailInput = screen.getByPlaceholder("Email");
+ await expect.element(emailInput).toHaveValue("[email protected]");
+
+ const phoneInput = screen.getByPlaceholder("Phone number");
+ await expect.element(phoneInput).toHaveValue("+1234567890");
+
+ // Submit without changes
+ await screen.getByRole("button", { name: /^save$/i }).click();
+
+ // Wait for the update call to complete
+ await expect.poll(() => updateMock.mock.calls.length).toBe(1);
+
+ // Verify existing data is preserved through the transform
+ const updateData = updateMock.mock.calls[0][1].data;
+ expect(updateData.email_jsonb).toEqual([
+ { email: "[email protected]", type: "Work" },
+ ]);
+ expect(updateData.phone_jsonb).toEqual([
+ { number: "+1234567890", type: "Home" },
+ ]);
+ });
+});
diff --git a/src/components/atomic-crm/contacts/ContactEdit.tsx b/src/components/atomic-crm/contacts/ContactEdit.tsx
index 7948a913..97a943c8 100644
--- a/src/components/atomic-crm/contacts/ContactEdit.tsx
+++ b/src/components/atomic-crm/contacts/ContactEdit.tsx
@@ -1,5 +1,5 @@
import { Card, CardContent } from "@/components/ui/card";
-import { EditBase, Form, useEditContext } from "ra-core";
+import { EditBase, Form, useEditContext, type MutationMode } from "ra-core";
import type { Contact } from "../types";
import { ContactAside } from "./ContactAside";
@@ -11,8 +11,16 @@ import {
defaultPhoneJsonb,
} from "./cleanupContact";
-export const ContactEdit = () => (
- <EditBase redirect="show" transform={cleanupContactForEdit}>
+export const ContactEdit = ({
+ mutationMode,
+}: {
+ mutationMode?: MutationMode;
+}) => (
+ <EditBase
+ redirect="show"
+ transform={cleanupContactForEdit}
+ mutationMode={mutationMode}
+ >
<ContactEditContent />
</EditBase>
);