Skip to content

Commit

Permalink
a few simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Nov 13, 2024
1 parent 11f2c81 commit 0419620
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 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 {
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

Expand Down Expand Up @@ -185,6 +186,7 @@ describe('SessionRecording', () => {
let onFeatureFlagsCallback: ((flags: string[], variants: Record<string, string | boolean>) => void) | null
let removeCaptureHookMock: Mock
let addCaptureHookMock: Mock
let simpleEventEmitter: SimpleEventEmitter

const addRRwebToWindow = () => {
assignableWindow.__PosthogExtensions__.rrweb = {
Expand Down Expand Up @@ -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]
Expand All @@ -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<PostHog> as PostHog

loadScriptMock.mockImplementation((_ph, _path, callback) => {
Expand Down Expand Up @@ -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)
})
})
})

0 comments on commit 0419620

Please sign in to comment.