diff --git a/src/__tests__/extensions/replay/sessionrecording.test.ts b/src/__tests__/extensions/replay/sessionrecording.test.ts index 3b4cadfbf..3cb10ba03 100644 --- a/src/__tests__/extensions/replay/sessionrecording.test.ts +++ b/src/__tests__/extensions/replay/sessionrecording.test.ts @@ -48,6 +48,7 @@ import { import Mock = jest.Mock import { ConsentManager } from '../../../consent' import { waitFor } from '@testing-library/preact' +import { SimpleEventEmitter } from '../../../utils/simple-event-emitter' // Type and source defined here designate a non-user-generated recording event @@ -185,6 +186,7 @@ describe('SessionRecording', () => { let onFeatureFlagsCallback: ((flags: string[], variants: Record) => void) | null let removeCaptureHookMock: Mock let addCaptureHookMock: Mock + let simpleEventEmitter: SimpleEventEmitter const addRRwebToWindow = () => { assignableWindow.__PosthogExtensions__.rrweb = { @@ -239,6 +241,8 @@ describe('SessionRecording', () => { removeCaptureHookMock = jest.fn() addCaptureHookMock = jest.fn().mockImplementation(() => removeCaptureHookMock) + simpleEventEmitter = new SimpleEventEmitter() + // TODO we really need to make this a real posthog instance :cry: posthog = { get_property: (property_key: string): Property | undefined => { return postHogPersistence?.['props'][property_key] @@ -261,6 +265,10 @@ describe('SessionRecording', () => { }, } as unknown as ConsentManager, register_for_session() {}, + _internalEventEmitter: simpleEventEmitter, + on: (event, cb) => { + return simpleEventEmitter.on(event, cb) + }, } as Partial as PostHog loadScriptMock.mockImplementation((_ph, _path, callback) => { @@ -2250,4 +2258,39 @@ describe('SessionRecording', () => { ]) }) }) + + describe('Event triggering', () => { + beforeEach(() => { + sessionRecording.startIfEnabledOrStop() + sessionRecording.afterDecideResponse( + makeDecideResponse({ + sessionRecording: { + endpoint: '/s/', + eventTriggers: ['$exception'], + }, + }) + ) + }) + + it('starts buffering', () => { + expect(sessionRecording['status']).toBe('buffering') + }) + + it('flushes buffer and starts when sees event', async () => { + // Emit some events before hitting blocked URL + _emit(createIncrementalSnapshot({ data: { source: 1 } })) + _emit(createIncrementalSnapshot({ data: { source: 2 } })) + + expect(sessionRecording['buffer'].data).toHaveLength(2) + + simpleEventEmitter.emit('eventCaptured', { event: 'not-$exception' }) + + expect(sessionRecording['status']).toBe('buffering') + + simpleEventEmitter.emit('eventCaptured', { event: '$exception' }) + + expect(sessionRecording['status']).toBe('active') + expect(sessionRecording['buffer'].data).toHaveLength(0) + }) + }) })