Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

5.2 KB
Raw Blame
import { expectLogic } from 'kea-test-utils'

import {
    eventFilterParam,
    heatmapApiPath,
    heatmapDataLogic,
    isWithinBounds,
} from 'lib/components/heatmaps/heatmapDataLogic'
import { CommonFilters, HeatmapBoundsFilter } from 'lib/components/heatmaps/types'

import { initKeaTests } from '~/test/init'
import { toolbarConfigLogic } from '~/toolbar/shared/toolbarConfigLogic'
import { AppContext } from '~/types'

describe('heatmapDataLogic window resize', () => {
    const originalInnerWidth = window.innerWidth

    beforeEach(() => {
        jest.spyOn(global, 'fetch').mockImplementation(() =>
            Promise.resolve(new Response(JSON.stringify({ results: [] }), { status: 200 }))
        )
        initKeaTests()
        toolbarConfigLogic.build({ apiURL: 'http://localhost', accessToken: 'test-token' }).mount()
    })

    afterEach(() => {
        jest.restoreAllMocks()
        Object.defineProperty(window, 'innerWidth', { configurable: true, value: originalInnerWidth })
    })

    it.each([
        ['in-app', false],
        ['toolbar', true],
    ] as const)('%s context refetches on resize: %s', async (context, refetches) => {
        const logic = heatmapDataLogic({ context })
        logic.mount()
        await expectLogic(logic, () => logic.actions.setHref('https://example.com/pricing'))
            .toDispatchActions(['loadHeatmapSuccess'])
            .toFinishAllListeners()
        const heatmapRequests = jest.mocked(global.fetch).mock.calls.filter(([url]) => String(url).includes('heatmap'))
        expect(heatmapRequests).toHaveLength(1)
        expect(logic.values.rawHeatmap).toEqual({ results: [] })

        Object.defineProperty(window, 'innerWidth', { configurable: true, value: window.innerWidth - 200 })
        window.dispatchEvent(new Event('resize'))

        expect(logic.values.rawHeatmapLoading).toBe(refetches)
    })
})

describe('isWithinBounds', () => {
    const staticArea: HeatmapBoundsFilter = {
        areaFixed: false,
        bounds: { left: 100, right: 200, top: 1000, bottom: 2000 },
    }
    const fixedArea: HeatmapBoundsFilter = {
        areaFixed: true,
        bounds: { left: 100, right: 200, top: 10, bottom: 60 },
    }

    it.each([
        ['no filter keeps every point', { x: 0, y: 0, targetFixed: false }, null, true],
        ['a static point inside a static area', { x: 150, y: 1500, targetFixed: false }, staticArea, true],
        ['a static point outside a static area', { x: 150, y: 100, targetFixed: false }, staticArea, false],
        // points and areas in different coordinate spaces are excluded, not cross-compared
        ['a fixed point against a static area', { x: 150, y: 1500, targetFixed: true }, staticArea, false],
        ['a static point against a fixed area', { x: 150, y: 30, targetFixed: false }, fixedArea, false],
        ['a fixed point inside a fixed area', { x: 150, y: 30, targetFixed: true }, fixedArea, true],
        ['a fixed point outside a fixed area', { x: 150, y: 300, targetFixed: true }, fixedArea, false],
        ['a boundary point is inclusive', { x: 100, y: 1000, targetFixed: false }, staticArea, true],
    ] as const)('%s', (_name, point, filter, expected) => {
        expect(isWithinBounds(point, filter)).toBe(expected)
    })
})

describe('heatmapApiPath', () => {
    let priorAppContext: AppContext | undefined

    beforeEach(() => {
        priorAppContext = window.POSTHOG_APP_CONTEXT
    })

    afterEach(() => {
        window.POSTHOG_APP_CONTEXT = priorAppContext
    })

    it.each([
        // in-app requests must pin the team the page was loaded for, not the user's global current project
        ['in-app', 42, '', '/api/projects/42/heatmaps/'],
        ['in-app', 42, 'events/', '/api/projects/42/heatmaps/events/'],
        // the toolbar has no app context and keeps the legacy unscoped route
        ['toolbar', 42, '', '/api/heatmap/'],
        ['toolbar', 42, 'events/', '/api/heatmap/events/'],
        // without an app context team there is nothing to scope to, so fall back to the legacy route
        ['in-app', null, '', '/api/heatmap/'],
        ['in-app', null, 'events/', '/api/heatmap/events/'],
    ] as const)('context %s with team %s and endpoint %s resolves %s', (context, teamId, endpoint, expected) => {
        window.POSTHOG_APP_CONTEXT = (teamId === null
            ? undefined
            : { current_team: { id: teamId } }) as unknown as AppContext

        expect(heatmapApiPath(context, endpoint)).toBe(expected)
    })
})

describe('eventFilterParam', () => {
    it.each([
        ['no events', undefined, undefined],
        ['an empty list', [], undefined],
        // ActionFilter adds the row before the user picks an event, and the API rejects a null id.
        ['only rows without a picked event', [{ id: null }, { id: null }], undefined],
        ['a mix of picked and unpicked rows', [{ id: null }, { id: 'purchase' }], '[{"id":"purchase"}]'],
        [
            'the property filters a picked row carries',
            [{ id: 'purchase', properties: [{ type: 'event', key: 'plan', value: 'pro' }] }],
            '[{"id":"purchase","properties":[{"type":"event","key":"plan","value":"pro"}]}]',
        ],
    ] as const)('%s', (_name, events, expected) => {
        expect(eventFilterParam(events as CommonFilters['events'])).toBe(expected)
    })
})