mirror of
https://github.com/hanzoai/insights-js.git
synced 2026-10-30 18:34:19 +00:00
We own the ingestion server (hanzoai/insights: Django + rust/capture + nodejs plugin) AND this SDK, so there is no external wire contract to preserve. The server is already 100% posthog-free; this SDK was the last laggard. - $last_posthog_reset -> $last_insights_reset. This FIXES A LIVE MISMATCH: the server taxonomy (insights/taxonomy/taxonomy.py, insights/insightsql/ai.py) already expected the insights form while the SDK emitted the posthog form. - __PosthogExtensions__ -> __InsightsExtensions__; all PostHog/posthog/POSTHOG identifiers -> Insights/insights/INSIGHTS (src, tests, playground, playwright, scripts, rrweb subpackages, package.json keywords). - Hosts: DEFAULT_PLUGIN_HOST + DEFAULT_NUXT_HOST + the core host default pointed at PostHog's cloud (us.i.posthog.com) — a real bug. All -> insights.hanzo.ai. - Removed the us-assets/eu-assets CDN split in getRemoteConfig (PostHog-specific; Hanzo serves remote config from the same host) — it had degenerated to a no-op. - Fixed generate-docs writing to a stray references/@hanzo/ subdir. - Renamed the 3 posthog-named test files; dropped 58MB of stale reference snapshots named for upstream posthog versions we never shipped (they are generated output — references/ is now gitignored). Verified: typecheck errors 42 (HEAD) -> 18 (all pre-existing test-mock looseness); types package 0; every package.json valid with @hanzo/* names. Guard: grep -rioE 'posthog' packages ... | wc -l => 0
75 lines
2.6 KiB
TypeScript
75 lines
2.6 KiB
TypeScript
import React from 'react'
|
|
import { render } from '@testing-library/react'
|
|
import { InsightsPageView } from '../src/pages/InsightsPageView'
|
|
|
|
const mockCapture = jest.fn()
|
|
const mockUseInsights = jest.fn(() => ({ capture: mockCapture }))
|
|
jest.mock('@hanzo/insights-react', () => ({
|
|
useInsights: () => mockUseInsights(),
|
|
}))
|
|
|
|
let mockRouter = { asPath: '/initial', isReady: true }
|
|
jest.mock('next/router.js', () => ({
|
|
useRouter: () => mockRouter,
|
|
}))
|
|
|
|
describe('Pages InsightsPageView', () => {
|
|
beforeEach(() => {
|
|
mockCapture.mockClear()
|
|
mockUseInsights.mockClear()
|
|
mockRouter = { asPath: '/initial', isReady: true }
|
|
})
|
|
|
|
it('captures a $pageview event on mount', () => {
|
|
render(<InsightsPageView />)
|
|
expect(mockCapture).toHaveBeenCalledWith('$pageview', {
|
|
$current_url: 'http://localhost/initial',
|
|
})
|
|
})
|
|
|
|
it('includes query params and hash fragments from asPath', () => {
|
|
mockRouter = { asPath: '/search?q=hello&page=2#section', isReady: true }
|
|
render(<InsightsPageView />)
|
|
expect(mockCapture).toHaveBeenCalledWith('$pageview', {
|
|
$current_url: 'http://localhost/search?q=hello&page=2#section',
|
|
})
|
|
})
|
|
|
|
it('captures a new $pageview when asPath changes', () => {
|
|
const { rerender } = render(<InsightsPageView />)
|
|
expect(mockCapture).toHaveBeenCalledTimes(1)
|
|
|
|
mockRouter = { asPath: '/new-page', isReady: true }
|
|
rerender(<InsightsPageView />)
|
|
expect(mockCapture).toHaveBeenCalledTimes(2)
|
|
expect(mockCapture).toHaveBeenLastCalledWith('$pageview', {
|
|
$current_url: 'http://localhost/new-page',
|
|
})
|
|
})
|
|
|
|
it('does not capture if insights client is not available', () => {
|
|
mockUseInsights.mockReturnValueOnce(null)
|
|
render(<InsightsPageView />)
|
|
expect(mockCapture).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('does not capture if router is not ready', () => {
|
|
mockRouter = { asPath: '/initial', isReady: false }
|
|
render(<InsightsPageView />)
|
|
expect(mockCapture).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('captures pageview once router becomes ready', () => {
|
|
mockRouter = { asPath: '/initial', isReady: false }
|
|
const { rerender } = render(<InsightsPageView />)
|
|
expect(mockCapture).not.toHaveBeenCalled()
|
|
|
|
mockRouter = { asPath: '/initial', isReady: true }
|
|
rerender(<InsightsPageView />)
|
|
expect(mockCapture).toHaveBeenCalledTimes(1)
|
|
expect(mockCapture).toHaveBeenCalledWith('$pageview', {
|
|
$current_url: 'http://localhost/initial',
|
|
})
|
|
})
|
|
})
|