From 07d4b62e69b03fe596142f22cedbbc436efa5bf2 Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Sun, 8 Dec 2024 19:00:35 +0000 Subject: [PATCH] fix some tests --- .../replay/sessionrecording.test.ts | 10 ++++---- src/__tests__/posthog-core.ts | 23 ++++++++++++++++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/__tests__/extensions/replay/sessionrecording.test.ts b/src/__tests__/extensions/replay/sessionrecording.test.ts index 250c83cfe..a29bd759c 100644 --- a/src/__tests__/extensions/replay/sessionrecording.test.ts +++ b/src/__tests__/extensions/replay/sessionrecording.test.ts @@ -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 @@ -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' }) diff --git a/src/__tests__/posthog-core.ts b/src/__tests__/posthog-core.ts index 15261bad3..df423036a 100644 --- a/src/__tests__/posthog-core.ts +++ b/src/__tests__/posthog-core.ts @@ -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' @@ -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(() => { @@ -419,6 +436,7 @@ describe('posthog core', () => { }, overrides ) + posthog.sessionRecording = new SessionRecordingLoader(posthog, () => true) }) it('returns calculated properties', () => { @@ -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', }) })