Skip to content

Commit

Permalink
fix: type error accessing null object and added test case (#1620)
Browse files Browse the repository at this point in the history
  • Loading branch information
supermar1010 authored Dec 19, 2024
1 parent 7424c35 commit 142dad1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
33 changes: 31 additions & 2 deletions src/__tests__/heatmaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,27 @@ describe('heatmaps', () => {
expect(posthog.heatmaps!.getAndClearBuffer()).toBeDefined()
})

it('should handle empty mouse moves', async () => {
posthog.heatmaps?.['_onMouseMove']?.(new Event('mousemove'))

jest.advanceTimersByTime(posthog.heatmaps!.flushIntervalMilliseconds + 1)

expect(beforeSendMock).toBeCalledTimes(1)
expect(beforeSendMock.mock.lastCall[0]).toMatchObject({
event: '$$heatmap',
properties: {
$heatmap_data: {
'http://replaced/': [
{
target_fixed: false,
type: 'mousemove',
},
],
},
},
})
})

it('should send rageclick events in the same area', async () => {
posthog.heatmaps?.['_onClick']?.(createMockMouseEvent())
posthog.heatmaps?.['_onClick']?.(createMockMouseEvent())
Expand Down Expand Up @@ -123,16 +144,24 @@ describe('heatmaps', () => {
})

it('should ignore clicks if they come from the toolbar', async () => {
const testElementToolbar = document.createElement('div')
testElementToolbar.id = '__POSTHOG_TOOLBAR__'

posthog.heatmaps?.['_onClick']?.(
createMockMouseEvent({
target: { id: '__POSTHOG_TOOLBAR__' } as Element,
target: testElementToolbar,
})
)
expect(posthog.heatmaps?.['buffer']).toEqual(undefined)

const testElementClosest = document.createElement('div')
testElementClosest.closest = () => {
return {}
}

posthog.heatmaps?.['_onClick']?.(
createMockMouseEvent({
target: { closest: () => ({}) } as unknown as Element,
target: testElementClosest,
})
)
expect(posthog.heatmaps?.['buffer']).toEqual(undefined)
Expand Down
2 changes: 1 addition & 1 deletion src/heatmaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class Heatmaps {
}

private _onMouseMove(e: Event): void {
if (isElementInToolbar(e.target as Element)) {
if (isElementInToolbar(e.target)) {
return
}
clearTimeout(this._mouseMoveTimeout)
Expand Down
9 changes: 6 additions & 3 deletions src/utils/element-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { TOOLBAR_CONTAINER_CLASS, TOOLBAR_ID } from '../constants'

export function isElementInToolbar(el: Element): boolean {
// NOTE: .closest is not supported in IE11 hence the operator check
return el.id === TOOLBAR_ID || !!el.closest?.('.' + TOOLBAR_CONTAINER_CLASS)
export function isElementInToolbar(el: EventTarget | null): boolean {
if (el instanceof Element) {
// NOTE: .closest is not supported in IE11 hence the operator check
return el.id === TOOLBAR_ID || !!el.closest?.('.' + TOOLBAR_CONTAINER_CLASS)
}
return false
}

/*
Expand Down

0 comments on commit 142dad1

Please sign in to comment.