Skip to content

Commit

Permalink
fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Dec 8, 2024
1 parent e86a694 commit 07d4b62
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/__tests__/extensions/replay/sessionrecording.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import LazyLoadedSessionRecording, {
RECORDING_IDLE_THRESHOLD_MS,
RECORDING_MAX_EVENT_SIZE,
} from '../../../entrypoints/session-recorder'
import SessionRecorder from '../../../entrypoints/session-recorder'

// Type and source defined here designate a non-user-generated recording event

Expand Down Expand Up @@ -1967,23 +1968,24 @@ describe('SessionRecording', () => {
loadScriptMock.mockImplementation((_ph, _path, callback) => {
callback()
})
sessionRecording = new LazyLoadedSessionRecording(posthog)
sessionRecording = new SessionRecorder(posthog)

sessionRecording.onRemoteConfig(makeDecideResponse({ sessionRecording: { endpoint: '/s/' } }))
sessionRecording.start()
expect(loadScriptMock).toHaveBeenCalled()

expect(sessionRecording['queuedRRWebEvents']).toHaveLength(0)
// called with `recording_initialized` event
expect(sessionRecording['queuedRRWebEvents']).toHaveLength(1)

sessionRecording['_tryAddCustomEvent']('test', { test: 'test' })
})

it('queues events', () => {
expect(sessionRecording['queuedRRWebEvents']).toHaveLength(1)
expect(sessionRecording['queuedRRWebEvents']).toHaveLength(2)
})

it('limits the queue of events', () => {
expect(sessionRecording['queuedRRWebEvents']).toHaveLength(1)
expect(sessionRecording['queuedRRWebEvents']).toHaveLength(2)

for (let i = 0; i < 100; i++) {
sessionRecording['_tryAddCustomEvent']('test', { test: 'test' })
Expand Down
23 changes: 20 additions & 3 deletions src/__tests__/posthog-core.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { mockLogger } from './helpers/mock-logger'

import { Info } from '../utils/event-utils'
import { document, window } from '../utils/globals'
import { uuidv7 } from '../uuidv7'
import * as globals from '../utils/globals'
import { assignableWindow, document, window } from '../utils/globals'
import { uuidv7 } from '../uuidv7'
import { ENABLE_PERSON_PROCESSING, USER_STATE } from '../constants'
import { createPosthogInstance, defaultPostHog } from './helpers/posthog-instance'
import { PostHogConfig, RemoteConfig } from '../types'
Expand Down Expand Up @@ -31,6 +31,23 @@ describe('posthog core', () => {

beforeEach(() => {
jest.useFakeTimers().setSystemTime(baseUTCDateTime)

assignableWindow.__PosthogExtensions__ = assignableWindow.__PosthogExtensions__ || {}
assignableWindow.__PosthogExtensions__.initSessionRecording = () => ({
start: jest.fn(),
stop: jest.fn(),
onRemoteConfig: jest.fn(),
status: 'buffering',
started: true,
overrideLinkedFlag: jest.fn(),
overrideSampling: jest.fn(),
overrideTrigger: jest.fn(),
})
assignableWindow.__PosthogExtensions__.loadExternalDependency = jest
.fn()
.mockImplementation(() => (_ph: PostHog, _name: string, cb: (err?: Error) => void) => {
cb()
})
})

afterEach(() => {
Expand Down Expand Up @@ -419,6 +436,7 @@ describe('posthog core', () => {
},
overrides
)
posthog.sessionRecording = new SessionRecordingLoader(posthog, () => true)
})

it('returns calculated properties', () => {
Expand Down Expand Up @@ -455,7 +473,6 @@ describe('posthog core', () => {
$lib_custom_api_host: 'https://custom.posthog.com',
$is_identified: false,
$process_person_profile: false,
$recording_status: 'buffering',
})
})

Expand Down

0 comments on commit 07d4b62

Please sign in to comment.