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
213 changes: 213 additions & 0 deletions apps/web/tests/campaigns/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import { Campaign, CampaignList } from "@/lib/types";
import { updateCampaignSchema } from "@/lib/zod/schemas/campaigns";
import { E2E_PARTNER_GROUP } from "tests/utils/resource";
import { describe, expect, onTestFinished, test } from "vitest";
import { z } from "zod";
import { IntegrationHarness } from "../utils/integration";

const campaign: z.infer<typeof updateCampaignSchema> = {
name: "Updated Test Campaign",
subject: "Updated Test Subject",
triggerCondition: {
attribute: "totalConversions",
operator: "gte",
value: 50,
},
bodyJson: {
type: "doc",
content: [
{
type: "paragraph",
content: [
{
type: "text",
text: "Test campaign body",
},
],
},
],
},
};

const expectedCampaign: Partial<Campaign> = {
...campaign,
type: "transactional",
groups: [{ id: E2E_PARTNER_GROUP.id }],
createdAt: expect.any(String),
updatedAt: expect.any(String),
};

describe.sequential("/campaigns/**", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();

let campaignId = "";

test("POST /campaigns - create draft campaign", async () => {
const { status, data } = await http.post<{ id: string }>({
path: "/campaigns",
body: {
type: "transactional",
},
});

expect(status).toEqual(200);
expect(data).toMatchObject({
id: expect.any(String),
});

campaignId = data.id;
});

test("PATCH /campaigns/[campaignId] - update campaign content", async () => {
const { status, data: updatedCampaign } = await http.patch<Campaign>({
path: `/campaigns/${campaignId}`,
body: {
...campaign,
groupIds: [E2E_PARTNER_GROUP.id],
},
});

expect(status).toEqual(200);
expect(updatedCampaign).toStrictEqual({
...expectedCampaign,
id: campaignId,
status: "draft",
});
});

test("GET /campaigns/[campaignId] - make sure the draft campaign is created", async () => {
const { status, data: fetchedCampaign } = await http.get<Campaign>({
path: `/campaigns/${campaignId}`,
});

expect(status).toEqual(200);
expect(fetchedCampaign).toStrictEqual({
...expectedCampaign,
id: campaignId,
status: "draft",
});
});

test("PATCH /campaigns/[campaignId] - publish campaign", async () => {
const { status, data: publishedCampaign } = await http.patch<Campaign>({
path: `/campaigns/${campaignId}`,
body: {
status: "active",
},
});

expect(status).toEqual(200);
expect(publishedCampaign).toStrictEqual({
...expectedCampaign,
id: campaignId,
status: "active",
});
});

test("PATCH /campaigns/[campaignId] - pause campaign", async () => {
const { status, data: pausedCampaign } = await http.patch<Campaign>({
path: `/campaigns/${campaignId}`,
body: {
status: "paused",
},
});

expect(status).toEqual(200);
expect(pausedCampaign).toStrictEqual({
...expectedCampaign,
id: campaignId,
status: "paused",
});
});

test("PATCH /campaigns/[campaignId] - resume campaign", async () => {
const { status, data: resumedCampaign } = await http.patch<Campaign>({
path: `/campaigns/${campaignId}`,
body: {
status: "active",
},
});

expect(status).toEqual(200);
expect(resumedCampaign).toStrictEqual({
...expectedCampaign,
id: campaignId,
status: "active",
});
});

test("POST /campaigns/[campaignId]/duplicate - duplicate campaign", async () => {
const { status, data } = await http.post<{ id: string }>({
path: `/campaigns/${campaignId}/duplicate`,
});

expect(status).toEqual(200);
expect(data.id).toBeDefined();

onTestFinished(async () => {
await h.deleteCampaign(data.id);
});

const { data: duplicatedCampaign } = await http.get<Campaign>({
path: `/campaigns/${data.id}`,
});

expect(duplicatedCampaign).toStrictEqual({
...expectedCampaign,
id: data.id,
name: `${expectedCampaign.name} (copy)`,
status: "draft",
});
});

test("GET /campaigns - list campaigns", async () => {
const { status, data: campaigns } = await http.get<CampaignList[]>({
path: "/campaigns",
});

expect(status).toEqual(200);
expect(Array.isArray(campaigns)).toBe(true);
expect(campaigns.length).toBeGreaterThan(0);

const campaign = campaigns.find((c) => c.id === campaignId);

expect(campaign).toStrictEqual({
id: campaignId,
name: expectedCampaign.name,
type: expectedCampaign.type,
status: "active",
groups: [{ id: E2E_PARTNER_GROUP.id }],
delivered: 0,
sent: 0,
bounced: 0,
opened: 0,
createdAt: expect.any(String),
updatedAt: expect.any(String),
});
});

test("GET /campaigns/[campaignId] - get single campaign", async () => {
const { status, data: fetchedCampaign } = await http.get<Campaign>({
path: `/campaigns/${campaignId}`,
});

expect(status).toEqual(200);
expect(fetchedCampaign).toStrictEqual({
...expectedCampaign,
id: campaignId,
status: "active",
});
});

test("DELETE /campaigns/[campaignId] - delete campaign", async () => {
const { status, data } = await http.delete<{ id: string }>({
path: `/campaigns/${campaignId}`,
});

expect(status).toEqual(200);
expect(data).toStrictEqual({
id: campaignId,
});
});
});
9 changes: 9 additions & 0 deletions apps/web/tests/utils/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,13 @@ export class IntegrationHarness {
path: `/bounties/${id}`,
});
}

// Delete campaign
public async deleteCampaign(id: string) {
if (!id) return;

await this.http.delete({
path: `/campaigns/${id}`,
});
}
}