-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpageview.test.js
More file actions
67 lines (55 loc) · 2 KB
/
pageview.test.js
File metadata and controls
67 lines (55 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { expect } = require("chai");
const { createDOM } = require("./helpers/dom");
describe("pageview", function () {
it("sends pageview, event and beacon requests", function (done) {
const dom = createDOM({ navigationType: "reload" });
dom.window.sa_event("unit_test");
if (!("onpagehide" in dom.window)) {
dom.window.document.hidden = true;
dom.window.document.dispatchEvent(
new dom.window.Event("visibilitychange")
);
} else {
dom.window.dispatchEvent(new dom.window.Event("pagehide"));
}
setTimeout(() => {
const gif = dom.sent.find(
(r) => r.type === "image" && /simple\.gif/.test(r.url)
);
const eventReq = dom.sent.find(
(r) => r.type === "image" && /event=unit_test/.test(r.url)
);
const beacon = dom.sent.find((r) => r.type === "beacon");
expect(gif, "pageview gif request").to.exist;
expect(eventReq, "event gif request").to.exist;
expect(beacon, "append beacon request").to.exist;
expect(beacon.url).to.match(/\/append$/);
expect(beacon.data).to.include('"type":"append"');
done();
}, 10);
});
it("falls back to pixel when sendBeacon fails", function (done) {
const dom = createDOM({ navigationType: "reload" });
dom.window.navigator.sendBeacon = function () {
throw new TypeError("Illegal invocation");
};
dom.window.sa_event("unit_test");
if (!("onpagehide" in dom.window)) {
dom.window.document.hidden = true;
dom.window.document.dispatchEvent(
new dom.window.Event("visibilitychange")
);
} else {
dom.window.dispatchEvent(new dom.window.Event("pagehide"));
}
setTimeout(() => {
const beacon = dom.sent.find((r) => r.type === "beacon");
const appendGif = dom.sent.find(
(r) => r.type === "image" && /type=append/.test(r.url)
);
expect(beacon, "append beacon request").to.not.exist;
expect(appendGif, "append gif request").to.exist;
done();
}, 10);
});
});