Skip to content

Commit fe70368

Browse files
committed
fix: update PostHogTelemetryClient tests to include context parameter
1 parent c1d4b9a commit fe70368

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

packages/telemetry/src/__tests__/PostHogTelemetryClient.test.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe("PostHogTelemetryClient", () => {
2626
}
2727

2828
let mockPostHogClient: any
29+
let mockContext: any
2930

3031
beforeEach(() => {
3132
vi.clearAllMocks()
@@ -35,9 +36,18 @@ describe("PostHogTelemetryClient", () => {
3536
optIn: vi.fn(),
3637
optOut: vi.fn(),
3738
shutdown: vi.fn().mockResolvedValue(undefined),
39+
flush: vi.fn().mockResolvedValue(undefined),
40+
on: vi.fn(),
3841
}
3942
;(PostHog as any).mockImplementation(() => mockPostHogClient)
4043

44+
// Mock VSCode extension context
45+
mockContext = {
46+
globalStorageUri: { fsPath: "/test/global/storage" },
47+
storageUri: { fsPath: "/test/workspace/storage" },
48+
extensionPath: "/test/extension",
49+
}
50+
4151
// @ts-expect-error - Accessing private static property for testing
4252
PostHogTelemetryClient._instance = undefined
4353
;(vscode.workspace.getConfiguration as any).mockReturnValue({
@@ -47,7 +57,7 @@ describe("PostHogTelemetryClient", () => {
4757

4858
describe("isEventCapturable", () => {
4959
it("should return true for events not in exclude list", () => {
50-
const client = new PostHogTelemetryClient()
60+
const client = new PostHogTelemetryClient(mockContext)
5161

5262
const isEventCapturable = getPrivateProperty<(eventName: TelemetryEventName) => boolean>(
5363
client,
@@ -59,7 +69,7 @@ describe("PostHogTelemetryClient", () => {
5969
})
6070

6171
it("should return false for events in exclude list", () => {
62-
const client = new PostHogTelemetryClient()
72+
const client = new PostHogTelemetryClient(mockContext)
6373

6474
const isEventCapturable = getPrivateProperty<(eventName: TelemetryEventName) => boolean>(
6575
client,
@@ -72,7 +82,7 @@ describe("PostHogTelemetryClient", () => {
7282

7383
describe("isPropertyCapturable", () => {
7484
it("should filter out git repository properties", () => {
75-
const client = new PostHogTelemetryClient()
85+
const client = new PostHogTelemetryClient(mockContext)
7686

7787
const isPropertyCapturable = getPrivateProperty<(propertyName: string) => boolean>(
7888
client,
@@ -95,7 +105,7 @@ describe("PostHogTelemetryClient", () => {
95105

96106
describe("getEventProperties", () => {
97107
it("should merge provider properties with event properties", async () => {
98-
const client = new PostHogTelemetryClient()
108+
const client = new PostHogTelemetryClient(mockContext)
99109

100110
const mockProvider: TelemetryPropertiesProvider = {
101111
getTelemetryProperties: vi.fn().mockResolvedValue({
@@ -136,7 +146,7 @@ describe("PostHogTelemetryClient", () => {
136146
})
137147

138148
it("should filter out git repository properties", async () => {
139-
const client = new PostHogTelemetryClient()
149+
const client = new PostHogTelemetryClient(mockContext)
140150

141151
const mockProvider: TelemetryPropertiesProvider = {
142152
getTelemetryProperties: vi.fn().mockResolvedValue({
@@ -184,7 +194,7 @@ describe("PostHogTelemetryClient", () => {
184194
})
185195

186196
it("should handle errors from provider gracefully", async () => {
187-
const client = new PostHogTelemetryClient()
197+
const client = new PostHogTelemetryClient(mockContext)
188198

189199
const mockProvider: TelemetryPropertiesProvider = {
190200
getTelemetryProperties: vi.fn().mockRejectedValue(new Error("Provider error")),
@@ -211,7 +221,7 @@ describe("PostHogTelemetryClient", () => {
211221
})
212222

213223
it("should return event properties when no provider is set", async () => {
214-
const client = new PostHogTelemetryClient()
224+
const client = new PostHogTelemetryClient(mockContext)
215225

216226
const getEventProperties = getPrivateProperty<
217227
(event: { event: TelemetryEventName; properties?: Record<string, any> }) => Promise<Record<string, any>>
@@ -228,7 +238,7 @@ describe("PostHogTelemetryClient", () => {
228238

229239
describe("capture", () => {
230240
it("should not capture events when telemetry is disabled", async () => {
231-
const client = new PostHogTelemetryClient()
241+
const client = new PostHogTelemetryClient(mockContext)
232242
client.updateTelemetryState(false)
233243

234244
await client.capture({
@@ -240,7 +250,7 @@ describe("PostHogTelemetryClient", () => {
240250
})
241251

242252
it("should not capture events that are not capturable", async () => {
243-
const client = new PostHogTelemetryClient()
253+
const client = new PostHogTelemetryClient(mockContext)
244254
client.updateTelemetryState(true)
245255

246256
await client.capture({
@@ -252,7 +262,7 @@ describe("PostHogTelemetryClient", () => {
252262
})
253263

254264
it("should capture events when telemetry is enabled and event is capturable", async () => {
255-
const client = new PostHogTelemetryClient()
265+
const client = new PostHogTelemetryClient(mockContext)
256266
client.updateTelemetryState(true)
257267

258268
const mockProvider: TelemetryPropertiesProvider = {
@@ -284,7 +294,7 @@ describe("PostHogTelemetryClient", () => {
284294
})
285295

286296
it("should filter out git repository properties when capturing events", async () => {
287-
const client = new PostHogTelemetryClient()
297+
const client = new PostHogTelemetryClient(mockContext)
288298
client.updateTelemetryState(true)
289299

290300
const mockProvider: TelemetryPropertiesProvider = {
@@ -328,7 +338,7 @@ describe("PostHogTelemetryClient", () => {
328338

329339
describe("updateTelemetryState", () => {
330340
it("should enable telemetry when user opts in and global telemetry is enabled", () => {
331-
const client = new PostHogTelemetryClient()
341+
const client = new PostHogTelemetryClient(mockContext)
332342

333343
;(vscode.workspace.getConfiguration as any).mockReturnValue({
334344
get: vi.fn().mockReturnValue("all"),
@@ -341,7 +351,7 @@ describe("PostHogTelemetryClient", () => {
341351
})
342352

343353
it("should disable telemetry when user opts out", () => {
344-
const client = new PostHogTelemetryClient()
354+
const client = new PostHogTelemetryClient(mockContext)
345355

346356
;(vscode.workspace.getConfiguration as any).mockReturnValue({
347357
get: vi.fn().mockReturnValue("all"),
@@ -354,7 +364,7 @@ describe("PostHogTelemetryClient", () => {
354364
})
355365

356366
it("should disable telemetry when global telemetry is disabled, regardless of user opt-in", () => {
357-
const client = new PostHogTelemetryClient()
367+
const client = new PostHogTelemetryClient(mockContext)
358368

359369
;(vscode.workspace.getConfiguration as any).mockReturnValue({
360370
get: vi.fn().mockReturnValue("off"),
@@ -368,7 +378,7 @@ describe("PostHogTelemetryClient", () => {
368378

369379
describe("shutdown", () => {
370380
it("should call shutdown on the PostHog client", async () => {
371-
const client = new PostHogTelemetryClient()
381+
const client = new PostHogTelemetryClient(mockContext)
372382
await client.shutdown()
373383
expect(mockPostHogClient.shutdown).toHaveBeenCalled()
374384
})

0 commit comments

Comments
 (0)