|
| 1 | +import { describe, it, expect, mock, beforeEach, spyOn } from "bun:test"; |
| 2 | +import { render, screen, waitFor } from "@testing-library/svelte"; |
| 3 | +import { |
| 4 | + createFormMock, |
| 5 | + createQueryMock, |
| 6 | +} from "../../../../test-helpers/sveltekit-mocks"; |
| 7 | + |
| 8 | +const mockLeadData = { |
| 9 | + lead: { |
| 10 | + id: "lead-1", |
| 11 | + name: "Acme Corp", |
| 12 | + placeId: "place_123", |
| 13 | + email: "hello@acme.com", |
| 14 | + phone: "+1234567890", |
| 15 | + website: "https://acme.com", |
| 16 | + address: "123 Main St", |
| 17 | + googleMapsUrl: "https://maps.google.com/?cid=123", |
| 18 | + types: ["restaurant"], |
| 19 | + rating: 4.5, |
| 20 | + ratingsTotal: 100, |
| 21 | + businessStatus: "OPERATIONAL", |
| 22 | + createdAt: new Date("2024-01-01"), |
| 23 | + }, |
| 24 | + customFieldSections: [ |
| 25 | + { |
| 26 | + projectId: "proj-1", |
| 27 | + projectName: "Test Project", |
| 28 | + fields: [], |
| 29 | + }, |
| 30 | + ], |
| 31 | +}; |
| 32 | + |
| 33 | +const mockGetLeadData = createQueryMock(mockLeadData); |
| 34 | +const mockDeleteLead = createFormMock({ ok: true }); |
| 35 | +const mockUpdateLeadCore = createFormMock({ ok: true }); |
| 36 | +const mockCreateProjectCustomField = createFormMock({ ok: true }); |
| 37 | +const mockUpsertLeadCustomFieldValue = createFormMock({ ok: true }); |
| 38 | + |
| 39 | +const mockGoto = mock(() => Promise.resolve()); |
| 40 | +const mockResolve = mock((path: string) => path); |
| 41 | + |
| 42 | +mock.module("$lib/remote/leads.remote.js", () => ({ |
| 43 | + getLeadData: mockGetLeadData, |
| 44 | + deleteLead: mockDeleteLead, |
| 45 | + updateLeadCore: mockUpdateLeadCore, |
| 46 | + createProjectCustomField: mockCreateProjectCustomField, |
| 47 | + upsertLeadCustomFieldValue: mockUpsertLeadCustomFieldValue, |
| 48 | + getLeads: createQueryMock([]), |
| 49 | + createManualLead: createFormMock(), |
| 50 | + getDiscoveryCapabilities: createQueryMock({ hasOpenRouter: false }), |
| 51 | + discoverLeads: createFormMock(), |
| 52 | +})); |
| 53 | + |
| 54 | +mock.module("$app/navigation", () => ({ |
| 55 | + goto: mockGoto, |
| 56 | +})); |
| 57 | + |
| 58 | +mock.module("$app/paths", () => ({ |
| 59 | + resolve: mockResolve, |
| 60 | +})); |
| 61 | + |
| 62 | +mock.module("$app/server", () => ({ |
| 63 | + query: (fn: (...args: unknown[]) => unknown) => fn, |
| 64 | + form: () => createFormMock(), |
| 65 | + getRequestEvent: () => ({}), |
| 66 | +})); |
| 67 | + |
| 68 | +const { default: LeadDetailPage } = await import("./+page.svelte"); |
| 69 | + |
| 70 | +describe("Lead detail page", () => { |
| 71 | + beforeEach(() => { |
| 72 | + mockGetLeadData.mockClear(); |
| 73 | + mockGoto.mockClear(); |
| 74 | + mockResolve.mockClear(); |
| 75 | + mockDeleteLead.mockClear(); |
| 76 | + mockGetLeadData.mockImplementation(() => |
| 77 | + Promise.resolve(mockLeadData) |
| 78 | + ); |
| 79 | + mockDeleteLead.mockImplementation(() => Promise.resolve({ ok: true })); |
| 80 | + mockResolve.mockImplementation((path: string) => path); |
| 81 | + }); |
| 82 | + |
| 83 | + it("renders the lead name in heading", async () => { |
| 84 | + render(LeadDetailPage, { params: { id: "lead-1" } }); |
| 85 | + await waitFor(() => { |
| 86 | + expect( |
| 87 | + screen.getByRole("heading", { name: "Acme Corp" }) |
| 88 | + ).toBeTruthy(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it("renders breadcrumbs with Leads link", async () => { |
| 93 | + render(LeadDetailPage, { params: { id: "lead-1" } }); |
| 94 | + await waitFor(() => { |
| 95 | + expect(screen.getByText("Leads")).toBeTruthy(); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("renders the Google Maps link", async () => { |
| 100 | + render(LeadDetailPage, { params: { id: "lead-1" } }); |
| 101 | + await waitFor(() => { |
| 102 | + expect(screen.getByText(/Open in Google Maps/)).toBeTruthy(); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("renders the delete button", async () => { |
| 107 | + render(LeadDetailPage, { params: { id: "lead-1" } }); |
| 108 | + await waitFor(() => { |
| 109 | + expect(screen.getByText("Delete")).toBeTruthy(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe("delete lead", () => { |
| 114 | + it("shows confirmation when Delete is clicked", async () => { |
| 115 | + render(LeadDetailPage, { params: { id: "lead-1" } }); |
| 116 | + |
| 117 | + await waitFor(() => { |
| 118 | + expect(screen.getByText("Delete")).toBeTruthy(); |
| 119 | + }); |
| 120 | + |
| 121 | + screen.getByText("Delete").click(); |
| 122 | + |
| 123 | + await waitFor(() => { |
| 124 | + expect(screen.getByText("Confirm Delete")).toBeTruthy(); |
| 125 | + expect( |
| 126 | + screen.getByText("Delete this lead from all projects?") |
| 127 | + ).toBeTruthy(); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + it("navigates to /leads after successful delete", async () => { |
| 132 | + render(LeadDetailPage, { params: { id: "lead-1" } }); |
| 133 | + |
| 134 | + await waitFor(() => { |
| 135 | + expect(screen.getByText("Delete")).toBeTruthy(); |
| 136 | + }); |
| 137 | + |
| 138 | + screen.getByText("Delete").click(); |
| 139 | + |
| 140 | + await waitFor(() => { |
| 141 | + expect(screen.getByText("Confirm Delete")).toBeTruthy(); |
| 142 | + }); |
| 143 | + |
| 144 | + const confirmButton = screen.getByText("Confirm Delete"); |
| 145 | + const form = confirmButton.closest("form")!; |
| 146 | + form.dispatchEvent(new Event("submit", { bubbles: true })); |
| 147 | + |
| 148 | + await waitFor(() => { |
| 149 | + expect(mockGoto).toHaveBeenCalledTimes(1); |
| 150 | + expect(mockGoto).toHaveBeenCalledWith("/leads"); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + it("shows error message when delete fails", async () => { |
| 155 | + const consoleSpy = spyOn(console, "error").mockImplementation( |
| 156 | + () => {} |
| 157 | + ); |
| 158 | + mockDeleteLead.mockImplementation(() => |
| 159 | + Promise.reject(new Error("Server error")) |
| 160 | + ); |
| 161 | + |
| 162 | + render(LeadDetailPage, { params: { id: "lead-1" } }); |
| 163 | + |
| 164 | + await waitFor(() => { |
| 165 | + expect(screen.getByText("Delete")).toBeTruthy(); |
| 166 | + }); |
| 167 | + |
| 168 | + screen.getByText("Delete").click(); |
| 169 | + |
| 170 | + await waitFor(() => { |
| 171 | + expect(screen.getByText("Confirm Delete")).toBeTruthy(); |
| 172 | + }); |
| 173 | + |
| 174 | + const confirmButton = screen.getByText("Confirm Delete"); |
| 175 | + const form = confirmButton.closest("form")!; |
| 176 | + form.dispatchEvent(new Event("submit", { bubbles: true })); |
| 177 | + |
| 178 | + await waitFor(() => { |
| 179 | + expect( |
| 180 | + screen.getByText("Failed to delete lead. Please try again.") |
| 181 | + ).toBeTruthy(); |
| 182 | + }); |
| 183 | + |
| 184 | + expect(mockGoto).not.toHaveBeenCalled(); |
| 185 | + consoleSpy.mockRestore(); |
| 186 | + }); |
| 187 | + }); |
| 188 | +}); |
0 commit comments