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.

add tests

Kiran Kebb8895c60a6
diff --git a/apps/web/app/api/customers/route.ts b/apps/web/app/api/customers/route.ts
index 67de6cc722..05ae6836b9 100644
--- a/apps/web/app/api/customers/route.ts
+++ b/apps/web/app/api/customers/route.ts
@@ -50,7 +50,9 @@ export const POST = withWorkspace(
},
});
- return NextResponse.json(CustomerSchema.parse(customer));
+ return NextResponse.json(CustomerSchema.parse(customer), {
+ status: 201,
+ });
} catch (error) {
if (error.code === "P2002") {
throw new DubApiError({
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 70f9d79249..656a2573ce 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -3,7 +3,10 @@ import { metaTagsSchema } from "@/lib/zod/schemas/metatags";
import { DirectorySyncProviders } from "@boxyhq/saml-jackson";
import { Link, Project, Webhook } from "@prisma/client";
import { WEBHOOK_TRIGGER_DESCRIPTIONS } from "./webhook/constants";
-import { trackCustomerResponseSchema } from "./zod/schemas/customers";
+import {
+ CustomerSchema,
+ trackCustomerResponseSchema,
+} from "./zod/schemas/customers";
import { integrationSchema } from "./zod/schemas/integration";
import { trackLeadResponseSchema } from "./zod/schemas/leads";
import { createLinkBodySchema } from "./zod/schemas/links";
@@ -259,3 +262,5 @@ export type TrackCustomerResponse = z.infer<typeof trackCustomerResponseSchema>;
export type TrackLeadResponse = z.infer<typeof trackLeadResponseSchema>;
export type TrackSaleResponse = z.infer<typeof trackSaleResponseSchema>;
+
+export type Customer = z.infer<typeof CustomerSchema>;
diff --git a/apps/web/tests/customers/index.test.ts b/apps/web/tests/customers/index.test.ts
new file mode 100644
index 0000000000..98b399a4ae
--- /dev/null
+++ b/apps/web/tests/customers/index.test.ts
@@ -0,0 +1,90 @@
+import { Customer } from "@/lib/types";
+import { afterAll, describe, expect, test } from "vitest";
+import { randomId } from "../utils/helpers";
+import { IntegrationHarness } from "../utils/integration";
+
+const externalId = `cust_${randomId()}`;
+
+const customerRecord = {
+ externalId,
+ name: "John Doe",
+ email: `${externalId}@example.com`,
+ avatar: `https://www.gravatar.com/avatar/${externalId}`,
+};
+
+const expectedCustomer = {
+ id: expect.any(String),
+ externalId: customerRecord.externalId,
+ name: customerRecord.name,
+ email: customerRecord.email,
+ avatar: customerRecord.avatar,
+ createdAt: expect.any(String),
+};
+
+describe.sequential("/customers/**", async () => {
+ const h = new IntegrationHarness();
+ const { workspace, http } = await h.init();
+
+ afterAll(async () => {
+ await h.deleteCustomer(customerRecord.externalId);
+ });
+
+ test("POST /customers", async () => {
+ const { status, data: customer } = await http.post<Customer>({
+ path: "/customers",
+ query: { workspaceId: workspace.id },
+ body: customerRecord,
+ });
+
+ expect(status).toEqual(201);
+ expect(customer).toStrictEqual(expectedCustomer);
+ });
+
+ test("GET /customers/{externalId}", async () => {
+ const { status, data: customer } = await http.get<Customer>({
+ path: `/customers/${customerRecord.externalId}`,
+ query: { workspaceId: workspace.id },
+ });
+
+ expect(status).toEqual(200);
+ expect(customer).toStrictEqual(expectedCustomer);
+ });
+
+ test("GET /customers", async () => {
+ const { status, data: customers } = await http.get<Customer[]>({
+ path: "/customers",
+ query: { workspaceId: workspace.id },
+ });
+
+ expect(status).toEqual(200);
+ expect(customers).toContainEqual(expectedCustomer);
+ });
+
+ test("PATCH /customers/{externalId}", async () => {
+ const toUpdate = {
+ email: `${externalId}@example.co`,
+ };
+
+ const { status, data: customer } = await http.patch<Customer>({
+ path: `/customers/${customerRecord.externalId}`,
+ query: { workspaceId: workspace.id },
+ body: toUpdate,
+ });
+
+ expect(status).toEqual(200);
+ expect(customer).toStrictEqual({
+ ...expectedCustomer,
+ ...toUpdate,
+ });
+ });
+
+ test("DELETE /customers/{externalId}", async () => {
+ const { status, data } = await http.delete({
+ path: `/customers/${customerRecord.externalId}`,
+ query: { workspaceId: workspace.id },
+ });
+
+ expect(status).toEqual(200);
+ expect(data).toEqual({ externalId: customerRecord.externalId });
+ });
+});
diff --git a/apps/web/tests/utils/integration.ts b/apps/web/tests/utils/integration.ts
index 4789d8f860..610807e729 100644
--- a/apps/web/tests/utils/integration.ts
+++ b/apps/web/tests/utils/integration.ts
@@ -73,4 +73,11 @@ export class IntegrationHarness {
path: `/domains/${slug}`,
});
}
+
+ // Delete customer
+ public async deleteCustomer(externalId: string) {
+ await this.http.delete({
+ path: `/customers/${externalId}`,
+ });
+ }
}