The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
improve track/lead test
Steven Tey66ceb6a70888
diff --git a/apps/web/lib/zod/schemas/sales.ts b/apps/web/lib/zod/schemas/sales.tsindex 4c23a9d697..bf1b4bcfd6 100644--- a/apps/web/lib/zod/schemas/sales.ts+++ b/apps/web/lib/zod/schemas/sales.ts@@ -29,7 +29,7 @@ export const trackSaleRequestSchema = z.object({.min(0, "amount cannot be negative").describe("The amount of the sale. Should be passed in cents."),paymentProcessor: z- .enum(["stripe", "shopify", "paddle"])+ .enum(["stripe", "shopify", "polar", "paddle", "custom"]).describe("The payment processor via which the sale was made."),eventName: z.string()diff --git a/apps/web/tests/tracks/track-lead.test.ts b/apps/web/tests/tracks/track-lead.test.tsindex 63e75f8f7f..efa80ca169 100644--- a/apps/web/tests/tracks/track-lead.test.ts+++ b/apps/web/tests/tracks/track-lead.test.ts@@ -1,45 +1,46 @@-import { getLeadEvent } from "@/lib/tinybird";-import { TrackLeadResponse } from "@/lib/types";-import { prisma } from "@dub/prisma";+import { TrackLeadResponse, TrackSaleResponse } from "@/lib/types";import { randomCustomer } from "tests/utils/helpers";-import { E2E_CLICK_ID, E2E_WORKSPACE_ID } from "tests/utils/resource";+import { E2E_CLICK_ID } from "tests/utils/resource";import { describe, expect, test } from "vitest";import { IntegrationHarness } from "../utils/integration";+// Helper function to verify lead tracking response+const expectValidLeadResponse = (+ response: { status: number; data: TrackLeadResponse },+ customer: any,+) => {+ expect(response.status).toEqual(200);+ expect(response.data).toStrictEqual({+ clickId: E2E_CLICK_ID,+ customerName: customer.name,+ customerEmail: customer.email,+ customerAvatar: customer.avatar,+ click: {+ id: E2E_CLICK_ID,+ },+ customer,+ });+};+describe("POST /track/lead", async () => {const h = new IntegrationHarness();const { http } = await h.init();- const customer = randomCustomer();+ const customer1 = randomCustomer();test("track a lead", async () => {const response = await http.post<TrackLeadResponse>({path: "/track/lead",body: {clickId: E2E_CLICK_ID,eventName: "Signup",- externalId: customer.id,- customerName: customer.name,- customerEmail: customer.email,- customerAvatar: customer.avatar,+ externalId: customer1.externalId,+ customerName: customer1.name,+ customerEmail: customer1.email,+ customerAvatar: customer1.avatar,},});- expect(response.status).toEqual(200);- expect(response.data).toStrictEqual({- clickId: E2E_CLICK_ID,- customerName: customer.name,- customerEmail: customer.email,- customerAvatar: customer.avatar,- click: {- id: E2E_CLICK_ID,- },- customer: {- name: customer.name,- email: customer.email,- avatar: customer.avatar,- externalId: customer.id,- },- });+ expectValidLeadResponse(response, customer1);});test("duplicate request with same externalId", async () => {@@ -48,7 +49,7 @@ describe("POST /track/lead", async () => {body: {clickId: E2E_CLICK_ID,eventName: "Signup",- externalId: customer.id,+ externalId: customer1.externalId,},});@@ -57,7 +58,7 @@ describe("POST /track/lead", async () => {error: {code: "conflict",doc_url: "https://dub.co/docs/api-reference/errors#conflict",- message: `Customer with externalId ${customer.id} and event name Signup has already been recorded.`,+ message: `Customer with externalId ${customer1.externalId} and event name Signup has already been recorded.`,},});});@@ -69,7 +70,7 @@ describe("POST /track/lead", async () => {body: {clickId: E2E_CLICK_ID,eventName: "Start Trial",- externalId: customer2.id,+ externalId: customer2.externalId,customerName: customer2.name,customerEmail: customer2.email,customerAvatar: customer2.avatar,@@ -77,54 +78,35 @@ describe("POST /track/lead", async () => {},});- expect(response.status).toEqual(200);- expect(response.data).toStrictEqual({- clickId: E2E_CLICK_ID,- customerName: customer2.name,- customerEmail: customer2.email,- customerAvatar: customer2.avatar,- click: {- id: E2E_CLICK_ID,- },- customer: {- name: customer2.name,- email: customer2.email,- avatar: customer2.avatar,- externalId: customer2.id,- },- });+ expectValidLeadResponse(response, customer2);});- test("track a lead with mode = 'wait'", async () => {+ test("track a lead with mode = 'wait' + track a sale right after", async () => {const customer3 = randomCustomer();const response = await http.post<TrackLeadResponse>({path: "/track/lead",body: {clickId: E2E_CLICK_ID,eventName: "Signup",- externalId: customer3.id,+ externalId: customer3.externalId,customerName: customer3.name,customerEmail: customer3.email,customerAvatar: customer3.avatar,mode: "wait",},});- expect(response.status).toEqual(200);+ expectValidLeadResponse(response, customer3);- const createdCustomer = await prisma.customer.findUniqueOrThrow({- where: {- projectId_externalId: {- projectId: E2E_WORKSPACE_ID.replace("ws_", ""),- externalId: customer3.id,- },- },- select: {- id: true,+ const saleResponse = await http.post<TrackSaleResponse>({+ path: "/track/sale",+ body: {+ externalId: customer3.externalId,+ eventName: "Purchase",+ amount: 500,+ paymentProcessor: "stripe",},});- const leadEvent = await getLeadEvent({ customerId: createdCustomer.id });- expect(leadEvent.data.length).toEqual(1);- expect(leadEvent.data[0].event_name).toEqual("Signup");- expect(leadEvent.data[0].customer_id).toEqual(createdCustomer.id);++ expect(saleResponse.status).toEqual(200);});});diff --git a/apps/web/tests/utils/helpers.ts b/apps/web/tests/utils/helpers.tsindex 27ee792563..cf60adfcc0 100644--- a/apps/web/tests/utils/helpers.ts+++ b/apps/web/tests/utils/helpers.ts@@ -5,14 +5,14 @@ export const randomId = () => nanoid(24);// Generate random customer dataexport const randomCustomer = () => {- const customerId = `cus_${randomId()}`;+ const externalId = `cus_${randomId()}`;const customerName = generateRandomName();return {- id: customerId,+ externalId,name: customerName,email: `${customerName.split(" ").join(".").toLowerCase()}@example.com`,- avatar: `https://api.dicebear.com/9.x/notionists/svg?seed=${customerId}`,+ avatar: `https://api.dicebear.com/9.x/notionists/svg?seed=${externalId}`,};};