Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Nov 1, 2023
1 parent 846a4a2 commit 13f9771
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/__tests__/session-props.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { SessionPropsManager } from '../session-props'
import { SessionIdManager } from '../sessionid'
import { PostHogPersistence } from '../posthog-persistence'

describe('Session Props Manager', () => {
const createSessionPropsManager = () => {
const onSessionId = jest.fn()
const generateProps = jest.fn()
const persistenceRegister = jest.fn()
const sessionIdManager = {
onSessionId,
} as unknown as SessionIdManager
const persistence = {
register: persistenceRegister,
props: {},
} as unknown as PostHogPersistence
const sessionPropsManager = new SessionPropsManager(sessionIdManager, persistence, generateProps)

return {
onSessionId,
sessionPropsManager,
persistence,
sessionIdManager,
generateProps,
persistenceRegister,
}
}

it('should register a callback with the session id manager', () => {
const { onSessionId } = createSessionPropsManager()
expect(onSessionId).toHaveBeenCalledTimes(1)
})

it('should update persistence with client session props', () => {
// arrange
const utmSource = 'some-utm-source'
const sessionId = 'session-id'
const { onSessionId, generateProps, persistenceRegister } = createSessionPropsManager()
generateProps.mockReturnValue({ utm_source: utmSource })
const callback = onSessionId.mock.calls[0][0]

// act
callback(sessionId)

//assert
expect(generateProps).toHaveBeenCalledTimes(1)

expect(persistenceRegister).toBeCalledWith({
$client_session_props: {
props: {
utm_source: 'some-utm-source',
},
sessionId: 'session-id',
},
})
})

it('should not update client session props when session id stays the same', () => {
// arrange
const sessionId1 = 'session-id-1'
const { onSessionId, persistence, generateProps, persistenceRegister } = createSessionPropsManager()
persistence.props = {
$client_session_props: {
props: {},
sessionId: sessionId1,
},
}
const callback = onSessionId.mock.calls[0][0]

// act
callback(sessionId1)

//assert
expect(generateProps).toHaveBeenCalledTimes(0)
expect(persistenceRegister).toHaveBeenCalledTimes(0)
})

it('should update client session props when session id changes', () => {
// arrange
const sessionId1 = 'session-id-1'
const sessionId2 = 'session-id-2'

const { onSessionId, persistence, generateProps, persistenceRegister } = createSessionPropsManager()
persistence.props = {
$client_session_props: {
props: {},
sessionId: sessionId1,
},
}
const callback = onSessionId.mock.calls[0][0]

// act
callback(sessionId2)

//assert
expect(generateProps).toHaveBeenCalledTimes(1)
expect(persistenceRegister).toHaveBeenCalledTimes(1)
})

it('should return client session props', () => {
// arrange
const { persistence, sessionPropsManager } = createSessionPropsManager()
persistence.props = {
$client_session_props: {
props: {
utm_source: 'some-utm-source',
},
sessionId: 'session-id',
},
}

// act
const properties = sessionPropsManager.getSessionProps()

//assert
expect(properties).toEqual({
$client_session_utm_source: 'some-utm-source',
})
})
})

0 comments on commit 13f9771

Please sign in to comment.