-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds End-to-end tests to the SDK. Closes: https://topsort.atlassian.net/browse/API-922
- Loading branch information
1 parent
1d13410
commit 6668565
Showing
16 changed files
with
302 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SERVER_PORT= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
.env | ||
|
||
coverage/ | ||
dist/ | ||
node_modules/ | ||
test-results/ | ||
test-results.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
[test] | ||
coverage = true | ||
root = "./test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { expect, test } from "@playwright/test"; | ||
import { apis, baseURL } from "../src/constants/apis.constant"; | ||
import { playwrightConstants } from "./config"; | ||
|
||
test.describe("Create Auction via Topsort SDK", () => { | ||
test("should create an auction successfully", async ({ page }) => { | ||
const mockAPIResponse = { | ||
results: [ | ||
{ | ||
resultType: "listings", | ||
winners: [], | ||
error: false, | ||
}, | ||
{ | ||
resultType: "banners", | ||
winners: [], | ||
error: false, | ||
}, | ||
], | ||
}; | ||
|
||
await page.route(`${baseURL}/${apis.auctions}`, async (route) => { | ||
await route.fulfill({ json: mockAPIResponse }); | ||
}); | ||
|
||
await page.goto(playwrightConstants.host); | ||
const result = await page.evaluate(() => { | ||
const config = { | ||
apiKey: "rando-api-key", | ||
}; | ||
|
||
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(mockAPIResponse); | ||
}); | ||
|
||
test("should fail to call with missing apiKey", async ({ page }) => { | ||
const expectedError = { status: 401, statusText: "API Key is required.", body: {} }; | ||
await page.goto(playwrightConstants.host); | ||
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const playwrightConstants = { | ||
host: `http://localhost:${process.env.SERVER_PORT || 8080}/index.html`, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { expect, test } from "@playwright/test"; | ||
import { apis, baseURL } from "../src/constants/apis.constant"; | ||
import { playwrightConstants } from "./config"; | ||
|
||
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(playwrightConstants.host); | ||
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(playwrightConstants.host); | ||
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Topsort SDK Test Application</title> | ||
<script src="./index.global.js"></script> | ||
</head> | ||
<body> | ||
<h1>Test Topsort.js Integration</h1> | ||
<script> | ||
window.sdk = { | ||
createAuction: async (config, auctionDetails) => { | ||
const result = await Topsort.createAuction( | ||
config, | ||
auctionDetails | ||
).catch((error) => error); | ||
return result; | ||
}, | ||
reportEvent: async (config, eventDetails) => { | ||
const result = await Topsort.reportEvent( | ||
config, | ||
eventDetails | ||
).catch((error) => error); | ||
return result; | ||
}, | ||
}; | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { file } from "bun"; | ||
|
||
const PORT = process.env.SERVER_PORT || 8080; | ||
|
||
Bun.serve({ | ||
fetch(req) { | ||
const url = new URL(req.url); | ||
const pathname = url.pathname === "/" ? "/index.html" : url.pathname; | ||
const filePath = `./dist${pathname}`; | ||
|
||
try { | ||
if (filePath.endsWith(".ico")) { | ||
return new Response("", { status: 204 }); | ||
} | ||
|
||
return new Response(file(filePath)); | ||
} catch (e) { | ||
return new Response("Not Found", { status: 404 }); | ||
} | ||
}, | ||
port: PORT, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
interface Window { | ||
sdk: { | ||
createAuction: (a, b) => unknown; | ||
reportEvent: (a, b) => unknown; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { defineConfig, devices } from "@playwright/test"; | ||
|
||
export default defineConfig({ | ||
testDir: "./e2e", | ||
timeout: 30000, | ||
retries: 0, | ||
reporter: [["list"], ["json", { outputFile: "test-results.json" }]], | ||
use: { | ||
trace: "on-first-retry", | ||
}, | ||
projects: [ | ||
{ | ||
name: "chromium", | ||
use: { ...devices["Desktop Chrome"] }, | ||
}, | ||
{ | ||
name: "firefox", | ||
use: { ...devices["Desktop Firefox"] }, | ||
}, | ||
{ | ||
name: "webkit", | ||
use: { ...devices["Desktop Safari"] }, | ||
}, | ||
], | ||
webServer: { | ||
command: "bun run serve:e2e", | ||
reuseExistingServer: !process.env.CI, | ||
stdout: "ignore", | ||
stderr: "pipe", | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters