|
| 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 | +}); |
0 commit comments