Skip to content

Commit 8845936

Browse files
committed
Add jsdom unit tests
1 parent 6e7ce19 commit 8845936

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ Just run `npm run watch` and every file will be validated and compiled on save.
2828

2929
The most important file of the repository is [`/src/default.js`](src/default.js)
3030

31+
## Unit tests
32+
33+
Run `npm run test:unit` to execute a small set of tests without BrowserStack.
34+
3135
## Device testing is sponsored by BrowserStack
3236

3337
We run our public script with more than [50 browsers on many different (real) devices](https://github.com/simpleanalytics/scripts/blob/main/test/helpers/get-browsers.js). We support Internet Explorer 9 (not sure who is still using that) and up. Including many mobile browsers and less common devices. We get amazing sponsorship from [BrowserStack](https://www.browserstack.com/). Thanks, BrowserStack!

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"pretest": "npm run build -- testing",
1111
"test": "node -r dotenv/config ./test/index.js",
1212
"posttest": "npm run build",
13-
"prettier": "prettier --check ."
13+
"prettier": "prettier --check .",
14+
"test:unit": "mocha ./test/unit"
1415
},
1516
"repository": {
1617
"type": "git",
@@ -37,6 +38,7 @@
3738
"request": "^2.88.0",
3839
"selenium-webdriver": "^4.3.1",
3940
"uglify-js": "^3.9.4",
40-
"uuid-validate": "0.0.3"
41+
"uuid-validate": "0.0.3",
42+
"jsdom": "^21.1.0"
4143
}
4244
}

test/unit/default.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const { JSDOM } = require("jsdom");
2+
const { readFileSync } = require("fs");
3+
const vm = require("vm");
4+
const { expect } = require("chai");
5+
6+
describe("default script", function () {
7+
it("sends pageview, event and beacon requests", function () {
8+
const dom = new JSDOM("<!doctype html><html><body></body></html>", {
9+
url: "https://example.com/",
10+
runScripts: "outside-only",
11+
});
12+
13+
const sent = [];
14+
dom.window.Image = function () {
15+
return {
16+
set src(url) {
17+
sent.push({ type: "image", url });
18+
},
19+
};
20+
};
21+
dom.window.navigator.sendBeacon = function (url, data) {
22+
sent.push({ type: "beacon", url, data });
23+
return true;
24+
};
25+
26+
const script = readFileSync("dist/latest/latest.dev.js", "utf8");
27+
vm.runInContext(script, dom.getInternalVMContext());
28+
29+
dom.window.sa_event("unit_test");
30+
31+
dom.window.document.hidden = true;
32+
dom.window.document.dispatchEvent(new dom.window.Event("visibilitychange"));
33+
34+
const gif = sent.find(
35+
(r) => r.type === "image" && /simple\.gif/.test(r.url)
36+
);
37+
const eventReq = sent.find(
38+
(r) => r.type === "image" && /event_unit_test/.test(r.url)
39+
);
40+
const beacon = sent.find((r) => r.type === "beacon");
41+
42+
expect(gif, "pageview gif request").to.exist;
43+
expect(eventReq, "event gif request").to.exist;
44+
expect(beacon, "append beacon request").to.exist;
45+
expect(beacon.url).to.match(/\/append$/);
46+
expect(beacon.data).to.include('"type":"append"');
47+
});
48+
});

0 commit comments

Comments
 (0)