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.

7.0 KB
Raw Blame
import { MOCK_TEAM_ID } from 'lib/api.mock'

import '@testing-library/jest-dom'

import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'

import { featureFlagsActivityResponseJson } from 'lib/components/ActivityLog/__mocks__/activityLogMocks'
import { activityDescriberLoaders } from 'lib/components/ActivityLog/activityDescribers'
import {
    activityLogLogic,
    describerFor,
    ensureActivityDescribersLoaded,
} from 'lib/components/ActivityLog/activityLogLogic'
import { ActivityLogItem, defaultDescriber, humanize } from 'lib/components/ActivityLog/humanizeActivity'
import { flagActivityDescriber } from 'scenes/feature-flags/activityDescriptions'

import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
import { ActivityScope } from '~/types'

describe('the activity log logic', () => {
    let logic: ReturnType<typeof activityLogLogic.build>

    beforeEach(async () => {
        await ensureActivityDescribersLoaded()
    })

    describe('when not scoped by ID', () => {
        beforeEach(() => {
            useMocks({
                get: {
                    [`/api/projects/${MOCK_TEAM_ID}/feature_flags/activity/`]: {
                        results: featureFlagsActivityResponseJson,
                        next: 'a provided url',
                    },
                },
            })
            initKeaTests()
            logic = activityLogLogic({ scope: ActivityScope.FEATURE_FLAG })
            logic.mount()
        })

        it('sets a key', () => {
            expect(logic.key).toEqual('activity/FeatureFlag/all')
        })

        it('loads on mount', async () => {
            await expectLogic(logic).toDispatchActions(['fetchActivity', 'fetchActivitySuccess'])
        })

        it('can load a page of activity', async () => {
            await expectLogic(logic).toFinishAllListeners().toMatchValues({
                activityLoading: false,
            })

            // react fragments confuse equality check so,
            // stringify to confirm this value has the humanized version of the response
            // detailed tests for humanization are below
            expect(JSON.stringify(logic.values.humanizedActivity)).toEqual(
                JSON.stringify(humanize(featureFlagsActivityResponseJson, describerFor))
            )
        })
    })

    describe('when scoped by ID', () => {
        beforeEach(() => {
            useMocks({
                get: {
                    [`/api/projects/${MOCK_TEAM_ID}/feature_flags/7/activity/`]: {
                        results: featureFlagsActivityResponseJson,
                        next: 'a provided url',
                    },
                },
            })
            initKeaTests()
            logic = activityLogLogic({ scope: ActivityScope.FEATURE_FLAG, id: 7, describer: flagActivityDescriber })
            logic.mount()
        })

        it('sets a key', () => {
            expect(logic.key).toEqual('activity/FeatureFlag/7')
        })

        it('loads on mount', async () => {
            await expectLogic(logic).toDispatchActions(['fetchActivity', 'fetchActivitySuccess'])
        })
    })

    describe('when starting at page 4', () => {
        beforeEach(() => {
            useMocks({
                get: {
                    [`/api/projects/${MOCK_TEAM_ID}/feature_flags/7/activity/`]: ({ request }) => {
                        const isOnPageFour = new URL(request.url).searchParams.get('page') === '4'

                        return [
                            200,
                            {
                                results: isOnPageFour ? featureFlagsActivityResponseJson : [],
                                next: 'a provided url',
                            },
                        ]
                    },
                },
            })
            initKeaTests()
            logic = activityLogLogic({
                scope: ActivityScope.FEATURE_FLAG,
                id: 7,
                describer: flagActivityDescriber,
                startingPage: 4,
            })
            logic.mount()
        })

        it('sets a key', () => {
            expect(logic.key).toEqual('activity/FeatureFlag/7')
        })

        it('loads data from page 4 on mount', async () => {
            await expectLogic(logic).toDispatchActions(['fetchActivity', 'fetchActivitySuccess'])

            expect(JSON.stringify(logic.values.humanizedActivity)).toEqual(
                JSON.stringify(humanize(featureFlagsActivityResponseJson, describerFor))
            )
        })
    })

    describe('highlighted activity ID from URL', () => {
        beforeEach(() => {
            useMocks({
                get: {
                    [`/api/projects/${MOCK_TEAM_ID}/feature_flags/activity/`]: {
                        results: featureFlagsActivityResponseJson,
                        next: null,
                    },
                },
            })
            initKeaTests()
            logic = activityLogLogic({ scope: ActivityScope.FEATURE_FLAG })
            logic.mount()
        })

        it('sets highlightedActivityId when activity param is in the URL', async () => {
            await expectLogic(logic).toDispatchActions(['fetchActivitySuccess'])

            router.actions.push('/project/1/feature_flags', { tab: 'history', activity: 'test-uuid-1234' }, {})

            await expectLogic(logic).toMatchValues({
                highlightedActivityId: 'test-uuid-1234',
            })
        })

        it('starts with no highlighted activity', async () => {
            await expectLogic(logic).toMatchValues({
                highlightedActivityId: null,
            })
        })
    })

    describe('incident regression test for #inc-2023-03-09-us-cloud-ui-unavailable-when-users-have-a-notification', () => {
        it('backend sends unexpected field and describer should not explode', () => {
            expect(() => {
                humanize(
                    [
                        {
                            // a very unexpected activity log item received in production
                            type: 'insight',
                            after: true,
                            field: 'saved',
                            action: 'changed',
                            before: false,
                        } as unknown as ActivityLogItem,
                    ],
                    describerFor,
                    true
                )
            }).not.toThrow()
        })
    })

    describe('describer registry', () => {
        it.each(Object.keys(activityDescriberLoaders))('resolves a describer for %s', (scope) => {
            const describer = describerFor({ scope } as ActivityLogItem)

            expect(describer).toBeInstanceOf(Function)
            expect(describer).not.toBe(defaultDescriber)
        })

        it.each([
            ['an unknown scope', { scope: 'NotARealScope' } as unknown as ActivityLogItem],
            ['no item', undefined],
        ])('falls back to the default describer for %s', (_, logItem) => {
            expect(describerFor(logItem)).toBe(defaultDescriber)
        })
    })
})