@@ -19,6 +19,18 @@ import {
1919 type Profile ,
2020} from "api/v0"
2121import OnboardingPage from "./OnboardingPage"
22+ import { usePostHog } from "posthog-js/react"
23+ import { PostHogEvents } from "@/common/constants"
24+
25+ jest . mock ( "posthog-js/react" , ( ) => ( {
26+ ...jest . requireActual ( "posthog-js/react" ) ,
27+ usePostHog : jest . fn ( ) ,
28+ } ) )
29+ const mockCapture = jest . fn ( )
30+ jest . mocked ( usePostHog ) . mockReturnValue (
31+ // @ts -expect-error Not mocking all of posthog
32+ { capture : mockCapture } ,
33+ )
2234
2335jest . mock ( "next/navigation" , ( ) =>
2436 jest . requireActual ( "next-router-mock/navigation" ) ,
@@ -167,4 +179,41 @@ describe("OnboardingPage", () => {
167179
168180 expect ( mockRouter . asPath ) . toEqual ( "/search?resource=184" )
169181 } )
182+
183+ describe ( "PostHog tracking" , ( ) => {
184+ beforeEach ( ( ) => {
185+ process . env . NEXT_PUBLIC_POSTHOG_API_KEY = "test-key"
186+ mockCapture . mockReset ( )
187+ } )
188+
189+ afterEach ( ( ) => {
190+ delete process . env . NEXT_PUBLIC_POSTHOG_API_KEY
191+ } )
192+
193+ it ( "fires cta_clicked with label 'Next' and step when Next is clicked" , async ( ) => {
194+ await setupAndProgressToStep ( 0 )
195+ await user . click ( await findNextButton ( ) )
196+ expect ( mockCapture ) . toHaveBeenCalledWith (
197+ PostHogEvents . CallToActionClicked ,
198+ { label : "Next" , step : 1 , location : "onboarding" } ,
199+ )
200+ } )
201+
202+ it ( "fires cta_clicked with label 'Finish' and step when Finish is clicked" , async ( ) => {
203+ await setupAndProgressToStep ( STEPS_DATA . length - 1 )
204+ mockCapture . mockClear ( )
205+ await user . click ( await findFinishButton ( ) )
206+ expect ( mockCapture ) . toHaveBeenCalledWith (
207+ PostHogEvents . CallToActionClicked ,
208+ { label : "Finish" , step : STEPS_DATA . length , location : "onboarding" } ,
209+ )
210+ } )
211+
212+ it ( "does not fire cta_clicked when NEXT_PUBLIC_POSTHOG_API_KEY is not set" , async ( ) => {
213+ delete process . env . NEXT_PUBLIC_POSTHOG_API_KEY
214+ await setupAndProgressToStep ( 0 )
215+ await user . click ( await findNextButton ( ) )
216+ expect ( mockCapture ) . not . toHaveBeenCalled ( )
217+ } )
218+ } )
170219} )
0 commit comments