Skip to content

Commit f72c082

Browse files
committed
test(sessions): mock feature flag in ContextUsageIndicator test
Gating the cost readout behind useFeatureFlag made the component call useService(FEATURE_FLAGS), which throws in the test's provider-less render. Mock the hook (matching the pattern used across the UI test suite) and add a case covering the cost suffix when the flag is on. Generated-By: PostHog Code Task-Id: f6d673c3-965e-4598-a4bd-663f2651a5a5
1 parent 9e64eac commit f72c082

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

packages/ui/src/features/sessions/components/ContextUsageIndicator.test.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import type { ContextUsage } from "@posthog/ui/features/sessions/hooks/useContextUsage";
22
import { Theme } from "@radix-ui/themes";
33
import { render, screen } from "@testing-library/react";
4-
import { describe, expect, it } from "vitest";
4+
import { beforeEach, describe, expect, it, vi } from "vitest";
55
import { ContextUsageIndicator } from "./ContextUsageIndicator";
66

7+
const flagState = vi.hoisted(() => ({ enabled: false }));
8+
vi.mock("@posthog/ui/features/feature-flags/useFeatureFlag", () => ({
9+
useFeatureFlag: () => flagState.enabled,
10+
}));
11+
12+
beforeEach(() => {
13+
flagState.enabled = false;
14+
});
15+
716
function usage(overrides?: Partial<ContextUsage>): ContextUsage {
817
return {
918
used: 50_000,
@@ -53,6 +62,18 @@ describe("ContextUsageIndicator", () => {
5362
).toBeInTheDocument();
5463
});
5564

65+
it("appends the estimated cost to the label when the flag is enabled", () => {
66+
flagState.enabled = true;
67+
render(
68+
<Theme>
69+
<ContextUsageIndicator
70+
usage={usage({ cost: { amount: 0.42, currency: "USD" } })}
71+
/>
72+
</Theme>,
73+
);
74+
expect(screen.getByText(/50K\/200K · 25% · \$0\.42/)).toBeInTheDocument();
75+
});
76+
5677
it("renders a finite stroke offset at 0% (no NaN/Infinity)", () => {
5778
const { container } = render(
5879
<Theme>

0 commit comments

Comments
 (0)