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.6 KB
Raw Blame
import { expectLogic } from 'kea-test-utils'

import { productSetupStatusLogic } from 'lib/components/ProductEmptyState/productSetupStatusLogic'

import { dashboardsModel } from '~/models/dashboardsModel'
import { ProductKey } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'

import * as generatedApi from '../generated/api'
import { dashboardsSetupLogic } from './dashboardsSetupLogic'

// Guards the mapping into the app-wide setup-status layer: if it breaks, the scene
// empty-state gate strands on its spinner or shows the wrong surface.
describe('dashboardsSetupLogic', () => {
    let listSpy: jest.SpyInstance

    beforeEach(() => {
        listSpy = jest.spyOn(generatedApi, 'dashboardsList')
        initKeaTests()
    })

    afterEach(() => {
        listSpy.mockRestore()
    })

    it.each([
        [0, 'needs-setup'],
        [1, 'has-data'],
        [17, 'has-data'],
    ])('pushes a dashboard count of %i as status %s', async (count, expected) => {
        listSpy.mockResolvedValue({ count, results: [] })
        const logic = dashboardsSetupLogic()
        logic.mount()
        await expectLogic(logic).toFinishAllListeners()
        expect(productSetupStatusLogic({ productKey: ProductKey.DASHBOARDS }).values.status).toBe(expected)
    })

    it('re-detects when a dashboard is created without leaving the scene', async () => {
        listSpy.mockResolvedValueOnce({ count: 0, results: [] }).mockResolvedValue({ count: 1, results: [] })
        const logic = dashboardsSetupLogic()
        logic.mount()
        await expectLogic(logic).toFinishAllListeners()
        expect(productSetupStatusLogic({ productKey: ProductKey.DASHBOARDS }).values.status).toBe('needs-setup')

        // Creating from the empty state's modal does not navigate unless the user opts in,
        // so without the recheck wiring the gate would keep hiding the new dashboard.
        dashboardsModel.actions.addDashboardSuccess({ id: 1, name: 'First' } as any)
        await expectLogic(logic).toFinishAllListeners()
        expect(productSetupStatusLogic({ productKey: ProductKey.DASHBOARDS }).values.status).toBe('has-data')
    })

    it.each(['single', 'bulk'] as const)(
        'returns to setup after the last visible dashboard is deleted through %s deletion',
        async (method) => {
            listSpy.mockResolvedValueOnce({ count: 1, results: [] }).mockImplementation(async (_, params) => ({
                count: params?.exclude_generated ? 0 : 1,
                results: [],
            }))
            const logic = dashboardsSetupLogic()
            logic.mount()
            await expectLogic(logic).toFinishAllListeners()
            expect(productSetupStatusLogic({ productKey: ProductKey.DASHBOARDS }).values.status).toBe('has-data')

            if (method === 'bulk') {
                dashboardsModel.actions.bulkDeleteDashboardsSuccess({ deleted: [1], skipped: [] })
            } else {
                dashboardsModel.actions.deleteDashboardSuccess({ id: 1, name: 'Last', deleted: true } as any)
            }
            await expectLogic(logic).toFinishAllListeners()
            expect(productSetupStatusLogic({ productKey: ProductKey.DASHBOARDS }).values.status).toBe('needs-setup')
        }
    )

    it('fails open to unknown when the count query fails before any answer', async () => {
        listSpy.mockRejectedValue(new Error('network down'))
        const logic = dashboardsSetupLogic()
        logic.mount()
        await expectLogic(logic).toFinishAllListeners()
        expect(productSetupStatusLogic({ productKey: ProductKey.DASHBOARDS }).values.status).toBe('unknown')
    })
})