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.

4.8 KB
Raw Blame
import { MOCK_DEFAULT_TEAM, MOCK_DEFAULT_USER } from 'lib/api.mock'

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

import { Scene } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
import { userLogic } from 'scenes/userLogic'

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

import { sceneDashboardChoiceModalLogic } from './sceneDashboardChoiceModalLogic'

describe('sceneDashboardChoiceModalLogic', () => {
    let logic: ReturnType<typeof sceneDashboardChoiceModalLogic.build>

    beforeEach(async () => {
        useMocks({
            get: {
                '/api/projects/@current': () => MOCK_DEFAULT_TEAM,
            },
            post: {
                '/api/users/@me/scene_personalisation': async ({ request }) => {
                    const data = (await request.json()) as any
                    return [
                        200,
                        {
                            ...MOCK_DEFAULT_USER,
                            scene_personalisation: [
                                ...(MOCK_DEFAULT_USER.scene_personalisation || []),
                                { scene: data.scene, dashboard: data.dashboard },
                            ],
                        },
                    ]
                },
            },
            patch: {
                '/api/environments/:team': async ({ request }) => {
                    const data = (await request.json()) as any
                    return [
                        200,
                        {
                            ...MOCK_DEFAULT_TEAM,
                            primary_dashboard: data?.primary_dashboard,
                            home_tab_dashboard: data?.home_tab_dashboard,
                        },
                    ]
                },
            },
        })
        initKeaTests()
        userLogic.mount()
        await expectLogic(userLogic).toDispatchActions(['loadUserSuccess']).toFinishAllListeners()
    })

    describe('for project homepage', () => {
        beforeEach(() => {
            logic = sceneDashboardChoiceModalLogic({ scene: Scene.ProjectHomepage })
            logic.mount()
        })

        it('modal starts off hidden', async () => {
            await expectLogic(logic).toMatchValues({
                isOpen: false,
            })
        })

        it('can be opened and closed', async () => {
            await expectLogic(logic, () => {
                logic.actions.showSceneDashboardChoiceModal()
            }).toMatchValues({
                isOpen: true,
            })

            logic.actions.closeSceneDashboardChoiceModal()
            await expectLogic(logic).toMatchValues({
                isOpen: false,
            })
        })

        it('is set by setSceneDashboardChoice', async () => {
            await expectLogic(logic, () => {
                logic.actions.setSceneDashboardChoice(12)
            })
                .toDispatchActions(teamLogic, ['updateCurrentTeam', 'updateCurrentTeamSuccess'])
                .toMatchValues({
                    currentDashboardId: 12,
                })
        })
    })

    describe('for person page', () => {
        beforeEach(() => {
            logic = sceneDashboardChoiceModalLogic({ scene: Scene.Person })
            logic.mount()
        })

        it('choice is set by setSceneDashboardChoice', async () => {
            await expectLogic(logic, () => {
                logic.actions.setSceneDashboardChoice(12)
            })
                .toDispatchActions(userLogic, ['setUserScenePersonalisation', 'setUserScenePersonalisationSuccess'])
                .toMatchValues({
                    currentDashboardId: 12,
                })
        })
    })

    describe('for product analytics Home', () => {
        beforeEach(() => {
            logic = sceneDashboardChoiceModalLogic({ scene: Scene.ProductAnalyticsHomeTab })
            logic.mount()
        })

        it('opens the starter before the existing dashboard picker', async () => {
            await expectLogic(logic, () => {
                logic.actions.showSceneDashboardChoiceModal()
            }).toMatchValues({
                isOpen: true,
                dashboardChoiceView: 'starter',
            })

            await expectLogic(logic, () => {
                logic.actions.showDashboardPicker()
            }).toMatchValues({
                dashboardChoiceView: 'existing',
            })
        })

        it('can restore PostHog Home', async () => {
            await expectLogic(logic, () => {
                logic.actions.setSceneDashboardChoice(null)
            })
                .toDispatchActions(teamLogic, ['updateCurrentTeam', 'updateCurrentTeamSuccess'])
                .toMatchValues({
                    currentDashboardId: null,
                })
        })
    })
})