The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
improve tests for links creation
Kiran K361142a7fb74
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.tsindex 0f86edb48c..534b09af0b 100644--- a/apps/web/lib/zod/schemas/links.ts+++ b/apps/web/lib/zod/schemas/links.ts@@ -210,6 +210,33 @@ export const createLinkBodySchema = z.object({.describe("Geo targeting information for the short link in JSON format `{[COUNTRY]: https://example.com }`.",),++ // UTM builder+ utm_source: z+ .string()+ .nullish()+ .describe("The UTM source of the short link.")+ .openapi({ example: "twitter" }),+ utm_medium: z+ .string()+ .nullish()+ .describe("The UTM medium of the short link.")+ .openapi({ example: "social" }),+ utm_campaign: z+ .string()+ .nullish()+ .describe("The UTM campaign of the short link.")+ .openapi({ example: "summer_sale" }),+ utm_term: z+ .string()+ .nullish()+ .describe("The UTM term of the short link.")+ .openapi({ example: "blue_shoes" }),+ utm_content: z+ .string()+ .nullish()+ .describe("The UTM content of the short link.")+ .openapi({ example: "logolink" }),});export const updateLinkBodySchema = createLinkBodySchema.partial();diff --git a/apps/web/tests/links/create-link.test.ts b/apps/web/tests/links/create-link.test.tsindex c96914cc00..87ac3c6277 100644--- a/apps/web/tests/links/create-link.test.ts+++ b/apps/web/tests/links/create-link.test.ts@@ -1,12 +1,14 @@-import { expect, test } from "vitest";+import { expect, test, describe, beforeAll, afterAll } from "vitest";import { HttpClient } from "../utils/http";import { Link } from "@prisma/client";import { IntegrationHarness } from "../utils/integration";import { expectedLink } from "../utils/schema";import { nanoid } from "@dub/utils";-test("creates new link", async (ctx) => {- const h = new IntegrationHarness(ctx);+const domain = "dub.sh";++describe("create links with", async () => {+ const h = new IntegrationHarness();const { workspace, apiKey, user } = await h.init();const http = new HttpClient({@@ -16,68 +18,94 @@ test("creates new link", async (ctx) => {},});- const domain = "dub.sh";- const url = "https://github.com/dubinc";-- const { status, data: link } = await http.post<Link>({- path: "/links",- query: { workspaceId: workspace.workspaceId },- body: {- url,- domain,- },+ afterAll(async () => {+ await h.teardown();});- expect(status).toEqual(200);- expect(link).toEqual({- ...expectedLink,- domain,- url,- userId: user.id,- projectId: workspace.id,- workspaceId: workspace.workspaceId,- shortLink: `https://${domain}/${link.key}`,- qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,- tags: [],- });-});+ test("default domain", async (ctx) => {+ const url = "https://github.com/dubinc";-test("creates new link with user defined key", async (ctx) => {- const h = new IntegrationHarness(ctx);- const { workspace, apiKey, user } = await h.init();+ const { status, data: link } = await http.post<Link>({+ path: "/links",+ query: { workspaceId: workspace.workspaceId },+ body: {+ url,+ },+ });- const http = new HttpClient({- baseUrl: h.baseUrl,- headers: {- Authorization: `Bearer ${apiKey.token}`,- },+ expect(status).toEqual(200);+ expect(link).toEqual({+ ...expectedLink,+ url,+ userId: user.id,+ projectId: workspace.id,+ workspaceId: workspace.workspaceId,+ shortLink: `https://${domain}/${link.key}`,+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,+ tags: [],+ });});- const url = "https://github.com/dubinc";- const domain = "dub.sh";- const key = nanoid(6);+ test("user defined key", async () => {+ const url = "https://github.com/dubinc";+ const key = nanoid(6);- const { status, data: link } = await http.post<Link>({- path: "/links",- query: { workspaceId: workspace.workspaceId },- body: {- url,- domain,+ const { status, data: link } = await http.post<Link>({+ path: "/links",+ query: { workspaceId: workspace.workspaceId },+ body: {+ url,+ key,+ },+ });++ expect(status).toEqual(200);+ expect(link).toEqual({+ ...expectedLink,key,- },+ url,+ userId: user.id,+ projectId: workspace.id,+ workspaceId: workspace.workspaceId,+ shortLink: `https://${domain}/${key}`,+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${key}?qr=1`,+ tags: [],+ });});- expect(status).toEqual(200);- expect(link).toEqual({- ...expectedLink,- key,- domain,- url,- userId: user.id,- projectId: workspace.id,- workspaceId: workspace.workspaceId,- shortLink: `https://${domain}/${key}`,- qrCode: `https://api.dub.co/qr?url=https://${domain}/${key}?qr=1`,- tags: [],+ test("utm builder", async (ctx) => {+ const url = "https://github.com/dubinc";++ const utm = {+ utm_source: "facebook",+ utm_medium: "social",+ utm_campaign: "summer",+ utm_term: "shoes",+ utm_content: "cta",+ };++ const { status, data: link } = await http.post<Link>({+ path: "/links",+ query: { workspaceId: workspace.workspaceId },+ body: {+ url,+ ...utm,+ },+ });++ console.log(link);++ expect(status).toEqual(200);+ expect(link).toEqual({+ ...expectedLink,+ ...utm,+ url,+ userId: user.id,+ projectId: workspace.id,+ workspaceId: workspace.workspaceId,+ shortLink: `https://${domain}/${link.key}`,+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,+ tags: [],+ });});});diff --git a/apps/web/tests/utils/integration.ts b/apps/web/tests/utils/integration.tsindex 316ae09b5c..3b7616e19a 100644--- a/apps/web/tests/utils/integration.ts+++ b/apps/web/tests/utils/integration.ts@@ -12,11 +12,11 @@ interface Resources {}export class IntegrationHarness {- private readonly ctx: TaskContext;+ private readonly ctx?: TaskContext;public resources: Resources;public baseUrl: string;- constructor(ctx: TaskContext) {+ constructor(ctx?: TaskContext) {const env = integrationTestEnv.parse(process.env);this.ctx = ctx;@@ -24,26 +24,8 @@ export class IntegrationHarness {}async init() {- this.resources = await this.seed();- return this.resources;- }-- async seed() {- this.ctx.onTestFinished(async () => {- await prisma.$transaction([- prisma.project.deleteMany({- where: {- users: {- some: {- userId: this.resources.user.id,- },- },- },- }),- prisma.user.delete({ where: { id: this.resources.user.id } }),- ]);-- await prisma.$disconnect();+ this.ctx?.onTestFinished(async () => {+ await this.teardown();});// Create a user@@ -88,12 +70,29 @@ export class IntegrationHarness {},});- const resource = {+ this.resources = {user,workspace: { ...workspace, workspaceId: `ws_${workspace.id}` },apiKey: { ...apiKey, token },} as const;- return resource;+ return this.resources;+ }++ public async teardown() {+ await prisma.$transaction([+ prisma.project.deleteMany({+ where: {+ users: {+ some: {+ userId: this.resources.user.id,+ },+ },+ },+ }),+ prisma.user.delete({ where: { id: this.resources.user.id } }),+ ]);++ await prisma.$disconnect();}}diff --git a/apps/web/tests/utils/schema.ts b/apps/web/tests/utils/schema.tsindex 751ff97ae8..7e67664040 100644--- a/apps/web/tests/utils/schema.ts+++ b/apps/web/tests/utils/schema.ts@@ -4,6 +4,7 @@ import { expect } from "vitest";export const expectedLink: Partial<Link> = {id: expect.any(String),key: expect.any(String),+ domain: "dub.sh",archived: false,expiresAt: null,password: null,