The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
test: refactor verifyCommission to use direct Prisma queries instead of HTTP API
Pedro Ladeirac23be6b2ba02
diff --git a/apps/web/tests/rewards/lead-reward.test.ts b/apps/web/tests/rewards/lead-reward.test.tsindex 95456359ec..66adf9e8b5 100644--- a/apps/web/tests/rewards/lead-reward.test.ts+++ b/apps/web/tests/rewards/lead-reward.test.ts@@ -45,7 +45,6 @@ describe.concurrent("Lead rewards", async () => {// Verify the commissionawait verifyCommission({- http,customerExternalId: customer.externalId,expectedEarnings: E2E_LEAD_REWARD.modifiers[1].amountInCents,});@@ -83,7 +82,6 @@ describe.concurrent("Lead rewards", async () => {// Verify the commissionawait verifyCommission({- http,customerExternalId: customer.externalId,expectedEarnings: E2E_LEAD_REWARD.modifiers[0].amountInCents,});diff --git a/apps/web/tests/rewards/sale-reward.test.ts b/apps/web/tests/rewards/sale-reward.test.tsindex 27bdf0ced1..89d6c0b5cb 100644--- a/apps/web/tests/rewards/sale-reward.test.ts+++ b/apps/web/tests/rewards/sale-reward.test.ts@@ -30,7 +30,6 @@ describe.concurrent("Sale rewards", async () => {// Verify the commission (10% of sale amount)await verifyCommission({- http,invoiceId,expectedEarnings: saleAmount * 0.1,});@@ -57,7 +56,6 @@ describe.concurrent("Sale rewards", async () => {// Verify the commission (base reward)await verifyCommission({- http,invoiceId,expectedEarnings: E2E_SALE_REWARD.amountInCents,});diff --git a/apps/web/tests/tracks/track-sale.test.ts b/apps/web/tests/tracks/track-sale.test.tsindex ea55faf1fd..bef3e1d9b8 100644--- a/apps/web/tests/tracks/track-sale.test.ts+++ b/apps/web/tests/tracks/track-sale.test.ts@@ -114,14 +114,12 @@ describe.concurrent("POST /track/sale", async () => {await Promise.all([verifyCommission({- http,invoiceId: regularInvoiceId,expectedAmount: response1.data.sale?.amount!,expectedEarnings: E2E_SALE_REWARD.amountInCents,}),verifyCommission({- http,invoiceId: premiumInvoiceId,expectedAmount: response2.data.sale?.amount!,expectedEarnings: E2E_SALE_REWARD.modifiers[0].amountInCents!,@@ -297,14 +295,12 @@ describe.concurrent("POST /track/sale", async () => {await Promise.all([verifyCommission({- http,invoiceId: smallSaleInvoiceId,expectedAmount: response1.data.sale?.amount!,expectedEarnings: E2E_SALE_REWARD.amountInCents,}),verifyCommission({- http,invoiceId: largeSaleInvoiceId,expectedAmount: response2.data.sale?.amount!,expectedEarnings: E2E_SALE_REWARD.modifiers[1].amountInCents!,diff --git a/apps/web/tests/utils/verify-commission.ts b/apps/web/tests/utils/verify-commission.tsindex dc33a9b273..89a2b1af01 100644--- a/apps/web/tests/utils/verify-commission.ts+++ b/apps/web/tests/utils/verify-commission.ts@@ -1,9 +1,7 @@-import { CommissionResponse, Customer } from "@/lib/types";import { expect } from "vitest";-import { HttpClient } from "./http";+import { prisma } from "./prisma";interface VerifyCommissionProps {- http: HttpClient;customerExternalId?: string;invoiceId?: string;expectedAmount?: number;@@ -14,7 +12,6 @@ const POLL_INTERVAL_MS = 5000; // 5 secondsconst TIMEOUT_MS = 30000; // 30 secondsexport const verifyCommission = async ({- http,customerExternalId,invoiceId,expectedAmount,@@ -24,44 +21,33 @@ export const verifyCommission = async ({// Resolve customer ID first if customerExternalId is givenif (customerExternalId) {- const { data: customers } = await http.get<Customer[]>({- path: "/customers",- query: { externalId: customerExternalId },+ const customer = await prisma.customer.findFirst({+ where: { externalId: customerExternalId },+ select: { id: true },});- expect(customers.length).toBeGreaterThan(0);- customerId = customers[0].id;- }-- const query: Record<string, string> = {};-- if (invoiceId) {- query.invoiceId = invoiceId;- }-- if (customerId) {- query.customerId = customerId;+ expect(customer).not.toBeNull();+ customerId = customer!.id;}// Poll for commission every 5 seconds, timeout after 30 secondsconst startTime = Date.now();while (Date.now() - startTime < TIMEOUT_MS) {- const { status, data: commissions } = await http.get<CommissionResponse[]>({- path: "/commissions",- query,+ const commission = await prisma.commission.findFirst({+ where: {+ ...(customerId && { customerId }),+ ...(invoiceId && { invoiceId }),+ },});- if (status === 200 && commissions.length === 1) {- const commission = commissions[0];-- // Verify all expectations+ if (commission) {if (invoiceId) {expect(commission.invoiceId).toEqual(invoiceId);}if (customerId) {- expect(commission.customer?.id).toEqual(customerId);+ expect(commission.customerId).toEqual(customerId);}if (expectedAmount !== undefined) {@@ -70,7 +56,7 @@ export const verifyCommission = async ({expect(commission.earnings).toEqual(expectedEarnings);- return;+ return commission;}// Wait before next poll@@ -80,6 +66,6 @@ export const verifyCommission = async ({// Timeout reached - fail the testthrow new Error(`Commission not found within ${TIMEOUT_MS / 1000} seconds. ` +- `Query: ${JSON.stringify(query)}`,+ `customerId: ${customerId}, invoiceId: ${invoiceId}`,);};