Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions apps/web/tests/rewards/lead-reward.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { TrackLeadResponse } from "@/lib/types";
import { randomCustomer } from "tests/utils/helpers";
import {
E2E_LEAD_REWARD,
E2E_PARTNERS,
E2E_TRACK_CLICK_HEADERS,
} from "tests/utils/resource";
import { verifyCommission } from "tests/utils/verify-commission";
import { describe, expect, test } from "vitest";
import { IntegrationHarness } from "../utils/integration";

describe.concurrent("Lead rewards", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();

test("when {Partner} {Country} is {US}", async () => {
// Track the click
const clickResponse = await http.post<{ clickId: string }>({
path: "/track/click",
headers: E2E_TRACK_CLICK_HEADERS,
body: {
...E2E_PARTNERS[0].shortLink,
},
});

expect(clickResponse.status).toEqual(200);

const clickId = clickResponse.data.clickId;
const customer = randomCustomer();

// Track the lead
const trackLeadResponse = await http.post<TrackLeadResponse>({
path: "/track/lead",
body: {
clickId,
eventName: "Signup",
customerExternalId: customer.externalId,
customerName: customer.name,
customerEmail: customer.email,
customerAvatar: customer.avatar,
},
});

expect(trackLeadResponse.status).toEqual(200);

// Verify the commission
await verifyCommission({
http,
customerExternalId: customer.externalId,
expectedEarnings: E2E_LEAD_REWARD.modifiers[0].amountInCents,
});
});

test("when {Partner} {Country} is not {US}", async () => {
// Track the click
const clickResponse = await http.post<{ clickId: string }>({
path: "/track/click",
headers: E2E_TRACK_CLICK_HEADERS,
body: {
...E2E_PARTNERS[1].shortLink,
},
});

expect(clickResponse.status).toEqual(200);

const clickId = clickResponse.data.clickId;
const customer = randomCustomer();

// Track the lead
const trackLeadResponse = await http.post<TrackLeadResponse>({
path: "/track/lead",
body: {
clickId,
eventName: "Signup",
customerExternalId: customer.externalId,
customerName: customer.name,
customerEmail: customer.email,
customerAvatar: customer.avatar,
},
});

expect(trackLeadResponse.status).toEqual(200);

// Verify the commission
await verifyCommission({
http,
customerExternalId: customer.externalId,
expectedEarnings: E2E_LEAD_REWARD.amountInCents,
});
});
});
65 changes: 65 additions & 0 deletions apps/web/tests/rewards/sale-reward.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { TrackSaleResponse } from "@/lib/types";
import { randomId } from "tests/utils/helpers";
import { E2E_CUSTOMERS, E2E_SALE_REWARD } from "tests/utils/resource";
import { verifyCommission } from "tests/utils/verify-commission";
import { describe, expect, test } from "vitest";
import { IntegrationHarness } from "../utils/integration";

describe.concurrent("Sale rewards", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();

test("when {Customer} {Country} is {SG}", async () => {
const saleAmount = 10000; // $100 in cents
const invoiceId = `INV_${randomId()}`;

// Track the sale
const trackSaleResponse = await http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
customerExternalId: E2E_CUSTOMERS[0].externalId,
eventName: "Subscription",
amount: saleAmount,
currency: "usd",
invoiceId,
paymentProcessor: "stripe",
},
});

expect(trackSaleResponse.status).toEqual(200);

// Verify the commission (10% of sale amount)
await verifyCommission({
http,
invoiceId,
expectedEarnings: saleAmount * 0.1,
});
});

test.skip("when {Customer} {Country} is {CA}", async () => {
const saleAmount = 10000; // $100 in cents
const invoiceId = `INV_${randomId()}`;

// Track the sale
const trackSaleResponse = await http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
customerExternalId: E2E_CUSTOMERS[1].externalId,
eventName: "Subscription",
amount: saleAmount,
currency: "usd",
invoiceId,
paymentProcessor: "stripe",
},
});

expect(trackSaleResponse.status).toEqual(200);

// Verify the commission (base reward)
await verifyCommission({
http,
invoiceId,
expectedEarnings: E2E_SALE_REWARD.amountInCents,
});
});
});
83 changes: 2 additions & 81 deletions apps/web/tests/tracks/track-lead.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import {
CommissionResponse,
Customer,
TrackLeadResponse,
TrackSaleResponse,
} from "@/lib/types";
import { TrackLeadResponse, TrackSaleResponse } from "@/lib/types";
import { randomCustomer } from "tests/utils/helpers";
import { HttpClient } from "tests/utils/http";
import { E2E_LEAD_REWARD, E2E_TRACK_CLICK_HEADERS } from "tests/utils/resource";
import { E2E_TRACK_CLICK_HEADERS } from "tests/utils/resource";
import { describe, expect, test } from "vitest";
import { IntegrationHarness } from "../utils/integration";

Expand All @@ -30,39 +24,6 @@ const expectValidLeadResponse = ({
});
};

const verifyCommission = async ({
http,
customerExternalId,
expectedEarnings,
}: {
http: HttpClient;
customerExternalId: string;
expectedEarnings: number;
}) => {
// Find the customer first
const { data: customers } = await http.get<Customer[]>({
path: "/customers",
query: {
externalId: customerExternalId,
},
});

const customer = customers[0];

// Find the commission for the customer
const { status, data: commissions } = await http.get<CommissionResponse[]>({
path: "/commissions",
query: {
customerId: customer.id,
},
});

expect(status).toEqual(200);
expect(commissions).toHaveLength(1);
expect(commissions[0].customer?.id).toEqual(customer.id);
expect(commissions[0].earnings).toEqual(expectedEarnings);
};

describe("POST /track/lead", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();
Expand Down Expand Up @@ -254,44 +215,4 @@ describe("POST /track/lead", async () => {
clickId: trackedClickId,
});
});

test("track a lead and verify the reward based on the partner.country (US)", async () => {
const clickResponse = await http.post<{ clickId: string }>({
path: "/track/click",
headers: E2E_TRACK_CLICK_HEADERS,
body: {
domain: "getacme.link",
key: "marvin",
},
});

const trackedClickId = clickResponse.data.clickId;
const customer = randomCustomer();

const response = await http.post<TrackLeadResponse>({
path: "/track/lead",
body: {
clickId: trackedClickId,
customerId: customer.externalId,
eventName: "Signup",
customerName: customer.name,
customerEmail: customer.email,
customerAvatar: customer.avatar,
},
});

expectValidLeadResponse({
response,
customer: customer,
clickId: trackedClickId,
});

await new Promise((resolve) => setTimeout(resolve, 2000));

await verifyCommission({
http,
customerExternalId: customer.externalId,
expectedEarnings: E2E_LEAD_REWARD.modifiers[0].amountInCents,
});
});
});
62 changes: 22 additions & 40 deletions apps/web/tests/tracks/track-sale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
E2E_SALE_REWARD,
E2E_TRACK_CLICK_HEADERS,
} from "tests/utils/resource";
import { verifyCommission } from "tests/utils/verify-commission";
import { describe, expect, test } from "vitest";
import { IntegrationHarness } from "../utils/integration";

Expand Down Expand Up @@ -38,25 +39,6 @@ const expectValidSaleResponse = (
});
};

// Helper function to verify commission details
const verifyCommission = async (
http: any,
invoiceId: string,
expectedAmount: number,
expectedEarnings: number,
) => {
const { status, data: commissions } = await http.get({
path: "/commissions",
query: { invoiceId },
});

expect(status).toEqual(200);
expect(commissions).toHaveLength(1);
expect(commissions[0].invoiceId).toEqual(invoiceId);
expect(commissions[0].amount).toEqual(expectedAmount);
expect(commissions[0].earnings).toEqual(expectedEarnings);
};

describe("POST /track/sale", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();
Expand Down Expand Up @@ -129,19 +111,19 @@ describe("POST /track/sale", async () => {
// pause for 2 seconds for data to be fully processed
await new Promise((resolve) => setTimeout(resolve, 2000));

// Verify commissions
await verifyCommission(
await verifyCommission({
http,
regularInvoiceId,
response1.data.sale?.amount!,
E2E_SALE_REWARD.amountInCents,
);
await verifyCommission(
invoiceId: regularInvoiceId,
expectedAmount: response1.data.sale?.amount!,
expectedEarnings: E2E_SALE_REWARD.amountInCents,
});

await verifyCommission({
http,
premiumInvoiceId,
response2.data.sale?.amount!,
E2E_SALE_REWARD.modifiers[0].amountInCents,
);
invoiceId: premiumInvoiceId,
expectedAmount: response2.data.sale?.amount!,
expectedEarnings: E2E_SALE_REWARD.modifiers[0].amountInCents!,
});
});

test("track a sale with an externalId that does not exist (should return null customer and sale)", async () => {
Expand Down Expand Up @@ -311,18 +293,18 @@ describe("POST /track/sale", async () => {
// Pause for 2 seconds for data to be fully processed
await new Promise((resolve) => setTimeout(resolve, 2000));

await verifyCommission(
await verifyCommission({
http,
smallSaleInvoiceId,
response1.data.sale?.amount!,
E2E_SALE_REWARD.amountInCents,
);
invoiceId: smallSaleInvoiceId,
expectedAmount: response1.data.sale?.amount!,
expectedEarnings: E2E_SALE_REWARD.amountInCents,
});

await verifyCommission(
await verifyCommission({
http,
largeSaleInvoiceId,
response2.data.sale?.amount!,
E2E_SALE_REWARD.modifiers[1].amountInCents,
);
invoiceId: largeSaleInvoiceId,
expectedAmount: response2.data.sale?.amount!,
expectedEarnings: E2E_SALE_REWARD.modifiers[1].amountInCents!,
});
});
});
Loading