Skip to content

Commit

Permalink
feat: initial revision for e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barbmarcio committed Jul 18, 2024
1 parent 7321da8 commit ba7486c
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
coverage/
dist/
node_modules/
test-results/
test-results.json
Binary file modified bun.lockb
Binary file not shown.
54 changes: 54 additions & 0 deletions e2e/auctions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { apis, baseURL } from '../src/constants/apis.constant';
import { test, expect } from '@playwright/test'

test.describe('Create Auction via Topsort.js', () => {
test('should create 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('http://localhost:8080/e2e/index.html');

await page.fill('input[name="apiKey"]', 'your-api-key');
await page.fill('input[name="auctionDetails"]', JSON.stringify({
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" },
},
],
}));

await page.click('button[type="submit"]');

const response = await page.locator('#response').textContent();
const jsonResponse = JSON.parse(response || '{}');
expect(jsonResponse).toEqual(mockAPIResponse)
expect(jsonResponse).toEqual(mockAPIResponse);
});
});
46 changes: 46 additions & 0 deletions e2e/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!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="../dist/index.global.js"></script>
</head>
<body>
<h1>Test Topsort.js Integration</h1>
<form id="auction-form">
<input type="text" name="apiKey" placeholder="API Key" required />
<input
type="text"
name="auctionDetails"
placeholder="Auction Details"
required
/>
<button type="submit">Create Auction</button>
</form>
<pre id="response"></pre>
<script>
document
.getElementById("auction-form")
.addEventListener("submit", async (event) => {
event.preventDefault();
const apiKey = event.target.apiKey.value;
const auctionDetails = JSON.parse(event.target.auctionDetails.value);
const config = { apiKey };
try {
const result = await Topsort.createAuction(
config,
auctionDetails
);
document.getElementById("response").textContent = JSON.stringify(
result,
null,
2
);
} catch (error) {
document.getElementById("response").textContent = error.toString();
}
});
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
],
"scripts": {
"build": "tsup",
"test:e2e": "playwright test",
"format": "biome check",
"format:fix": "biome check --write",
"prepare": "lefthook install"
},
"devDependencies": {
"@biomejs/biome": "1.8.3",
"@playwright/test": "^1.45.2",
"@types/bun": "1.1.6",
"http-server": "^14.1.1",
"lefthook": "1.7.2",
"msw": "2.3.1",
"tsup": "8.1.0",
Expand Down
31 changes: 31 additions & 0 deletions playwright.config.ts
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: 1,
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: 'http-server ./ -p 8080',
reuseExistingServer: !process.env.CI,
stdout: 'ignore',
stderr: 'pipe',
}
})
3 changes: 2 additions & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts"],
format: ["cjs", "esm"],
format: ["cjs", "esm", "iife"],
dts: true,
clean: true,
minify: true,
esbuildOptions(options) {
options.keepNames = true;
options.globalName = "Topsort"
},
});

0 comments on commit ba7486c

Please sign in to comment.