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.

Improve the tests

Kiran K19287de1f4f3
diff --git a/apps/web/tests/links/update-link.test.ts b/apps/web/tests/links/update-link.test.ts
index d500c5051c..1b044f34c2 100644
--- a/apps/web/tests/links/update-link.test.ts
+++ b/apps/web/tests/links/update-link.test.ts
@@ -1,5 +1,5 @@
import { Link } from "@prisma/client";
-import { describe, expect, test } from "vitest";
+import { afterAll, describe, expect, test } from "vitest";
import { randomId } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
import { link } from "../utils/resource";
@@ -7,57 +7,62 @@ import { expectedLink } from "../utils/schema";
const { domain, url } = link;
-describe("PUT /links/{linkId}", async () => {
+describe.sequential("PUT /links/{linkId}", async () => {
const h = new IntegrationHarness();
const { workspace, http, user } = await h.init();
const { workspaceId } = workspace;
- const projectId = workspaceId.replace("ws_", "");
+ const externalId = randomId();
- test("update link", async () => {
- // Update the link with new data
- const { data: link } = await http.post<Link>({
- path: "/links",
- query: { workspaceId },
- body: {
- url,
- domain,
- },
- });
+ const { data: link } = await http.post<Link>({
+ path: "/links",
+ query: { workspaceId },
+ body: {
+ url,
+ domain,
+ externalId,
+ },
+ });
- const newLink: Partial<Link> = {
- key: randomId(),
- url: "https://github.com/dubinc/dub",
- title: "Dub Inc",
- description: "Open-source link management infrastructure.",
- publicStats: true,
- comments: "This is a comment.",
- expiresAt: new Date("2030-04-16T17:00:00.000Z"),
- expiredUrl: "https://github.com/expired",
- password: "link-password",
- ios: "https://apps.apple.com/app/1611158928",
- android:
- "https://play.google.com/store/apps/details?id=com.disney.disneyplus",
- geo: {
- AF: `${url}/AF`,
- },
- };
+ const toUpdate: Partial<Link> = {
+ key: randomId(),
+ url: "https://github.com/dubinc/dub",
+ title: "Dub Inc",
+ description: "Open-source link management infrastructure.",
+ publicStats: true,
+ comments: "This is a comment.",
+ expiresAt: new Date("2030-04-16T17:00:00.000Z"),
+ expiredUrl: "https://github.com/expired",
+ password: "link-password",
+ ios: "https://apps.apple.com/app/1611158928",
+ android:
+ "https://play.google.com/store/apps/details?id=com.disney.disneyplus",
+ geo: {
+ AF: `${url}/AF`,
+ },
+ };
+
+ afterAll(async () => {
+ await h.deleteLink(link.id);
+ });
+ test("update link", async () => {
const { data: updatedLink } = await http.put<Link>({
path: `/links/${link.id}`,
query: { workspaceId },
- body: newLink,
+ body: { ...toUpdate },
});
expect(updatedLink).toStrictEqual({
...expectedLink,
- ...newLink,
- expiresAt: "2030-04-16T17:00:00.000Z",
+ ...toUpdate,
domain,
- projectId,
workspaceId,
+ externalId,
userId: user.id,
- shortLink: `https://${domain}/${newLink.key}`,
- qrCode: `https://api.dub.co/qr?url=https://${domain}/${newLink.key}?qr=1`,
+ expiresAt: "2030-04-16T17:00:00.000Z",
+ projectId: workspaceId.replace("ws_", ""),
+ shortLink: `https://${domain}/${toUpdate.key}`,
+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${toUpdate.key}?qr=1`,
tags: [],
});
@@ -67,27 +72,20 @@ describe("PUT /links/{linkId}", async () => {
query: { workspaceId },
});
- expect({
- ...fetchedLink,
- workspaceId,
- shortLink: `https://${domain}/${newLink.key}`,
- qrCode: `https://api.dub.co/qr?url=https://${domain}/${newLink.key}?qr=1`,
- }).toStrictEqual(updatedLink);
-
- await h.deleteLink(link.id);
+ expect(fetchedLink).toStrictEqual({
+ ...expectedLink,
+ ...toUpdate,
+ domain,
+ externalId,
+ userId: user.id,
+ expiresAt: "2030-04-16T17:00:00.000Z",
+ projectId: workspaceId.replace("ws_", ""),
+ tags: [],
+ });
});
// Archive the link
test("archive link", async () => {
- const { data: link } = await http.post<Link>({
- path: "/links",
- query: { workspaceId },
- body: {
- url,
- domain,
- },
- });
-
const { status, data: updatedLink } = await http.put<Link>({
path: `/links/${link.id}`,
query: { workspaceId },
@@ -99,14 +97,16 @@ describe("PUT /links/{linkId}", async () => {
expect(status).toEqual(200);
expect(updatedLink).toStrictEqual({
...expectedLink,
- archived: true,
+ ...toUpdate,
domain,
- url: link.url,
- userId: user.id,
- projectId,
workspaceId,
- shortLink: `https://${domain}/${link.key}`,
- qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,
+ externalId,
+ archived: true,
+ userId: user.id,
+ expiresAt: "2030-04-16T17:00:00.000Z",
+ projectId: workspaceId.replace("ws_", ""),
+ shortLink: `https://${domain}/${toUpdate.key}`,
+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${toUpdate.key}?qr=1`,
tags: [],
});
@@ -117,21 +117,10 @@ describe("PUT /links/{linkId}", async () => {
});
expect(archivedLink.archived).toEqual(true);
-
- await h.deleteLink(link.id);
});
// Unarchive the link
test("unarchive link", async () => {
- const { data: link } = await http.post<Link>({
- path: "/links",
- query: { workspaceId },
- body: {
- url,
- domain,
- },
- });
-
const { status, data: updatedLink } = await http.put<Link>({
path: `/links/${link.id}`,
query: { workspaceId },
@@ -143,14 +132,16 @@ describe("PUT /links/{linkId}", async () => {
expect(status).toEqual(200);
expect(updatedLink).toStrictEqual({
...expectedLink,
- archived: false,
+ ...toUpdate,
domain,
- url: link.url,
- userId: user.id,
- projectId,
workspaceId,
- shortLink: `https://${domain}/${link.key}`,
- qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,
+ externalId,
+ archived: false,
+ userId: user.id,
+ expiresAt: "2030-04-16T17:00:00.000Z",
+ projectId: workspaceId.replace("ws_", ""),
+ shortLink: `https://${domain}/${toUpdate.key}`,
+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${toUpdate.key}?qr=1`,
tags: [],
});
@@ -161,7 +152,41 @@ describe("PUT /links/{linkId}", async () => {
});
expect(unarchivedLink.archived).toEqual(false);
+ });
- await h.deleteLink(link.id);
+ // Update the link using externalId
+ test("update link using externalId", async () => {
+ const { status, data: updatedLink } = await http.put<Link>({
+ path: `/links/ext_${externalId}`,
+ query: { workspaceId },
+ body: {
+ url: "https://github.com/dubinc",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(updatedLink).toStrictEqual({
+ ...expectedLink,
+ ...toUpdate,
+ domain,
+ workspaceId,
+ externalId,
+ archived: false,
+ userId: user.id,
+ url: "https://github.com/dubinc",
+ expiresAt: "2030-04-16T17:00:00.000Z",
+ projectId: workspaceId.replace("ws_", ""),
+ shortLink: `https://${domain}/${toUpdate.key}`,
+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${toUpdate.key}?qr=1`,
+ tags: [],
+ });
+
+ // Fetch the link
+ const { data: linkUpdated } = await http.get<Link>({
+ path: `/links/ext_${externalId}`,
+ query: { workspaceId },
+ });
+
+ expect(linkUpdated.url).toEqual("https://github.com/dubinc");
});
});