|
2 | 2 |
|
3 | 3 | import React from "react" |
4 | 4 | import { render, screen, act, cleanup } from "@/utils/test-utils" |
| 5 | +import posthog from "posthog-js" |
5 | 6 |
|
6 | 7 | import AppWithProviders from "../App" |
7 | 8 |
|
| 9 | +// Mock posthog |
| 10 | +vi.mock("posthog-js", () => ({ |
| 11 | + default: { |
| 12 | + onFeatureFlags: vi.fn(), |
| 13 | + getFeatureFlag: vi.fn(), |
| 14 | + }, |
| 15 | +})) |
| 16 | + |
8 | 17 | vi.mock("@src/utils/vscode", () => ({ |
9 | 18 | vscode: { |
10 | 19 | postMessage: vi.fn(), |
@@ -189,6 +198,7 @@ describe("App", () => { |
189 | 198 | shouldShowAnnouncement: false, |
190 | 199 | experiments: {}, |
191 | 200 | language: "en", |
| 201 | + telemetrySetting: "enabled", |
192 | 202 | }) |
193 | 203 | }) |
194 | 204 |
|
@@ -338,4 +348,54 @@ describe("App", () => { |
338 | 348 | expect(chatView.getAttribute("data-hidden")).toBe("false") |
339 | 349 | expect(screen.queryByTestId("marketplace-view")).not.toBeInTheDocument() |
340 | 350 | }) |
| 351 | + |
| 352 | + describe("PostHog feature flag initialization", () => { |
| 353 | + it("waits for state hydration before checking feature flags", () => { |
| 354 | + mockUseExtensionState.mockReturnValue({ |
| 355 | + didHydrateState: false, |
| 356 | + showWelcome: false, |
| 357 | + shouldShowAnnouncement: false, |
| 358 | + experiments: {}, |
| 359 | + language: "en", |
| 360 | + telemetrySetting: "enabled", |
| 361 | + }) |
| 362 | + |
| 363 | + render(<AppWithProviders />) |
| 364 | + |
| 365 | + // PostHog feature flag check should not be called before hydration |
| 366 | + expect(posthog.onFeatureFlags).not.toHaveBeenCalled() |
| 367 | + }) |
| 368 | + |
| 369 | + it("checks feature flags after state hydration when telemetry is enabled", () => { |
| 370 | + mockUseExtensionState.mockReturnValue({ |
| 371 | + didHydrateState: true, |
| 372 | + showWelcome: false, |
| 373 | + shouldShowAnnouncement: false, |
| 374 | + experiments: {}, |
| 375 | + language: "en", |
| 376 | + telemetrySetting: "enabled", |
| 377 | + }) |
| 378 | + |
| 379 | + render(<AppWithProviders />) |
| 380 | + |
| 381 | + // PostHog feature flag check should be called after hydration |
| 382 | + expect(posthog.onFeatureFlags).toHaveBeenCalled() |
| 383 | + }) |
| 384 | + |
| 385 | + it("does not check feature flags when telemetry is disabled", () => { |
| 386 | + mockUseExtensionState.mockReturnValue({ |
| 387 | + didHydrateState: true, |
| 388 | + showWelcome: false, |
| 389 | + shouldShowAnnouncement: false, |
| 390 | + experiments: {}, |
| 391 | + language: "en", |
| 392 | + telemetrySetting: "disabled", |
| 393 | + }) |
| 394 | + |
| 395 | + render(<AppWithProviders />) |
| 396 | + |
| 397 | + // PostHog feature flag check should not be called when telemetry is disabled |
| 398 | + expect(posthog.onFeatureFlags).not.toHaveBeenCalled() |
| 399 | + }) |
| 400 | + }) |
341 | 401 | }) |
0 commit comments