|
| 1 | +import { |
| 2 | + Campaign, |
| 3 | + CampaignWritable, |
| 4 | + CampaignUpdatable, |
| 5 | + CmpScheduleType, |
| 6 | +} from "../models"; |
| 7 | +import { CampaignsApi } from "../api/campaigns-api"; |
| 8 | +import { CONFIG_FOR_INTEGRATION } from "./testFixtures"; |
| 9 | + |
| 10 | +describe("CampaignsApi", () => { |
| 11 | + jest.setTimeout(1000 * 60); |
| 12 | + |
| 13 | + it("Campaign API can be instantiated", () => { |
| 14 | + const campaignsApi = new CampaignsApi(CONFIG_FOR_INTEGRATION); |
| 15 | + expect(campaignsApi).toBeDefined(); |
| 16 | + expect(typeof campaignsApi).toEqual("object"); |
| 17 | + expect(campaignsApi).toBeInstanceOf(CampaignsApi); |
| 18 | + }); |
| 19 | + |
| 20 | + it("all individual Campaign functions exists", () => { |
| 21 | + const campaignsApi = new CampaignsApi(CONFIG_FOR_INTEGRATION); |
| 22 | + expect(campaignsApi.create).toBeDefined(); |
| 23 | + expect(typeof campaignsApi.create).toEqual("function"); |
| 24 | + |
| 25 | + expect(campaignsApi.list).toBeDefined(); |
| 26 | + expect(typeof campaignsApi.list).toEqual("function"); |
| 27 | + |
| 28 | + expect(campaignsApi.get).toBeDefined(); |
| 29 | + expect(typeof campaignsApi.get).toEqual("function"); |
| 30 | + |
| 31 | + expect(campaignsApi.update).toBeDefined(); |
| 32 | + expect(typeof campaignsApi.update).toEqual("function"); |
| 33 | + |
| 34 | + expect(campaignsApi.delete).toBeDefined(); |
| 35 | + expect(typeof campaignsApi.delete).toEqual("function"); |
| 36 | + }); |
| 37 | + |
| 38 | + describe("performs single-Campaign operations", () => { |
| 39 | + const campaignWrite = new CampaignWritable({ |
| 40 | + name: "TS Integration Test Campaign", |
| 41 | + schedule_type: CmpScheduleType.Immediate, |
| 42 | + }); |
| 43 | + |
| 44 | + it("creates, updates, retrieves, and deletes a campaign", async () => { |
| 45 | + const campaignsApi = new CampaignsApi(CONFIG_FOR_INTEGRATION); |
| 46 | + // Create |
| 47 | + const createdCampaign = await campaignsApi.create(campaignWrite); |
| 48 | + expect(createdCampaign.id).toBeDefined(); |
| 49 | + |
| 50 | + // Get |
| 51 | + const retrievedCampaign = await campaignsApi.get( |
| 52 | + createdCampaign.id as string |
| 53 | + ); |
| 54 | + expect(retrievedCampaign).toBeDefined(); |
| 55 | + expect(retrievedCampaign.id).toEqual(createdCampaign.id); |
| 56 | + |
| 57 | + // Update |
| 58 | + const updates = new CampaignUpdatable({ |
| 59 | + description: "TS Integration Test Updated Campaign", |
| 60 | + }); |
| 61 | + const updatedCampaign = await campaignsApi.update( |
| 62 | + retrievedCampaign.id as string, |
| 63 | + updates |
| 64 | + ); |
| 65 | + expect(updatedCampaign).toBeDefined(); |
| 66 | + expect(updatedCampaign.description).toEqual( |
| 67 | + "TS Integration Test Updated Campaign" |
| 68 | + ); |
| 69 | + |
| 70 | + // Delete |
| 71 | + const deletedCampaign = await campaignsApi.delete( |
| 72 | + updatedCampaign.id as string |
| 73 | + ); |
| 74 | + expect(deletedCampaign.deleted).toBeTruthy(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe("list campaigns", () => { |
| 79 | + let createdCampaigns: Campaign[] = []; |
| 80 | + |
| 81 | + beforeAll(async () => { |
| 82 | + // ensure there are at least 3 campaigns present, to test pagination |
| 83 | + const campaign1 = new CampaignWritable({ |
| 84 | + name: "TS Integration Test Campaign 1", |
| 85 | + schedule_type: CmpScheduleType.Immediate, |
| 86 | + }); |
| 87 | + const campaign2 = new CampaignWritable( |
| 88 | + Object.assign({}, campaign1, { |
| 89 | + name: "TS Integration Test Campaign 2", |
| 90 | + schedule_type: CmpScheduleType.Immediate, |
| 91 | + }) |
| 92 | + ); |
| 93 | + const campaign3 = new CampaignWritable( |
| 94 | + Object.assign({}, campaign1, { |
| 95 | + name: "TS Integration Test Campaign 3", |
| 96 | + schedule_type: CmpScheduleType.Immediate, |
| 97 | + }) |
| 98 | + ); |
| 99 | + |
| 100 | + const campaignsApi = new CampaignsApi(CONFIG_FOR_INTEGRATION); |
| 101 | + await Promise.all([ |
| 102 | + campaignsApi.create(campaign1), |
| 103 | + campaignsApi.create(campaign2), |
| 104 | + campaignsApi.create(campaign3), |
| 105 | + ]) |
| 106 | + .then((creationResults) => { |
| 107 | + if (creationResults.length !== 3) { |
| 108 | + fail(); |
| 109 | + } |
| 110 | + createdCampaigns = createdCampaigns.concat(creationResults); |
| 111 | + }) |
| 112 | + .catch((err) => { |
| 113 | + fail(err); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + afterAll(async () => { |
| 118 | + const campaignsApi = new CampaignsApi(CONFIG_FOR_INTEGRATION); |
| 119 | + const deleteOperations: Promise<unknown>[] = []; |
| 120 | + for (const campaign of createdCampaigns) { |
| 121 | + deleteOperations.push(campaignsApi.delete(campaign.id as string)); |
| 122 | + } |
| 123 | + await Promise.all(deleteOperations); |
| 124 | + }); |
| 125 | + |
| 126 | + it("exists", () => { |
| 127 | + const campaignsApi = new CampaignsApi(CONFIG_FOR_INTEGRATION); |
| 128 | + expect(campaignsApi.list).toBeDefined(); |
| 129 | + expect(typeof campaignsApi.list).toEqual("function"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("lists campaigns", async () => { |
| 133 | + const response = await new CampaignsApi(CONFIG_FOR_INTEGRATION).list(); |
| 134 | + expect(response.data).toBeDefined(); |
| 135 | + expect(response.data?.length).toBeGreaterThan(0); |
| 136 | + }); |
| 137 | + |
| 138 | + it("lists campaigns given include param", async () => { |
| 139 | + const response = await new CampaignsApi(CONFIG_FOR_INTEGRATION).list( |
| 140 | + undefined, |
| 141 | + ["total_count"] |
| 142 | + ); |
| 143 | + expect(response.data).toBeDefined(); |
| 144 | + expect(response.total_count).toBeDefined(); |
| 145 | + }); |
| 146 | + |
| 147 | + it("lists campaigns given before or after params", async () => { |
| 148 | + const campaignsApi = new CampaignsApi(CONFIG_FOR_INTEGRATION); |
| 149 | + const response = await campaignsApi.list(); |
| 150 | + expect(response.next_url).toBeDefined(); |
| 151 | + const after: string = (response as { next_url: string }).next_url |
| 152 | + .slice( |
| 153 | + (response as { next_url: string }).next_url.lastIndexOf("after=") |
| 154 | + ) |
| 155 | + .split("=")[1]; |
| 156 | + |
| 157 | + const responseAfter = await campaignsApi.list( |
| 158 | + 10, |
| 159 | + undefined, |
| 160 | + undefined, |
| 161 | + after |
| 162 | + ); |
| 163 | + expect(responseAfter.data).toBeDefined(); |
| 164 | + expect(responseAfter.previous_url).toBeDefined(); |
| 165 | + expect(responseAfter.previous_url).not.toBeNull(); |
| 166 | + |
| 167 | + expect(responseAfter.data?.length).toBeGreaterThan(0); |
| 168 | + |
| 169 | + expect(responseAfter.previous_url).toBeDefined(); |
| 170 | + expect(responseAfter.previous_url).not.toBeNull(); |
| 171 | + const before: string = ( |
| 172 | + responseAfter as { previous_url: string } |
| 173 | + ).previous_url |
| 174 | + .slice( |
| 175 | + (responseAfter as { previous_url: string }).previous_url.lastIndexOf( |
| 176 | + "before=" |
| 177 | + ) |
| 178 | + ) |
| 179 | + .split("=")[1]; |
| 180 | + |
| 181 | + const responseBefore = await campaignsApi.list(10, undefined, before); |
| 182 | + expect(responseBefore.data).toBeDefined(); |
| 183 | + expect(responseBefore.data?.length).toBeGreaterThan(0); |
| 184 | + }); |
| 185 | + }); |
| 186 | +}); |
0 commit comments