Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Apr 24, 2024
1 parent a4d4294 commit f9a374c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
36 changes: 36 additions & 0 deletions src/__tests__/heatmaps.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createPosthogInstance } from './helpers/posthog-instance'
import { uuidv7 } from '../uuidv7'
import { PostHog } from '../posthog-core'
import { HEATMAPS_ENABLED_SERVER_SIDE } from '../constants'

Check failure on line 4 in src/__tests__/heatmaps.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'HEATMAPS_ENABLED_SERVER_SIDE' is defined but never used
import { DecideResponse } from '../types'
jest.mock('../utils/logger')

describe('heatmaps', () => {
Expand Down Expand Up @@ -105,4 +107,38 @@ describe('heatmaps', () => {
)
expect(posthog.heatmaps?.['buffer']).not.toEqual(undefined)
})

describe('afterDecideResponse()', () => {
it('should not be enabled before the decide response', () => {
expect(posthog.heatmaps!.isEnabled).toBe(false)
})

it('should be enabled if client config option is enabled', () => {
posthog.config.__preview_heatmaps = true
expect(posthog.heatmaps!.isEnabled).toBe(true)
})

it.each([
// Client not defined
[undefined, false, false],
[undefined, true, true],
[undefined, false, false],
// Client false
[false, false, false],
[false, true, false],

// Client true
[true, false, true],
[true, true, true],
])(
'when client side config is %p and remote opt in is %p - heatmaps enabled should be %p',
(clientSideOptIn, serverSideOptIn, expected) => {
posthog.config.__preview_heatmaps = clientSideOptIn
posthog.heatmaps!.afterDecideResponse({
heatmaps: serverSideOptIn,
} as DecideResponse)
expect(posthog.heatmaps!.isEnabled).toBe(expected)
}
)
})
})
10 changes: 5 additions & 5 deletions src/heatmaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PostHog } from './posthog-core'
import { document, window } from './utils/globals'
import { getParentElement, isTag } from './autocapture-utils'
import { HEATMAPS_ENABLED_SERVER_SIDE } from './constants'
import { isUndefined } from './utils/type-utils'

type HeatmapEventBuffer =
| {
Expand Down Expand Up @@ -50,6 +51,7 @@ export class Heatmaps {

constructor(instance: PostHog) {
this.instance = instance
this._enabledServerSide = !!this.instance.persistence?.props[HEATMAPS_ENABLED_SERVER_SIDE]
}

public startIfEnabled(): void {
Expand All @@ -59,11 +61,9 @@ export class Heatmaps {
}

public get isEnabled(): boolean {
return (
!!this.instance.config.__preview_heatmaps ||
!!this._enabledServerSide ||
!!this.instance.persistence?.props[HEATMAPS_ENABLED_SERVER_SIDE]
)
return !isUndefined(this.instance.config.__preview_heatmaps)
? this.instance.config.__preview_heatmaps
: this._enabledServerSide
}

public afterDecideResponse(response: DecideResponse) {
Expand Down

0 comments on commit f9a374c

Please sign in to comment.