Skip to content

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

Cover contact list via stories

Anibal Svarcasfcf8a0f3b23d
diff --git a/src/components/atomic-crm/contacts/ContactList.integration.test.tsx b/src/components/atomic-crm/contacts/ContactList.integration.test.tsx
deleted file mode 100644
index 1ae0c696..00000000
--- a/src/components/atomic-crm/contacts/ContactList.integration.test.tsx
+++ /dev/null
@@ -1,152 +0,0 @@
-import { describe, expect, it, vi } from "vitest";
-import { render } from "vitest-browser-react";
-
-import {
- buildContact,
- createCrmDb,
- createCrmScenario,
- CrmTestProvider,
- DesktopContactListContentHarness,
- DesktopContactListHarness,
- MobileContactListContentHarness,
-} from "@/test/browser/atomic-crm/crmUiHarness";
-
-describe("Contact list integration", () => {
- it("renders the empty state in the desktop list", async () => {
- const scenario = createCrmScenario({
- db: createCrmDb(),
- });
-
- const screen = await render(
- <CrmTestProvider resource="contacts" scenario={scenario}>
- <DesktopContactListHarness />
- </CrmTestProvider>,
- );
-
- await expect
- .element(screen.getByRole("heading", { name: "No contacts found" }))
- .toBeInTheDocument();
- await expect
- .element(screen.getByText("It seems your contact list is empty."))
- .toBeVisible();
- });
-
- it("renders contacts in the desktop success state", async () => {
- const scenario = createCrmScenario({
- db: createCrmDb({
- contacts: [
- buildContact({
- first_name: "Ada",
- id: 1,
- last_name: "Lovelace",
- last_seen: "2025-01-05T10:00:00.000Z",
- title: "CTO",
- }),
- buildContact({
- first_name: "Grace",
- id: 2,
- last_name: "Hopper",
- last_seen: "2025-01-06T11:00:00.000Z",
- title: "Rear Admiral",
- }),
- ] as any,
- }),
- });
-
- const screen = await render(
- <CrmTestProvider resource="contacts" scenario={scenario}>
- <DesktopContactListHarness />
- </CrmTestProvider>,
- );
-
- await expect.element(screen.getByText("Ada Lovelace")).toBeVisible();
- await expect.element(screen.getByText("Grace Hopper")).toBeVisible();
- await expect
- .element(screen.getByRole("heading", { name: "No contacts found" }))
- .not.toBeInTheDocument();
- });
-
- it("renders a loading skeleton for the desktop list content", async () => {
- const scenario = createCrmScenario({
- db: createCrmDb({
- contacts: [buildContact()] as any,
- }),
- getListDelays: { contacts: 5_000 },
- });
-
- const screen = await render(
- <CrmTestProvider resource="contacts" scenario={scenario}>
- <DesktopContactListContentHarness />
- </CrmTestProvider>,
- );
-
- await expect
- .poll(() => screen.container.querySelector('[data-slot="skeleton"]'))
- .not.toBeNull();
- });
-
- it("renders the mobile error state when loading contacts fails", async () => {
- const consoleError = vi
- .spyOn(console, "error")
- .mockImplementation(() => {});
- try {
- const scenario = createCrmScenario({
- db: createCrmDb({
- contacts: [buildContact()] as any,
- }),
- failGetListOnce: { contacts: "Error loading contacts" },
- });
-
- const screen = await render(
- <CrmTestProvider resource="contacts" scenario={scenario}>
- <MobileContactListContentHarness />
- </CrmTestProvider>,
- );
-
- await expect
- .element(screen.getByText("Error loading contacts"))
- .toBeVisible();
- await expect
- .element(screen.getByRole("button", { name: /retry/i }))
- .toBeVisible();
- } finally {
- consoleError.mockRestore();
- }
- });
-
- it("retries successfully in the mobile error state", async () => {
- const consoleError = vi
- .spyOn(console, "error")
- .mockImplementation(() => {});
- try {
- const scenario = createCrmScenario({
- db: createCrmDb({
- contacts: [
- buildContact({
- first_name: "Grace",
- id: 2,
- last_name: "Hopper",
- title: "Rear Admiral",
- }),
- ] as any,
- }),
- failGetListOnce: { contacts: "Error loading contacts" },
- });
-
- const screen = await render(
- <CrmTestProvider resource="contacts" scenario={scenario}>
- <MobileContactListContentHarness />
- </CrmTestProvider>,
- );
-
- await screen.getByRole("button", { name: /retry/i }).click();
-
- await expect.element(screen.getByText("Grace Hopper")).toBeVisible();
- await expect
- .element(screen.getByText("Error loading contacts"))
- .not.toBeInTheDocument();
- } finally {
- consoleError.mockRestore();
- }
- });
-});
diff --git a/src/components/atomic-crm/contacts/ContactList.stories.tsx b/src/components/atomic-crm/contacts/ContactList.stories.tsx
index b47f1428..9eb45040 100644
--- a/src/components/atomic-crm/contacts/ContactList.stories.tsx
+++ b/src/components/atomic-crm/contacts/ContactList.stories.tsx
@@ -37,6 +37,30 @@ const successContacts = [
}),
];
+const bulkTagContacts = [
+ buildContact({
+ first_name: "Ada",
+ id: 1,
+ last_name: "Lovelace",
+ tags: [1],
+ }),
+ buildContact({
+ first_name: "Grace",
+ id: 2,
+ last_name: "Hopper",
+ tags: [],
+ }),
+];
+
+const bulkCreateContacts = Array.from({ length: 30 }, (_, index) =>
+ buildContact({
+ first_name: `Contact${index + 1}`,
+ id: index + 1,
+ last_name: "Tester",
+ tags: [],
+ }),
+);
+
export const EmptyDesktop: Story = {
render: () => (
<CrmStoryProvider
@@ -88,3 +112,34 @@ export const ErrorMobile: Story = {
</CrmStoryProvider>
),
};
+
+export const BulkTagDesktop: Story = {
+ render: () => (
+ <CrmStoryProvider
+ resource="contacts"
+ scenarioOptions={{
+ db: createCrmDb({
+ contacts: bulkTagContacts as any,
+ tags: [{ color: "#A5B4FC", id: 1, name: "VIP" }] as any,
+ }),
+ }}
+ >
+ <DesktopContactListHarness />
+ </CrmStoryProvider>
+ ),
+};
+
+export const BulkCreateTagDesktop: Story = {
+ render: () => (
+ <CrmStoryProvider
+ resource="contacts"
+ scenarioOptions={{
+ db: createCrmDb({
+ contacts: bulkCreateContacts as any,
+ }),
+ }}
+ >
+ <DesktopContactListHarness />
+ </CrmStoryProvider>
+ ),
+};
diff --git a/src/components/atomic-crm/contacts/ContactList.test.tsx b/src/components/atomic-crm/contacts/ContactList.test.tsx
new file mode 100644
index 00000000..3092967c
--- /dev/null
+++ b/src/components/atomic-crm/contacts/ContactList.test.tsx
@@ -0,0 +1,161 @@
+import { composeStories } from "@storybook/react-vite";
+import { describe, expect, it, vi } from "vitest";
+import { render } from "vitest-browser-react";
+
+import * as stories from "./ContactList.stories";
+
+const {
+ BulkCreateTagDesktop,
+ BulkTagDesktop,
+ EmptyDesktop,
+ ErrorMobile,
+ LoadingContent,
+ SuccessDesktop,
+} = composeStories(stories);
+
+const getSelectionCheckboxes = (container: HTMLElement) =>
+ Array.from(container.querySelectorAll('[data-slot="checkbox"]')).map(
+ (element) => element as HTMLElement,
+ );
+
+describe("Contact list", () => {
+ it("renders the empty state in the desktop list", async () => {
+ const screen = await render(<EmptyDesktop />);
+
+ await expect
+ .element(screen.getByRole("heading", { name: "No contacts found" }))
+ .toBeInTheDocument();
+ await expect
+ .element(screen.getByText("It seems your contact list is empty."))
+ .toBeVisible();
+ });
+
+ it("renders contacts in the desktop success state", async () => {
+ const screen = await render(<SuccessDesktop />);
+
+ await expect.element(screen.getByText("Ada Lovelace")).toBeVisible();
+ await expect.element(screen.getByText("Grace Hopper")).toBeVisible();
+ await expect
+ .element(screen.getByRole("heading", { name: "No contacts found" }))
+ .not.toBeInTheDocument();
+ });
+
+ it("renders a loading skeleton for the desktop list content", async () => {
+ const screen = await render(<LoadingContent />);
+
+ await expect
+ .poll(() => screen.container.querySelector('[data-slot="skeleton"]'))
+ .not.toBeNull();
+ });
+
+ it("shows the bulk tag button only after selecting contacts", async () => {
+ const screen = await render(<BulkTagDesktop />);
+
+ await expect
+ .element(screen.getByRole("button", { name: /^tag$/i }))
+ .not.toBeInTheDocument();
+
+ await expect
+ .poll(() => getSelectionCheckboxes(screen.container).length)
+ .toBe(2);
+
+ const [selectionCheckbox] = getSelectionCheckboxes(screen.container);
+ await selectionCheckbox.click();
+
+ await expect
+ .element(screen.getByRole("button", { name: /^tag$/i }))
+ .toBeVisible();
+ });
+
+ it("adds an existing tag to selected contacts without duplicating it", async () => {
+ const screen = await render(<BulkTagDesktop />);
+
+ await expect
+ .poll(() => getSelectionCheckboxes(screen.container).length)
+ .toBe(2);
+
+ const checkboxes = getSelectionCheckboxes(screen.container);
+ await checkboxes[0].click();
+ await checkboxes[1].click();
+
+ await screen.getByRole("button", { name: /^tag$/i }).click();
+ await screen.getByRole("button", { name: "VIP" }).click();
+
+ await expect
+ .element(screen.getByText("Tag added to 1 contact"))
+ .toBeInTheDocument();
+ await expect
+ .poll(() => screen.getByText("VIP").all().length)
+ .toBeGreaterThanOrEqual(2);
+ });
+
+ it("creates a new tag inline and applies it to the full selected list", async () => {
+ const screen = await render(<BulkCreateTagDesktop />);
+
+ await expect
+ .poll(() => getSelectionCheckboxes(screen.container).length)
+ .toBeGreaterThan(0);
+
+ const [selectionCheckbox] = getSelectionCheckboxes(screen.container);
+ await selectionCheckbox.click();
+ await screen.getByRole("button", { name: /select all/i }).click();
+
+ await screen.getByRole("button", { name: /^tag$/i }).click();
+ await screen.getByRole("button", { name: /create new tag/i }).click();
+
+ await expect
+ .element(
+ screen.getByText(
+ "Create a new tag and apply it to the selected contacts.",
+ ),
+ )
+ .toBeVisible();
+
+ await screen.getByLabelText("Tag name").fill("Prospect");
+ await screen.getByRole("button", { name: /^save$/i }).click();
+
+ await expect
+ .element(screen.getByText("Tag added to 30 contacts"))
+ .toBeInTheDocument();
+ await expect.element(screen.getByText("Prospect").first()).toBeVisible();
+ await expect
+ .element(screen.getByText("Add tag to contacts"))
+ .not.toBeInTheDocument();
+ });
+
+ it("renders the mobile error state when loading contacts fails", async () => {
+ const consoleError = vi
+ .spyOn(console, "error")
+ .mockImplementation(() => {});
+ try {
+ const screen = await render(<ErrorMobile />);
+
+ await expect
+ .element(screen.getByText("Error loading contacts"))
+ .toBeVisible();
+ await expect
+ .element(screen.getByRole("button", { name: /retry/i }))
+ .toBeVisible();
+ } finally {
+ consoleError.mockRestore();
+ }
+ });
+
+ it("retries successfully in the mobile error state", async () => {
+ const consoleError = vi
+ .spyOn(console, "error")
+ .mockImplementation(() => {});
+ try {
+ const screen = await render(<ErrorMobile />);
+
+ await screen.getByRole("button", { name: /retry/i }).click();
+
+ await expect.element(screen.getByText("Ada Lovelace")).toBeVisible();
+ await expect
+ .element(screen.getByText("Error loading contacts"))
+ .not.toBeInTheDocument();
+ } finally {
+ consoleError.mockRestore();
+ }
+ });
+});
diff --git a/vitest.config.ts b/vitest.config.ts
index 903994d4..883562e1 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -5,6 +5,18 @@ import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
+ optimizeDeps: {
+ include: [
+ "@storybook/react-vite",
+ "faker/locale/en",
+ "faker/locale/en_US",
+ "jsonexport/dist",
+ "lodash",
+ "papaparse",
+ "ra-supabase-language-english",
+ ],
+ exclude: ["playwright", "playwright-core"],
+ },
test: {
globals: true,
browser: {
@@ -34,9 +46,6 @@ export default defineConfig({
},
},
},
- optimizeDeps: {
- exclude: ["playwright", "playwright-core"],
- },
resolve: {
preserveSymlinks: true,
alias: {