Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit ef8ef8a

Browse files
committed
e2e basic run test
e2e/unit configs
1 parent e7a0b01 commit ef8ef8a

11 files changed

+105
-133
lines changed

jest-playwright.config.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
serverOptions: [
3+
{
4+
command: 'npm run bundle && npx serve -C -l 8000 dist/sdk',
5+
port: 8000,
6+
launchTimeout: 10000,
7+
},
8+
{
9+
command: 'npx serve -l 9000 test/fixtures',
10+
port: 9000,
11+
launchTimeout: 20000,
12+
},
13+
]
14+
}

jest.e2e.config.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@ module.exports = {
22
verbose: true,
33
preset: 'jest-playwright-preset',
44
testMatch: [
5-
'**/__tests__/**/*.+(ts|js)',
6-
'**/test/e2e/?(*.)+(spec|test).+(ts|js)',
5+
'**/test/e2e/**/?(*.)+(test).+(ts|js)',
76
],
87
transform: {
98
'^.+\\.ts$': 'ts-jest',
109
},
1110
testEnvironmentOptions: {
1211
'jest-playwright': {
13-
browsers: ['firefox'],
14-
exitOnPageError: false, // GitHub currently throws errors
15-
lauchOptions: {
16-
headless: true,
17-
},
12+
browsers: ['chromium', 'firefox'],
1813
},
1914
},
2015
};

jest.config.js renamed to jest.unit.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
module.exports = {
2+
verbose: true,
23
preset: 'ts-jest',
34
testEnvironment: 'jsdom',
5+
testMatch: [
6+
'**/test/unit/**/?(*.)+(test).+(ts|js)',
7+
],
48
automock: false,
59
setupFiles: ['./test/setupJest.ts'],
610
modulePaths: ['<rootDir>'],

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
"compile-esm": "tsc --module es6 --outDir dist/esm",
3535
"compile-cjs": "tsc --module commonjs --outDir dist/cjs",
3636
"build": "run-s clean compile",
37-
"bundle": "microbundle --format umd,esm --name edkt",
37+
"bundle": "microbundle --format umd,esm --name edkt -o dist/sdk/edgekit.min.js -i src/index.ts",
3838
"watch": "tsc -w",
39-
"test": "jest --verbose",
40-
"test:watch": "jest --watch --verbose",
41-
"test:coverage": "jest --coverage",
39+
"test": "npm run test:unit && npm run test:e2e",
40+
"test:unit": "jest --config jest.unit.config.js",
41+
"test:unit:watch": "jest --config jest.unit.config.js --watch",
42+
"test:unit:coverage": "jest --coverage",
4243
"test:e2e": "jest --config jest.e2e.config.js",
4344
"test:e2e:watch": "jest --config jest.e2e.config.js --watch",
4445
"lint": "eslint src test --ext js,ts",
@@ -51,6 +52,7 @@
5152
"eslint": "^7.3.0",
5253
"eslint-config-prettier": "^6.11.0",
5354
"eslint-plugin-prettier": "^3.1.4",
55+
"expect-playwright": "^0.3.1",
5456
"jest": "^26.1.0",
5557
"jest-fetch-mock": "^3.0.3",
5658
"jest-playwright-preset": "^1.4.4",

test/e2e/run.test.ts

+45-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,54 @@
11
/// <reference types="jest-playwright-preset" />
2-
/// <reference types="expect-playwright" />
32

4-
import { mockHtmlResponse } from '../helpers/network';
5-
import * as examplePage from '../fixtures/examplePage.json';
6-
import { edkt } from '../../src/index';
3+
import {
4+
makeAudienceDefinition,
5+
makeStringArrayQuery,
6+
} from '../helpers/audienceDefinitions';
7+
8+
type Store = {edkt_matched_audiences: string, edkt_page_views: string}
9+
10+
const getPageViewsFromStore = (store: Store) => JSON.parse(store['edkt_page_views'])
11+
const getMatchedAudiencesFromStore = (store: Store) => JSON.parse(store['edkt_matched_audiences'])
12+
const getLocalStorageFromPage = (): Promise<Store> => page.evaluate('localStorage')
713

814
describe('edgekit basic run behaviour', () => {
9-
const testUrl = 'https://example.com/';
15+
const testUrl = 'http://localhost:9000';
16+
17+
const sportAudience = makeAudienceDefinition({
18+
id: 'sport_id',
19+
occurrences: 1,
20+
definition: [makeStringArrayQuery(['sport'])],
21+
});
22+
23+
const sportPageFeatures = {
24+
keywords: {
25+
version: 1,
26+
value: ['sport'],
27+
}
28+
}
29+
30+
const runEdkt = async () =>
31+
await page.evaluate((params) => (<any>window).edkt.edkt.run(params), {
32+
audienceDefinitions: [sportAudience],
33+
pageFeatures: sportPageFeatures,
34+
omitGdprConsent: true,
35+
})
36+
37+
beforeAll(async () => {
38+
await page.goto(`${testUrl}?edktDebug=true`, { waitUntil: 'networkidle' });
39+
})
1040

11-
beforeEach(async () => {
12-
mockHtmlResponse(page, `${testUrl}?edktDebug=true`, examplePage);
41+
it('runs and adds pageView to store', async () => {
42+
await runEdkt()
43+
const store = await getLocalStorageFromPage()
44+
expect(getPageViewsFromStore(store)).toHaveLength(1)
45+
expect(getMatchedAudiencesFromStore(store)).toHaveLength(0)
1346
});
1447

15-
it('runs', async () => {
16-
await page.goto(`${testUrl}?edktDebug=true`, { waitUntil: 'load' });
17-
await page.evaluate(
18-
(params) => {
19-
console.log(localStorage);
20-
edkt.run(params);
21-
},
22-
{
23-
audienceDefinitions: [],
24-
pageFeatures: {},
25-
omitGdprConsent: true,
26-
}
27-
);
48+
it('adds another pageView and match audienceDefinition', async () => {
49+
await runEdkt()
50+
const store = await getLocalStorageFromPage()
51+
expect(getPageViewsFromStore(store)).toHaveLength(2)
52+
expect(getMatchedAudiencesFromStore(store)).toHaveLength(1)
2853
});
2954
});

test/fixtures/examplePage.json

-8
This file was deleted.

test/fixtures/index.html

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<html>
2+
<head>
3+
<script async>
4+
(function () {
5+
var edktInitializor = (window.edktInitializor =
6+
window.edktInitializor || {});
7+
if (!edktInitializor.invoked) {
8+
edktInitializor.invoked = true;
9+
edktInitializor.load = function (e) {
10+
var p = e ? e : 'sdk';
11+
var n = document.createElement('script');
12+
n.type = 'text/javascript';
13+
n.async = true;
14+
n.src = 'http://localhost:8000/edgekit.min.umd.js';
15+
var a = document.getElementsByTagName('script')[0];
16+
a.parentNode.insertBefore(n, a);
17+
};
18+
edktInitializor.load();
19+
}
20+
})();
21+
</script>
22+
</head>
23+
24+
<body>
25+
<h2>Edge | Jest Test</h2>
26+
</body>
27+
</html>

test/helpers/injectSdk.ts

-48
This file was deleted.

test/helpers/network.ts

-44
This file was deleted.

tsconfig.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"noFallthroughCasesInSwitch": true,
1919
"esModuleInterop": true,
2020
"target": "es5",
21-
"module": "es6",
22-
"resolveJsonModule": true
21+
"module": "es6"
2322
}
2423
}

0 commit comments

Comments
 (0)