Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: type error accessing null object and added test case #1620

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, does this mean that the mousemove event has no clientX or clientY?

if so we're actually better ignoring them completely since the data is invalid without a coordinate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's a mouse move event without any clientX or clientY. According to mozilla dev everything is optional, so it is valid to send an event with any data.

Yeah ignoring is probably a good call!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're welcome to add that to this PR - if you're interested?

i can do as follow-up otherwise

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're already dropping events with no x and y during ingestion, so it's only a nice-to-have not to omit them (although good to not do work)

so only open question for me is if you'd like to handle that in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd leave that to you, I'm not sure where you'd implement that filtering.

type: 'mousemove',
},
],
},
},
})
})

it('should send rageclick events in the same area', async () => {
posthog.heatmaps?.['_onClick']?.(createMockMouseEvent())
posthog.heatmaps?.['_onClick']?.(createMockMouseEvent())
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
pauldambra marked this conversation as resolved.
Show resolved Hide resolved
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) {
pauldambra marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading