Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Test that a pending application gets an enrollment on sign-in

Rafid194544e974a
diff --git a/apps/web/tests/partners/complete-program-applications.test.ts b/apps/web/tests/partners/complete-program-applications.test.ts
index 2b0d4110b8..5cce31b168 100644
--- a/apps/web/tests/partners/complete-program-applications.test.ts
+++ b/apps/web/tests/partners/complete-program-applications.test.ts
@@ -1,11 +1,13 @@
import { completeProgramApplications } from "@/lib/partners/complete-program-applications";
import { ProgramApplicationStatus } from "@prisma/client";
-import { beforeEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
findUniqueOrThrow: vi.fn(),
findMany: vi.fn(),
+ createMany: vi.fn(),
transaction: vi.fn(),
+ queuePartnerSearchSync: vi.fn(),
}));
vi.mock("server-only", () => ({}));
@@ -20,13 +22,53 @@ vi.mock("@/lib/prisma", () => ({
updateMany: vi.fn(),
},
programEnrollment: {
- createMany: vi.fn(),
+ createMany: mocks.createMany,
+ },
+ project: {
+ findMany: vi.fn().mockResolvedValue([]),
},
$transaction: mocks.transaction,
},
}));
+// Side effects of a completed application are not under test here
+vi.mock("@/lib/api/fraud/detect-record-fraud-application", () => ({
+ detectAndRecordFraudApplication: vi.fn(),
+}));
+
+vi.mock("@/lib/api/partners/notify-program-application", () => ({
+ notifyProgramApplication: vi.fn(),
+}));
+
+vi.mock("@/lib/api/partners/queue-partner-search-sync", () => ({
+ queuePartnerSearchSync: mocks.queuePartnerSearchSync,
+}));
+
+vi.mock("@/lib/application-events/update-application-event", () => ({
+ markApplicationEventSubmitted: vi.fn(),
+}));
+
+vi.mock("@/lib/jobs/handlers/auto-reject-program-application-job", () => ({
+ autoRejectProgramApplicationJob: { dispatch: vi.fn() },
+}));
+
+vi.mock("@/lib/webhook/publish", () => ({
+ sendWorkspaceWebhook: vi.fn(),
+}));
+
+vi.mock("@/lib/partners/backfill-partner-platforms", () => ({
+ backfillPartnerPlatforms: vi.fn().mockResolvedValue({ platforms: [] }),
+ mergeApplicationSocialPlatforms: vi
+ .fn()
+ .mockReturnValue({ platforms: [], socialFields: {} }),
+}));
+
+vi.mock("@/lib/partners/dispatch-partner-application-review", () => ({
+ dispatchPartnerApplicationReview: vi.fn(),
+}));
+
const EMAIL = "[email protected]";
+const PARTNER_ID = "pn_test";
// Rejected with an instant reapplication timeframe, so the enrollment
// was deleted and the application has no enrollment.
@@ -38,27 +80,57 @@ const rejectedApplication = {
createdAt: new Date("2026-10-09"),
};
+// Submitted while logged out, so it waits for the partner to sign in
+const pendingApplication = {
+ id: "pga_pending",
+ programId: "prog_other",
+ email: EMAIL,
+ status: ProgramApplicationStatus.pending,
+ formData: null,
+ createdAt: new Date("2026-10-10"),
+ program: {
+ id: "prog_other",
+ applicationRequirements: null,
+ applicationScreeningCriteria: null,
+ },
+ partnerGroup: null,
+};
+
+function mockApplications(applications: { status: string }[]) {
+ mocks.findMany.mockImplementation(({ where }) =>
+ applications.filter(
+ (application) => !where.status || application.status === where.status,
+ ),
+ );
+}
+
describe("completeProgramApplications", () => {
+ // The function catches and logs every error, so a failure is only visible here
+ let consoleError: ReturnType<typeof vi.spyOn>;
+
beforeEach(() => {
vi.clearAllMocks();
+ consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
mocks.findUniqueOrThrow.mockResolvedValue({
partners: [
{
- partnerId: "pn_test",
- partner: { id: "pn_test", platforms: [], programs: [] },
+ partnerId: PARTNER_ID,
+ partner: { id: PARTNER_ID, platforms: [], programs: [] },
},
],
});
- mocks.findMany.mockImplementation(({ where }) =>
- [rejectedApplication].filter(
- (application) => !where.status || application.status === where.status,
- ),
- );
+ mocks.transaction.mockResolvedValue([]);
+ });
+
+ afterEach(() => {
+ consoleError.mockRestore();
});
it("only queries pending applications without an enrollment", async () => {
+ mockApplications([rejectedApplication]);
+
await completeProgramApplications(EMAIL);
expect(mocks.findMany).toHaveBeenCalledWith(
@@ -73,8 +145,32 @@ describe("completeProgramApplications", () => {
});
it("does not recreate an enrollment for a rejected application", async () => {
+ mockApplications([rejectedApplication]);
+
await completeProgramApplications(EMAIL);
expect(mocks.transaction).not.toHaveBeenCalled();
});
+
+ it("creates an enrollment only for the pending application", async () => {
+ mockApplications([pendingApplication, rejectedApplication]);
+
+ await completeProgramApplications(EMAIL);
+
+ expect(mocks.createMany).toHaveBeenCalledWith({
+ data: [
+ expect.objectContaining({
+ applicationId: pendingApplication.id,
+ programId: pendingApplication.programId,
+ partnerId: PARTNER_ID,
+ }),
+ ],
+ skipDuplicates: true,
+ });
+ expect(mocks.transaction).toHaveBeenCalledOnce();
+ expect(mocks.queuePartnerSearchSync).toHaveBeenCalledWith({
+ enrollmentIds: [expect.any(String)],
+ });
+ expect(consoleError).not.toHaveBeenCalled();
+ });
});