A full-featured CRM built with React, shadcn/ui, and Supabase.
update test on ContactEdit
ThieryMichel225033dee6d1
diff --git a/registry.json b/registry.jsonindex 46cadd18..1825eed7 100644--- a/registry.json+++ b/registry.json@@ -709,10 +709,6 @@"path": "src/components/atomic-crm/contacts/ContactEdit.tsx","type": "registry:component"},- {- "path": "src/components/atomic-crm/contacts/ContactEdit.stories.tsx",- "type": "registry:component"- },{"path": "src/components/atomic-crm/contacts/ContactCreateSheet.tsx","type": "registry:component"@@ -721,10 +717,6 @@"path": "src/components/atomic-crm/contacts/ContactCreate.tsx","type": "registry:component"},- {- "path": "src/components/atomic-crm/contacts/ContactCreate.stories.tsx",- "type": "registry:component"- },{"path": "src/components/atomic-crm/contacts/ContactBackgroundInfo.tsx","type": "registry:component"diff --git a/src/components/atomic-crm/contacts/ContactEdit.mobile.stories.tsx b/src/components/atomic-crm/contacts/ContactEdit.mobile.stories.tsxnew file mode 100644index 00000000..5ca46687--- /dev/null+++ b/src/components/atomic-crm/contacts/ContactEdit.mobile.stories.tsx@@ -0,0 +1,107 @@+import type { Meta } from "@storybook/react-vite";++import { ContactEdit } from "./ContactEdit";+import { Route, Routes } from "react-router";+import { buildContact, StoryWrapper } from "@/test/StoryWrapper";+import type { DataProvider } from "ra-core";++const meta = {+ title: "Atomic CRM/Contacts/Contact Edit Mobile",+ parameters: {+ layout: "fullscreen",+ },+ globals: {+ viewport: { value: "mobile1", isRotated: false },+ },+} satisfies Meta;++export default meta;++export const ContactEditBasic = ({+ dataProvider = {},+ silent,+}: {+ dataProvider?: Partial<DataProvider>;+ silent?: boolean;+}) => (+ <StoryWrapper+ initialEntries={["/contacts/1/show"]}+ data={{+ contacts: [+ buildContact({+ id: 1,+ email_jsonb: [],+ phone_jsonb: [],+ }),+ ],+ }}+ dataProvider={dataProvider}+ silent={silent}+ >+ <Routes>+ <Route path="/contacts/:id" element={<ContactEdit />} />+ </Routes>+ </StoryWrapper>+);++export const ContactEditWithEmailsAndPhones = ({+ dataProvider = {},+ silent,+}: {+ dataProvider?: Partial<DataProvider>;+ silent?: boolean;+}) => (+ <StoryWrapper+ initialEntries={["/contacts/1/show"]}+ data={{+ contacts: [+ buildContact({+ id: 1,+ email_jsonb: [{ email: "[email protected]", type: "Work" }],+ phone_jsonb: [{ number: "0123456789", type: "Work" }],+ }),+ ],+ }}+ dataProvider={dataProvider}+ silent={silent}+ >+ <Routes>+ <Route path="/contacts/:id" element={<ContactEdit />} />+ </Routes>+ </StoryWrapper>+);++export const ContactEditWithError = () => (+ <StoryWrapper+ initialEntries={["/contacts/1/show"]}+ data={{+ contacts: [+ buildContact({+ id: 1,+ email_jsonb: [],+ phone_jsonb: [],+ }),+ ] as any,+ }}+ dataProvider={{+ update: async (resource, params) => {+ if (resource === "contacts") {+ throw new Error("Failed to update contact");+ }+ return { data: params.data as any };+ },+ }}+ >+ <Routes>+ <Route path="/contacts/:id" element={<ContactEdit />} />+ </Routes>+ </StoryWrapper>+);++export const ContactEditNotFound = () => (+ <StoryWrapper initialEntries={["/contacts/1/show"]}>+ <Routes>+ <Route path="/contacts/:id" element={<ContactEdit />} />+ </Routes>+ </StoryWrapper>+);diff --git a/src/components/atomic-crm/contacts/ContactEdit.stories.tsx b/src/components/atomic-crm/contacts/ContactEdit.stories.tsxindex 4344594b..8a86712a 100644--- a/src/components/atomic-crm/contacts/ContactEdit.stories.tsx+++ b/src/components/atomic-crm/contacts/ContactEdit.stories.tsx@@ -1,96 +1,107 @@-import type { Meta, StoryObj } from "@storybook/react-vite";+import type { Meta } from "@storybook/react-vite";-import {- buildContact,- createCrmDb,- CrmStoryProvider,-} from "@/test/browser/atomic-crm/crmUiHarness";import { ContactEdit } from "./ContactEdit";import { Route, Routes } from "react-router";+import { buildContact, StoryWrapper } from "@/test/StoryWrapper";+import type { DataProvider } from "ra-core";const meta = {title: "Atomic CRM/Contacts/Contact Edit",parameters: {layout: "fullscreen",},+ globals: {+ viewport: { value: "responsive", isRotated: false },+ },} satisfies Meta;export default meta;-type Story = StoryObj<typeof meta>;+export const ContactEditBasic = ({+ dataProvider = {},+ silent,+}: {+ dataProvider?: Partial<DataProvider>;+ silent?: boolean;+}) => (+ <StoryWrapper+ initialEntries={["/contacts/1"]}+ data={{+ contacts: [+ buildContact({+ id: 1,+ email_jsonb: [],+ phone_jsonb: [],+ }),+ ],+ }}+ dataProvider={dataProvider}+ silent={silent}+ >+ <Routes>+ <Route path="/contacts/:id" element={<ContactEdit />} />+ </Routes>+ </StoryWrapper>+);-export const ConcatEditBasic: Story = {- render: () => (- <CrmStoryProvider- resource="contacts"- initialEntries={["/contacts/1"]}- scenarioOptions={{- db: createCrmDb({- contacts: [- buildContact({- id: 1,- email_jsonb: [],- phone_jsonb: [],- }),- ] as any,+export const ContactEditWithEmailsAndPhones = ({+ dataProvider = {},+ silent,+}: {+ dataProvider?: Partial<DataProvider>;+ silent?: boolean;+}) => (+ <StoryWrapper+ initialEntries={["/contacts/1"]}+ data={{+ contacts: [+ buildContact({+ id: 1,+ email_jsonb: [{ email: "[email protected]", type: "Work" }],+ phone_jsonb: [{ number: "0123456789", type: "Work" }],}),- silent: false,- }}- >- <Routes>- <Route- path="/contacts/:id"- element={<ContactEdit mutationMode="pessimistic" />}- />- </Routes>- </CrmStoryProvider>- ),-};-export const ConcatEditWithError: Story = {- render: () => (- <CrmStoryProvider- resource="contacts"- initialEntries={["/contacts/1"]}- scenarioOptions={{- db: createCrmDb({- contacts: [- buildContact({- id: 1,- email_jsonb: [],- phone_jsonb: [],- }),- ] as any,+ ],+ }}+ dataProvider={dataProvider}+ silent={silent}+ >+ <Routes>+ <Route path="/contacts/:id" element={<ContactEdit />} />+ </Routes>+ </StoryWrapper>+);++export const ContactEditWithError = () => (+ <StoryWrapper+ initialEntries={["/contacts/1"]}+ data={{+ contacts: [+ buildContact({+ id: 1,+ email_jsonb: [],+ phone_jsonb: [],}),- updateError: "Simulated update error",- silent: false,- }}- >- <Routes>- <Route- path="/contacts/:id"- element={<ContactEdit mutationMode="pessimistic" />}- />- </Routes>- </CrmStoryProvider>- ),-};+ ] as any,+ }}+ dataProvider={{+ update: async (resource, params) => {+ if (resource === "contacts") {+ throw new Error("Failed to update contact");+ }+ return { data: params.data as any };+ },+ }}+ >+ <Routes>+ <Route path="/contacts/:id" element={<ContactEdit />} />+ </Routes>+ </StoryWrapper>+);-export const ConcatEditNotFound: Story = {- render: () => (- <CrmStoryProvider- resource="contacts"- initialEntries={["/contacts/1"]}- scenarioOptions={{- db: createCrmDb(),- silent: false,- }}- >- <Routes>- <Route- path="/contacts/:id"- element={<ContactEdit mutationMode="pessimistic" />}- />- </Routes>- </CrmStoryProvider>- ),-};+export const ContactEditNotFound = () => (+ <StoryWrapper initialEntries={["/contacts/1"]}>+ <Routes>+ <Route path="/contacts/:id" element={<ContactEdit />} />+ </Routes>+ </StoryWrapper>+);diff --git a/src/components/atomic-crm/contacts/ContactEdit.test.tsx b/src/components/atomic-crm/contacts/ContactEdit.test.tsxindex 44ec5ddc..d458c5ba 100644--- a/src/components/atomic-crm/contacts/ContactEdit.test.tsx+++ b/src/components/atomic-crm/contacts/ContactEdit.test.tsx@@ -1,199 +1,289 @@import { render } from "vitest-browser-react";-import {- buildContact,- createCrmDb,- createCrmScenario,- CrmTestProvider,-} from "@/test/browser/atomic-crm/crmUiHarness";-import { Route, Routes } from "react-router";-import { ContactEdit } from "./ContactEdit";--// EditBase reads the record id from the route params, so we mount a Route.-// We use pessimistic mode to avoid the 5s undoable delay in tests.-export const ContactEditHarness = () => (- <Routes>- <Route- path="/contacts/:id"- element={<ContactEdit mutationMode="pessimistic" />}- />- </Routes>-);--/**- * 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: [],+ ContactEditBasic,+ ContactEditWithEmailsAndPhones,+} from "./ContactEdit.stories";+import {+ ContactEditBasic as ContactEditMobileBasic,+ ContactEditWithEmailsAndPhones as ContactEditMobileWithEmailsAndPhones,+} from "./ContactEdit.mobile.stories";+import { page } from "vitest/browser";++describe("ContactEdit", () => {+ describe("desktop", () => {+ beforeAll(() => {+ page.viewport(1600, 900);+ });++ it("shows empty email and phone inputs when the contact has none", async () => {+ const screen = await render(<ContactEditBasic silent />);++ // 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 updateMock = vi.fn().mockResolvedValue({ data: {} });+ const screen = await render(+ <ContactEditBasic+ silent+ dataProvider={{+ update: updateMock,+ }}+ />,+ );++ // 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();+ await expect+ .poll(() => screen.getByText("Element updated"))+ .toBeInTheDocument();++ await screen.getByLabelText("Close toast").click();++ // Verify the transform cleaned up the empty arrays+ expect(updateMock).toBeCalledTimes(1);+ expect(updateMock).toBeCalledWith(+ expect.anything(),+ expect.objectContaining({+ data: expect.objectContaining({+ email_jsonb: null,+ phone_jsonb: null,}),- ] 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("submits only filled email and phone entries, stripping empty ones", async () => {+ const updateMock = vi.fn().mockResolvedValue({ data: {} });- it("does not submit empty email and phone entries", async () => {- const { scenario, updateMock } = createScenarioWithUpdateMock({- db: createCrmDb({- contacts: [- buildContact({- id: 1,- email_jsonb: [],- phone_jsonb: [],+ const screen = await render(+ <ContactEditBasic+ dataProvider={{+ update: updateMock,+ }}+ silent+ />,+ );++ // 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();+ await expect+ .poll(() => screen.getByText("Element updated"))+ .toBeInTheDocument();++ await screen.getByLabelText("Close toast").click();++ // Wait for the update call to complete+ await expect.poll(() => updateMock.mock.calls.length).toBe(1);++ expect(updateMock).toBeCalledTimes(1);++ expect(updateMock).toBeCalledWith(+ expect.anything(),+ expect.objectContaining({+ data: expect.objectContaining({+ email_jsonb: [{ email: "[email protected]", type: "Work" }],+ phone_jsonb: null,}),- ] 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("preserves existing email and phone entries on edit", async () => {+ const updateMock = vi.fn().mockResolvedValue({ data: {} });+ const screen = await render(+ <ContactEditWithEmailsAndPhones+ silent+ dataProvider={{ update: updateMock }}+ />,+ );++ // 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("0123456789");++ // Submit without changes+ await screen.getByRole("button", { name: /^save$/i }).click();+ await expect+ .poll(() => screen.getByText("Element updated"))+ .toBeInTheDocument();++ await screen.getByLabelText("Close toast").click();++ // Wait for the update call to complete+ expect(updateMock).toBeCalledTimes(1);++ // Verify existing data is preserved through the transform+ expect(updateMock).toBeCalledWith(+ expect.anything(),+ expect.objectContaining({+ data: expect.objectContaining({+ email_jsonb: [{ email: "[email protected]", type: "Work" }],+ phone_jsonb: [{ number: "0123456789", type: "Work" }],+ }),+ }),+ );+ });});+ describe("mobile", () => {+ beforeAll(() => {+ page.viewport(375, 667);+ });++ it("shows empty email and phone inputs when the contact has none on mobile", async () => {+ const screen = await render(<ContactEditMobileBasic silent />);++ await screen.getByRole("button", { name: /edit/i }).click();+ // 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("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: [],+ it("does not submit empty email and phone entries on mobile", async () => {+ const updateMock = vi.fn().mockResolvedValue({ data: {} });+ const screen = await render(+ <ContactEditMobileBasic+ silent+ dataProvider={{+ update: updateMock,+ }}+ />,+ );+ await screen.getByRole("button", { name: /edit/i }).click();++ // 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();+ await expect+ .poll(() => screen.getByText("Element updated"))+ .toBeInTheDocument();++ await screen.getByLabelText("Close toast").click();++ // Verify the transform cleaned up the empty arrays+ expect(updateMock).toBeCalledTimes(1);+ expect(updateMock).toBeCalledWith(+ expect.anything(),+ expect.objectContaining({+ data: expect.objectContaining({+ email_jsonb: null,+ phone_jsonb: null,}),- ] 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("submits only filled email and phone entries, stripping empty ones on mobile", async () => {+ const updateMock = vi.fn().mockResolvedValue({ data: {} });++ const screen = await render(+ <ContactEditMobileBasic+ dataProvider={{+ update: updateMock,+ }}+ silent+ />,+ );+ await screen.getByRole("button", { name: /edit/i }).click();++ // Wait for the edit sheet form to render before interacting+ const emailInput = screen.getByPlaceholder("Email");+ await expect.element(emailInput).toBeInTheDocument();+ await emailInput.fill("[email protected]");- it("preserves existing email and phone entries on edit", async () => {- const { scenario, updateMock } = createScenarioWithUpdateMock({- db: createCrmDb({- contacts: [- buildContact({- id: 1,+ // Leave the phone input empty and submit+ await screen.getByRole("button", { name: /^save$/i }).click();+ await expect+ .poll(() => screen.getByText("Element updated"))+ .toBeInTheDocument();++ await screen.getByLabelText("Close toast").click();++ // Wait for the update call to complete+ await expect.poll(() => updateMock.mock.calls.length).toBe(1);++ expect(updateMock).toBeCalledTimes(1);++ expect(updateMock).toBeCalledWith(+ expect.anything(),+ expect.objectContaining({+ data: expect.objectContaining({email_jsonb: [{ email: "[email protected]", type: "Work" }],- phone_jsonb: [{ number: "+1234567890", type: "Home" }],+ phone_jsonb: null,}),- ] 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" },- ]);+ it("preserves existing email and phone entries on edit on mobile", async () => {+ const updateMock = vi.fn().mockResolvedValue({ data: {} });+ const screen = await render(+ <ContactEditMobileWithEmailsAndPhones+ silent+ dataProvider={{ update: updateMock }}+ />,+ );+ await screen.getByRole("button", { name: /edit/i }).click();++ // 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("0123456789");++ // Submit without changes+ await screen.getByRole("button", { name: /^save$/i }).click();+ await expect+ .poll(() => screen.getByText("Element updated"))+ .toBeInTheDocument();++ await screen.getByLabelText("Close toast").click();++ // Wait for the update call to complete+ expect(updateMock).toBeCalledTimes(1);++ // Verify existing data is preserved through the transform+ expect(updateMock).toBeCalledWith(+ expect.anything(),+ expect.objectContaining({+ data: expect.objectContaining({+ email_jsonb: [{ email: "[email protected]", type: "Work" }],+ phone_jsonb: [{ number: "0123456789", type: "Work" }],+ }),+ }),+ );+ });});});