Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.2 KB
Raw Blame
import { expectLogic } from 'kea-test-utils'

import { initKeaTests } from '~/test/init'

import { featureFlagLogic, getFeatureFlagPayload } from './featureFlagLogic'
import { featureFlagSummaryLogic } from './featureFlagSummaryLogic'
import { membersLogic } from './membersLogic'
import { sceneLogic } from './sceneLogic'
import { surveyQuestionLabelsLogic } from './surveyQuestionLabelsLogic'
import { isAuthenticatedTeam, teamLogic } from './teamLogic'
import { userLogic } from './userLogic'

describe('toolbar shims', () => {
    beforeEach(() => {
        initKeaTests(false)
    })

    describe('default values', () => {
        it.each([
            ['userLogic', userLogic, { user: null, themeMode: 'system' }],
            ['membersLogic', membersLogic, { members: [] }],
            ['sceneLogic', sceneLogic, { sceneConfig: null }],
            ['teamLogic', teamLogic, { currentTeam: null, timezone: 'UTC', weekStartDay: 0 }],
            ['featureFlagLogic', featureFlagLogic, { featureFlags: {} }],
            ['surveyQuestionLabelsLogic', surveyQuestionLabelsLogic, { surveyQuestionLabels: {} }],
            [
                'featureFlagSummaryLogic',
                featureFlagSummaryLogic({ flagKey: 'my-flag' }),
                { summary: null, flagLoading: false, flagLoadFailed: false },
            ],
        ])('%s provides expected defaults', (_name, logic, expected) => {
            logic.mount()
            expectLogic(logic).toMatchValues(expected)
        })
    })

    describe('actions are callable without side effects', () => {
        it.each([
            ['userLogic.updateUser', () => userLogic.mount() && userLogic.actions.updateUser({})],
            ['userLogic.loadUserSuccess', () => userLogic.mount() && userLogic.actions.loadUserSuccess({})],
            [
                'membersLogic.ensureAllMembersLoaded',
                () => membersLogic.mount() && membersLogic.actions.ensureAllMembersLoaded(),
            ],
        ])('%s does not throw', (_name, fn) => {
            expect(fn).not.toThrow()
        })
    })

    it('sceneLogic exposes sceneConfig selector for direct reference', () => {
        sceneLogic.mount()
        expect(typeof sceneLogic.selectors.sceneConfig).toBe('function')
    })

    it('no shim triggers fetch when mounted', () => {
        const fetchSpy = jest.fn()
        global.fetch = fetchSpy

        userLogic.mount()
        membersLogic.mount()
        sceneLogic.mount()
        teamLogic.mount()
        featureFlagLogic.mount()
        surveyQuestionLabelsLogic.mount()
        featureFlagSummaryLogic({ flagKey: 'my-flag' }).mount()

        expect(fetchSpy).not.toHaveBeenCalled()
    })

    describe('isAuthenticatedTeam', () => {
        it.each([
            [null, false],
            [undefined, false],
            [{}, false],
            [{ api_token: 'phc_abc123' }, true],
        ])('isAuthenticatedTeam(%j) returns %s', (input, expected) => {
            expect(isAuthenticatedTeam(input)).toBe(expected)
        })
    })

    describe('getFeatureFlagPayload', () => {
        it.each([['SOME_FLAG'], ['ANOTHER_FLAG'], ['']])('returns undefined for %s', (flag) => {
            expect(getFeatureFlagPayload(flag)).toBeUndefined()
        })
    })
})