Skip to content

Commit 56de0bb

Browse files
committed
test: Add test for missing host/Electron behavior
1 parent 8474011 commit 56de0bb

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { describe, test, expect, vi, beforeEach, afterEach } from "vitest";
2+
import { trackPageview } from "../track";
3+
import * as requestModule from "../request";
4+
import { Client } from "../client";
5+
6+
describe("trackPageview", () => {
7+
// Mock the makeRequest function
8+
const makeRequestMock = vi.fn();
9+
10+
beforeEach(() => {
11+
// Mock the makeRequest function
12+
vi.spyOn(requestModule, "makeRequest").mockImplementation(
13+
makeRequestMock,
14+
);
15+
16+
// Reset mocks between tests
17+
makeRequestMock.mockReset();
18+
19+
// Mock window.location
20+
Object.defineProperty(window, "location", {
21+
writable: true,
22+
value: {
23+
pathname: "/test-path",
24+
search: "?test=true",
25+
host: "example.com",
26+
},
27+
});
28+
29+
// Mock navigator.userAgent
30+
Object.defineProperty(navigator, "userAgent", {
31+
writable: true,
32+
value: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
33+
});
34+
35+
// Mock document.referrer
36+
Object.defineProperty(document, "referrer", {
37+
writable: true,
38+
value: "",
39+
});
40+
41+
// Mock document.querySelector for canonical URL
42+
vi.spyOn(document, "querySelector").mockReturnValue(null);
43+
});
44+
45+
afterEach(() => {
46+
vi.restoreAllMocks();
47+
});
48+
49+
test("should make a request when host is not empty", () => {
50+
const client = new Client({
51+
siteId: "test-site",
52+
reporterUrl: "https://example.com/collect",
53+
autoTrackPageviews: false,
54+
});
55+
56+
trackPageview(client);
57+
58+
expect(makeRequestMock).toHaveBeenCalledTimes(1);
59+
expect(makeRequestMock).toHaveBeenCalledWith(
60+
"https://example.com/collect",
61+
expect.objectContaining({
62+
p: "/test-path",
63+
h: "http://localhost",
64+
r: "",
65+
sid: "test-site",
66+
}),
67+
);
68+
});
69+
70+
test("should exit early when host is empty and not in Electron", () => {
71+
// Mock empty host (file:/// URI)
72+
Object.defineProperty(window, "location", {
73+
writable: true,
74+
value: {
75+
pathname: "/test-path",
76+
search: "?test=true",
77+
host: "",
78+
},
79+
});
80+
81+
// Mock non-Electron user agent
82+
Object.defineProperty(navigator, "userAgent", {
83+
writable: true,
84+
value: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
85+
});
86+
87+
const client = new Client({
88+
siteId: "test-site",
89+
reporterUrl: "https://example.com/collect",
90+
autoTrackPageviews: false,
91+
});
92+
93+
trackPageview(client);
94+
95+
// Verify that makeRequest was not called
96+
expect(makeRequestMock).not.toHaveBeenCalled();
97+
});
98+
99+
test("should make a request when host is empty but in Electron", () => {
100+
// Mock empty host (file:/// URI)
101+
Object.defineProperty(window, "location", {
102+
writable: true,
103+
value: {
104+
pathname: "/test-path",
105+
search: "?test=true",
106+
host: "",
107+
},
108+
});
109+
110+
// Mock Electron user agent
111+
Object.defineProperty(navigator, "userAgent", {
112+
writable: true,
113+
value: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Electron/15.0.0",
114+
});
115+
116+
const client = new Client({
117+
siteId: "test-site",
118+
reporterUrl: "https://example.com/collect",
119+
autoTrackPageviews: false,
120+
});
121+
122+
trackPageview(client);
123+
124+
// Verify that makeRequest was called
125+
expect(makeRequestMock).toHaveBeenCalledTimes(1);
126+
});
127+
});

packages/tracker/src/lib/track.ts

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export function trackPageview(client: Client, opts: TrackPageviewOpts = {}) {
5252
const canonical = getCanonicalUrl();
5353
const location = canonical ?? window.location;
5454

55+
// if host is empty, we're probably loading a file:/// URI
56+
// -- exit early if this is not an Electron app
5557
if (location.host === "" && navigator.userAgent.indexOf("Electron") < 0) {
5658
return;
5759
}

0 commit comments

Comments
 (0)