Skip to content

Commit

Permalink
feat: adding reportevent tests and negative tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barbmarcio committed Jul 19, 2024
1 parent c5c03fe commit cc9fb33
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ bun run test

### E2E Tests

To run the end-to-end tests, use the following command:
To run the end-to-end tests, make sure you have latest bundled file before running it. Use the following commands:

```bash
bun run build
bun run test:e2e
```

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ reportEvent(config, event)
400:
```json
{
"status": 204,
"status": 400,
"statusText": "No Content",
"body": {
"errCode": "bad_request",
Expand Down
38 changes: 37 additions & 1 deletion e2e/auctions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,48 @@ test.describe("Create Auction via Topsort SDK", () => {
],
};
if (typeof window.sdk.createAuction === "undefined") {
throw new Error("Global function `createAuctions` is not available.");
throw new Error("Global function `createAuction` is not available.");
}

return window.sdk.createAuction(config, auctionDetails);
});

expect(result).toEqual(mockAPIResponse);
});

test("should fail to call with missing apiKey", async ({ page }) => {
const expectedError = { status: 401, statusText: 'API Key is required.', body: {} }
await page.goto("http://localhost:8080/e2e");
const result = await page.evaluate(() => {
const config = {
apiKey: null
};

const auctionDetails = {
auctions: [
{
type: "listings",
slots: 3,
category: { id: "cat123" },
geoTargeting: { location: "US" },
},
{
type: "banners",
slots: 1,
device: "desktop",
slotId: "slot123",
category: { ids: ["cat1", "cat2"] },
geoTargeting: { location: "UK" },
},
],
};
if (typeof window.sdk.createAuction === "undefined") {
throw new Error("Global function `createAuction` is not available.");
}

return window.sdk.createAuction(config, auctionDetails);
});

expect(result).toEqual(expectedError);
});
});
76 changes: 76 additions & 0 deletions e2e/events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { expect, test } from "@playwright/test";
import { apis, baseURL } from "../src/constants/apis.constant";

test.describe("Report Events via Topsort SDK", () => {
test("should report an successfully", async ({ page }) => {
const mockAPIResponse = {
ok: true
}

await page.route(`${baseURL}/${apis.events}`, async (route) => {
await route.fulfill({ json: mockAPIResponse });
});

await page.goto("http://localhost:8080/e2e");
const result = await page.evaluate(() => {
const config = {
apiKey: "rando-api-key",
};

const event = {
impressions: [
{
resolvedBidId:
"ChAGaP5D2ex-UKEEBCOHwvDjEhABkF4FDAx0S5mMD2cOG0w9GhABkEnL2CB6qKIoqeItVgA_InsKd2h0dHBzOi8vd3d3LndlYmEuYmUvZnIvcHJvbW8uaHRtbD91dG1fc291cmNlPW15c2hvcGkmdXRtX21lZGl1bT1iYW5uZXJfMTI4MHg0MDAmdXRtX2NvbnRlbnQ9ZGlzcGxheSZ1dG1fY2FtcGFpZ249c29sZGVuEAU",
id: "1720706109.713344-53B92988-7A49-4679-B18E-465943B46149",
occurredAt: "2024-07-11T13:55:09Z",
opaqueUserId: "38e0a5ff-9f8a-4e80-8969-e5e3f01348e8",
placement: {
path: "/categories/sports",
},
},
],
};

if (typeof window.sdk.reportEvent === "undefined") {
throw new Error("Global function `reportEvent` is not available.");
}

return window.sdk.reportEvent(config, event);
});

expect(result).toEqual(mockAPIResponse);
});

test("should fail to call with missing apiKey", async ({ page }) => {
const expectedError = { status: 401, statusText: 'API Key is required.', body: {} }
await page.goto("http://localhost:8080/e2e");
const result = await page.evaluate(() => {
const config = {
apiKey: null
};

const event = {
impressions: [
{
resolvedBidId:
"ChAGaP5D2ex-UKEEBCOHwvDjEhABkF4FDAx0S5mMD2cOG0w9GhABkEnL2CB6qKIoqeItVgA_InsKd2h0dHBzOi8vd3d3LndlYmEuYmUvZnIvcHJvbW8uaHRtbD91dG1fc291cmNlPW15c2hvcGkmdXRtX21lZGl1bT1iYW5uZXJfMTI4MHg0MDAmdXRtX2NvbnRlbnQ9ZGlzcGxheSZ1dG1fY2FtcGFpZ249c29sZGVuEAU",
id: "1720706109.713344-53B92988-7A49-4679-B18E-465943B46149",
occurredAt: "2024-07-11T13:55:09Z",
opaqueUserId: "38e0a5ff-9f8a-4e80-8969-e5e3f01348e8",
placement: {
path: "/categories/sports",
},
},
],
};
if (typeof window.sdk.reportEvent === "undefined") {
throw new Error("Global function `reportEvent` is not available.");
}

return window.sdk.reportEvent(config, event);
});

expect(result).toEqual(expectedError);
});
});
7 changes: 7 additions & 0 deletions e2e/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ <h1>Test Topsort.js Integration</h1>
).catch((error) => error);
return result;
},
reportEvent: async (config, eventDetails) => {
const result = await Topsort.reportEvent(
config,
eventDetails
).catch((error) => error);
return result;
},
};
</script>
</body>
Expand Down
3 changes: 2 additions & 1 deletion global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
interface Window {
sdk: {
createAuction: (a, b) => void;
createAuction: (a, b) => unknown,
reportEvent: (a, b) => unknown,
};
}

0 comments on commit cc9fb33

Please sign in to comment.