: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { expectLogic } from 'kea-test-utils'
import { heatmapDataLogic } from 'lib/components/heatmaps/heatmapDataLogic'
import { resumeKeaLoadersErrors, silenceKeaLoadersErrors } from '~/initKea'
import { initKeaTests } from '~/test/init'
import { currentPageLogic } from '~/toolbar/shared/stats/currentPageLogic'
import { toolbarApi } from '~/toolbar/shared/toolbarApi'
import { toolbarConfigLogic } from '~/toolbar/shared/toolbarConfigLogic'
import { toolbarPosthogJS } from '~/toolbar/shared/toolbarPosthogJS'
import { ElementsEventType } from '~/toolbar/shared/types'
import { TOOLBAR_ROOT_ATTRIBUTE } from '~/toolbar/shared/utils'
import {
LOAD_ALL_MAX_PAGES,
buildElementStatsProperties,
computeAreaCandidates,
dedupeByChainIdentity,
heatmapToolbarMenuLogic,
isInFixedContainer,
resolveAreaElement,
resolveAreaTarget,
stepAreaCandidate,
} from './heatmapToolbarMenuLogic'
function statsRow(overrides: Partial<ElementsEventType>): ElementsEventType {
return {
count: 1,
hash: null,
type: '$autocapture',
elements: [{ tag_name: 'button', attr_class: ['btn'], nth_child: 1, nth_of_type: 1, attributes: {} }],
...overrides,
} as ElementsEventType
}
describe('heatmapToolbarMenuLogic', () => {
// These suites intentionally reject requests to exercise error states; the
// failures are asserted via authStatus/values, so skip the global loader logging.
beforeAll(silenceKeaLoadersErrors)
afterAll(resumeKeaLoadersErrors)
describe('dedupeByChainIdentity', () => {
const cases = [
{
name: 'keeps distinct chains when a legacy server returns hash null',
events: [
statsRow({ elements: [{ tag_name: 'button', attributes: {} }] as ElementsEventType['elements'] }),
statsRow({ elements: [{ tag_name: 'a', attributes: {} }] as ElementsEventType['elements'] }),
],
expectedCount: 2,
},
{
name: 'drops a null-hash chain repeated across paginated pages, keeping the first occurrence',
events: [statsRow({ count: 10 }), statsRow({ count: 3 })],
expectedCount: 1,
},
{
name: 'keeps identical chains that differ by event type',
events: [statsRow({ type: '$autocapture' }), statsRow({ type: '$rageclick' })],
expectedCount: 2,
},
{
name: 'keeps distinct hashes even when attribute trimming made the chains serialize identically',
events: [statsRow({ hash: 'abc123' }), statsRow({ hash: 'def456' })],
expectedCount: 2,
},
{
name: 'drops a repeated hash across paginated pages, keeping the first occurrence',
events: [statsRow({ hash: 'abc123', count: 10 }), statsRow({ hash: 'abc123', count: 3 })],
expectedCount: 1,
},
]
it.each(cases)('$name', ({ events, expectedCount }) => {
expect(dedupeByChainIdentity(events)).toHaveLength(expectedCount)
})
it('keeps the first occurrence of a duplicated chain', () => {
const [kept] = dedupeByChainIdentity([statsRow({ count: 10 }), statsRow({ count: 3 })])
expect(kept.count).toBe(10)
})
})
describe('buildElementStatsProperties', () => {
it.each([
[
'an exact url filter when the wildcard href matches the href',
'https://example.com/page',
'https://example.com/page',
null,
[{ key: '$current_url', value: 'https://example.com/page', operator: 'exact', type: 'event' }],
],
[
'a regex url filter when the wildcard href differs',
'https://example.com/page/1',
'https://example.com/page/*',
null,
[
{
key: '$current_url',
value: '^https\\:\\/\\/example\\.com\\/page\\/.+$',
operator: 'regex',
type: 'event',
},
],
],
[
'an exact url filter on the normalized url when the wildcard href is a url without a wildcard',
'https://example.com/page',
'https://example.com',
null,
[{ key: '$current_url', value: 'https://example.com/', operator: 'exact', type: 'event' }],
],
[
'an element selector filter when an area is chosen',
'https://example.com/page',
'https://example.com/page',
'nav#main-nav',
[
{ key: '$current_url', value: 'https://example.com/page', operator: 'exact', type: 'event' },
{ key: 'selector', value: 'nav#main-nav', operator: 'exact', type: 'element' },
],
],
])('builds %s', (_name, href, wildcardHref, areaSelector, expected) => {
expect(buildElementStatsProperties(href, wildcardHref, areaSelector)).toEqual(expected)
})
})
describe('resolveAreaTarget', () => {
afterEach(() => {
document.body.innerHTML = ''
})
it.each([
['snaps to the nearest semantic container', '<nav id="n"><ul><li id="leaf">x</li></ul></nav>', 'leaf', 'n'],
['keeps a semantic container that is hovered directly', '<main id="m">x</main>', 'm', 'm'],
['falls back to the hovered element when no container wraps it', '<div><b id="b">x</b></div>', 'b', 'b'],
[
'snaps to a role-annotated container',
'<div role="navigation" id="r"><span id="s">x</span></div>',
's',
'r',
],
])('%s', (_name, html, hoveredId, expectedId) => {
document.body.innerHTML = html
const hovered = document.getElementById(hoveredId) as HTMLElement
expect(resolveAreaTarget(hovered).id).toBe(expectedId)
})
})
describe('resolveAreaElement', () => {
afterEach(() => {
document.body.innerHTML = ''
})
it.each([
[
'keeps the tracked element while it is connected',
(element: HTMLElement) => ({ element, selector: 'nav#other' }),
'tracked',
],
[
'follows a replaced node via the stored selector',
(element: HTMLElement) => {
element.remove()
return { element, selector: 'nav#other' }
},
'replacement',
],
[
'returns null when the node is gone and the selector matches nothing',
(element: HTMLElement) => {
element.remove()
return { element, selector: 'nav#gone' }
},
null,
],
[
'returns null when the node is gone and no selector was derived',
(element: HTMLElement) => {
element.remove()
return { element, selector: null }
},
null,
],
[
'returns null when the node is gone and the selector is not valid querySelector input',
(element: HTMLElement) => {
element.remove()
return { element, selector: ':::' }
},
null,
],
])('%s', (_name, buildFilter, expectedTestId) => {
document.body.innerHTML =
'<nav id="tracked" data-testid="tracked"></nav><nav id="other" data-testid="replacement"></nav>'
const tracked = document.getElementById('tracked') as HTMLElement
const filter = buildFilter(tracked)
const resolved = resolveAreaElement(filter)
expect(resolved?.dataset.testid ?? null).toBe(expectedTestId)
})
})
describe('stepAreaCandidate', () => {
afterEach(() => {
document.body.innerHTML = ''
})
function chain(rects: number[]): HTMLElement[] {
// builds body > el0 > el1 > ... where each element's mocked rect width is
// rects[i] (same-width neighbours simulate zero-layout wrapper divs)
const elements: HTMLElement[] = []
let parent: HTMLElement = document.body
rects.forEach((width, i) => {
const el = document.createElement('div')
el.id = `el${i}`
el.getBoundingClientRect = () =>
({ top: 0, left: 0, width, height: 100, right: width, bottom: 100 }) as DOMRect
parent.appendChild(el)
elements.push(el)
parent = el
})
return elements
}
it.each([
['up steps to the parent when it is bigger', [300, 200, 100], 2, 2, 'up', 1],
['up skips same-size wrapper parents', [300, 200, 200, 200, 100], 4, 3, 'up', 0],
['up stops below body when nothing bigger remains', [300], 0, 0, 'up', 0],
['down steps back toward the anchor', [300, 200, 100], 2, 0, 'down', 1],
['down skips same-size wrappers next to the candidate', [300, 300, 300, 100], 3, 0, 'down', 3],
['down at the anchor stays put', [300, 200, 100], 2, 2, 'down', 2],
] as const)('%s', (_name, rects, anchorIdx, candidateIdx, direction, expectedIdx) => {
const els = chain([...rects])
expect(stepAreaCandidate(els[anchorIdx], els[candidateIdx], direction)).toBe(els[expectedIdx])
})
})
describe('computeAreaCandidates', () => {
afterEach(() => {
document.body.innerHTML = ''
})
function addElement(
tag: string,
rect: { top: number; left: number; width: number; height: number },
parent: HTMLElement = document.body
): HTMLElement {
const el = document.createElement(tag)
el.getBoundingClientRect = () =>
({ ...rect, right: rect.left + rect.width, bottom: rect.top + rect.height }) as DOMRect
parent.appendChild(el)
return el
}
it('offers visible containers sorted biggest first, dropping too-small elements and non-containers', () => {
const big = addElement('div', { top: 0, left: 0, width: 800, height: 600 })
const nav = addElement('nav', { top: 0, left: 0, width: 200, height: 600 })
addElement('div', { top: 0, left: 0, width: 50, height: 20 }) // below the size floor
addElement('span', { top: 0, left: 0, width: 400, height: 400 }) // not a container tag
expect(computeAreaCandidates()).toEqual([big, nav])
})
it('skips a toolbar overlay root that covers the page', () => {
const toolbarRoot = addElement('div', { top: 0, left: 0, width: 800, height: 600 })
toolbarRoot.setAttribute(TOOLBAR_ROOT_ATTRIBUTE, '')
const main = addElement('main', { top: 0, left: 0, width: 600, height: 400 })
expect(computeAreaCandidates()).toEqual([main])
})
it('collapses a same-rect wrapper chain to its deepest element', () => {
const outer = addElement('div', { top: 0, left: 0, width: 800, height: 600 })
const inner = addElement('div', { top: 0, left: 0, width: 800, height: 600 }, outer)
const distinct = addElement('div', { top: 0, left: 0, width: 400, height: 300 }, inner)
expect(computeAreaCandidates()).toEqual([inner, distinct])
})
})
describe('isInFixedContainer', () => {
afterEach(() => {
document.body.innerHTML = ''
})
it.each([
['a fixed element itself', 'fixed', 'self', true],
['a sticky element itself', 'sticky', 'self', true],
['a child of a fixed container', 'fixed', 'child', true],
['a static element', 'static', 'self', false],
])('detects %s', (_name, position, which, expected) => {
const container = document.createElement('div')
container.style.position = position
const child = document.createElement('span')
container.appendChild(child)
document.body.appendChild(container)
expect(isInFixedContainer(which === 'self' ? container : child)).toBe(expected)
})
})
describe('clickmap loading', () => {
let logic: ReturnType<typeof heatmapToolbarMenuLogic.build>
afterEach(resumeKeaLoadersErrors)
beforeEach(() => {
global.IntersectionObserver = class {
observe(): void {}
unobserve(): void {}
disconnect(): void {}
} as any
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve({ results: [] }),
} as any as Response)
)
jest.spyOn(toolbarApi.elementStats, 'list').mockResolvedValue({
ok: true,
status: 200,
data: { results: [], next: null, previous: null },
} as any)
initKeaTests()
toolbarConfigLogic
.build({
apiURL: 'http://localhost',
accessToken: 'test-token',
refreshToken: 'test-refresh',
clientId: 'test-client',
})
.mount()
logic = heatmapToolbarMenuLogic()
logic.mount()
})
afterEach(() => {
jest.restoreAllMocks()
})
it('loads element stats as soon as the heatmap menu opens, without toggling clickmaps', async () => {
await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions([
'getElementStats',
'getElementStatsSuccess',
])
expect(toolbarApi.elementStats.list).toHaveBeenCalled()
})
it('does not fetch element stats on navigation while the heatmap menu is closed', async () => {
await expectLogic(logic, () =>
logic.actions.setHref('https://example.com/other')
).toNotHaveDispatchedActions(['getElementStats'])
expect(toolbarApi.elementStats.list).not.toHaveBeenCalled()
})
it('fetches element stats on navigation while the heatmap menu is open', async () => {
await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions(['getElementStats'])
await expectLogic(logic, () => logic.actions.setHref('https://example.com/other')).toDispatchActions([
'getElementStats',
])
})
it.each([
[
'setHref',
'https://example.com/other?filter=[a-b]&count={2,1}',
'exact',
'https://example.com/other?filter=[a-b]&count={2,1}',
],
[
'setWildcardHref',
'https://example.com/other?filter=[a-b]&count={2,1}',
'exact',
'https://example.com/other?filter=[a-b]&count={2,1}',
],
['setHref', 'https://example.com/search?q=*', 'exact', 'https://example.com/search?q=*'],
[
'setWildcardHref',
'https://example.com/products/*?filter=[a-b]',
'pattern',
String.raw`https\:\/\/example\.com\/products\/*\?filter\=\[a-b\]`,
],
['setWildcardHref', 'not a URL', 'exact', ''],
] as const)('normalizes %s input %s for heatmap data', async (action, input, matchType, href) => {
await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions(['getElementStatsSuccess'])
await expectLogic(heatmapDataLogic({ context: 'toolbar' }), () =>
logic.actions[action](input)
).toMatchValues({
href,
hrefMatchType: matchType,
})
expect(currentPageLogic.values.wildcardHref).toEqual(input)
})
it.each([
[
'setWildcardHref',
'https://example.com/products/*',
false,
'pattern',
String.raw`https\:\/\/example\.com\/products\/*`,
],
[
'setWildcardHref',
'https://example.com/products/123?lang=en',
true,
'pattern',
String.raw`https\:\/\/example\.com\/products\/*\?lang\=en`,
],
[
'setWildcardHref',
'https://example.com/products/latest?lang=en',
true,
'exact',
'https://example.com/products/latest?lang=en',
],
['setHref', 'https://example.com/search?q=*', false, 'exact', 'https://example.com/search?q=*'],
] as const)(
'preserves matching for %s %s when reopening the menu',
async (action, input, automatic, matchType, href) => {
logic.actions[action](input)
if (automatic) {
currentPageLogic.actions.autoWildcardHref()
}
await expectLogic(heatmapDataLogic({ context: 'toolbar' }), () =>
logic.actions.enableHeatmap()
).toMatchValues({
href,
hrefMatchType: matchType,
})
}
)
it('does not fetch element stats when clickmaps are toggled off while the request is debouncing', async () => {
await expectLogic(logic, () => {
logic.actions.enableHeatmap()
logic.actions.toggleClickmapsEnabled(false)
}).toDispatchActions(['getElementStats', 'getElementStatsSuccess'])
expect(toolbarApi.elementStats.list).not.toHaveBeenCalled()
})
it('keeps heatmapEnabled true after a stats fetch failure so the menu stays open', async () => {
// Deliberate loader failure — kea-loaders would log it
silenceKeaLoadersErrors()
jest.spyOn(toolbarApi.elementStats, 'list').mockRejectedValue(new Error('network error'))
await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions([
'getElementStats',
'getElementStatsFailure',
])
expect(logic.values.heatmapEnabled).toBe(true)
})
it('reloads the clickmap with the element selector filter when an area is chosen', async () => {
await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions(['getElementStatsSuccess'])
const area = document.createElement('nav')
area.id = 'main-nav'
document.body.appendChild(area)
try {
await expectLogic(logic, () => logic.actions.selectHeatmapAreaFilter(area)).toDispatchActions([
'setHeatmapAreaFilter',
'getElementStatsSuccess',
])
const lastCall = (toolbarApi.elementStats.list as jest.Mock).mock.calls.at(-1)[0]
expect(lastCall.properties).toContainEqual(
expect.objectContaining({
key: 'selector',
type: 'element',
value: expect.stringContaining('nav'),
})
)
await expectLogic(logic, () => logic.actions.selectHeatmapAreaFilter(null)).toDispatchActions([
'getElementStatsSuccess',
])
const clearedCall = (toolbarApi.elementStats.list as jest.Mock).mock.calls.at(-1)[0]
expect(clearedCall.properties).toHaveLength(1)
} finally {
area.remove()
}
})
it.each([
{ eventFilterEnabled: true, expectedProperties: 2 },
{ eventFilterEnabled: false, expectedProperties: 1 },
])(
'reloads the clickmap with the variant filter only when the event filter flag is $eventFilterEnabled',
async ({ eventFilterEnabled, expectedProperties }) => {
jest.spyOn(toolbarPosthogJS, 'getFeatureFlag').mockReturnValue(eventFilterEnabled)
await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions([
'getElementStatsSuccess',
])
const variantFilter = { flagKey: 'checkout', variant: 'test' }
await expectLogic(logic, () => logic.actions.setHeatmapVariantFilter(variantFilter)).toDispatchActions([
'getElementStatsSuccess',
])
expect(logic.values.heatmapVariantFilter).toEqual(variantFilter)
const lastCall = (toolbarApi.elementStats.list as jest.Mock).mock.calls.at(-1)[0]
expect(lastCall.properties).toHaveLength(expectedProperties)
if (eventFilterEnabled) {
expect(lastCall.properties).toContainEqual(
expect.objectContaining({ key: '$feature/checkout', value: 'test', type: 'event' })
)
}
}
)
it('loads every remaining page when load all starts, and ends the run on the last page', async () => {
jest.spyOn(toolbarApi.elementStats, 'list').mockResolvedValue({
ok: true,
status: 200,
data: { results: [], next: 'http://localhost/api/element/stats?offset=100', previous: null },
} as any)
const page = jest
.spyOn(toolbarApi.elementStats, 'page')
.mockResolvedValueOnce({
ok: true,
status: 200,
data: { results: [], next: 'http://localhost/api/element/stats?offset=200', previous: null },
} as any)
.mockResolvedValueOnce({
ok: true,
status: 200,
data: { results: [], next: null, previous: null },
} as any)
await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions([
'getElementStatsSuccess',
'getElementStatsSuccess',
])
await expectLogic(logic, () => logic.actions.startLoadingAllElementStats()).toDispatchActions([
'getElementStatsSuccess',
])
expect(logic.values.loadingAllElementStats).toBe(true)
await expectLogic(logic).toDispatchActions(['getElementStatsSuccess'])
expect(page).toHaveBeenCalledTimes(2)
expect(logic.values.loadingAllElementStats).toBe(false)
})
it('ends a run at the page cap, so one press cannot page forever', async () => {
// every page reports another page, so only the cap can end the run
jest.spyOn(toolbarApi.elementStats, 'list').mockResolvedValue({
ok: true,
status: 200,
data: { results: [], next: 'http://localhost/api/element/stats?offset=100', previous: null },
} as any)
const page = jest.spyOn(toolbarApi.elementStats, 'page').mockResolvedValue({
ok: true,
status: 200,
data: { results: [], next: 'http://localhost/api/element/stats?offset=200', previous: null },
} as any)
await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions([
'getElementStatsSuccess',
'getElementStatsSuccess',
])
// the run outlasts the action history expectLogic can match against, so watch the value
logic.actions.startLoadingAllElementStats()
const deadline = Date.now() + 15000
while (logic.values.loadingAllElementStats && Date.now() < deadline) {
await new Promise((resolve) => setTimeout(resolve, 50))
}
expect(page).toHaveBeenCalledTimes(LOAD_ALL_MAX_PAGES)
expect(logic.values.loadingAllElementStats).toBe(false)
// every page waits out the loader's debounce, so the whole run outlasts the default timeout
}, 20000)
it('ends the run when the page navigates', async () => {
// every page reports another page, so only the navigation can end the run
jest.spyOn(toolbarApi.elementStats, 'list').mockResolvedValue({
ok: true,
status: 200,
data: { results: [], next: 'http://localhost/api/element/stats?offset=100', previous: null },
} as any)
jest.spyOn(toolbarApi.elementStats, 'page').mockResolvedValue({
ok: true,
status: 200,
data: { results: [], next: 'http://localhost/api/element/stats?offset=200', previous: null },
} as any)
await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions([
'getElementStatsSuccess',
'getElementStatsSuccess',
])
await expectLogic(logic, () => logic.actions.startLoadingAllElementStats()).toDispatchActions([
'getElementStatsSuccess',
])
expect(logic.values.loadingAllElementStats).toBe(true)
currentPageLogic.actions.setHref('http://localhost/another-page')
expect(logic.values.loadingAllElementStats).toBe(false)
})
it('ends the run when a page comes back refused', async () => {
jest.spyOn(toolbarApi.elementStats, 'list').mockResolvedValue({
ok: true,
status: 200,
data: { results: [], next: 'http://localhost/api/element/stats?offset=100', previous: null },
} as any)
// a 403 resolves to the empty page, which carries no next link
jest.spyOn(toolbarApi.elementStats, 'page').mockResolvedValue({ ok: false, status: 403 } as any)
await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions([
'getElementStatsSuccess',
'getElementStatsSuccess',
])
await expectLogic(logic, () => logic.actions.startLoadingAllElementStats()).toDispatchActions([
'getElementStatsSuccess',
])
expect(logic.values.loadingAllElementStats).toBe(false)
})
it('requests no more pages after the run is stopped', async () => {
// every page reports another page, so only the stop can end the run
jest.spyOn(toolbarApi.elementStats, 'list').mockResolvedValue({
ok: true,
status: 200,
data: { results: [], next: 'http://localhost/api/element/stats?offset=100', previous: null },
} as any)
const page = jest.spyOn(toolbarApi.elementStats, 'page').mockResolvedValue({
ok: true,
status: 200,
data: { results: [], next: 'http://localhost/api/element/stats?offset=200', previous: null },
} as any)
await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions([
'getElementStatsSuccess',
'getElementStatsSuccess',
])
await expectLogic(logic, () => logic.actions.startLoadingAllElementStats()).toDispatchActions([
'getElementStatsSuccess',
])
logic.actions.stopLoadingAllElementStats()
expect(logic.values.loadingAllElementStats).toBe(false)
// long enough for the page already in flight to land and try to queue another
await new Promise((resolve) => setTimeout(resolve, 500))
expect(page).toHaveBeenCalledTimes(2)
})
it('retries the initial load via load more after a stats fetch failure', async () => {
// Deliberate loader failure — kea-loaders would log it
silenceKeaLoadersErrors()
jest.spyOn(toolbarApi.elementStats, 'list').mockRejectedValueOnce(new Error('network error'))
await expectLogic(logic, () => logic.actions.enableHeatmap()).toDispatchActions(['getElementStatsFailure'])
await expectLogic(logic, () => logic.actions.loadMoreElementStats()).toDispatchActions([
'getElementStats',
'getElementStatsSuccess',
])
expect(toolbarApi.elementStats.list).toHaveBeenCalledTimes(2)
})
})
})