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

import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { sceneLogic } from 'scenes/sceneLogic'
import { emptySceneParams } from 'scenes/scenes'
import { Scene } from 'scenes/sceneTypes'
import { urls } from 'scenes/urls'

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

import { customerAnalyticsSceneLogic } from './customerAnalyticsSceneLogic'

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

    beforeEach(() => {
        initKeaTests()
        localStorage.clear()
        sceneLogic.mount()
        router.actions.push(urls.customerAnalytics())
        logic = customerAnalyticsSceneLogic()
        logic.mount()
    })

    afterEach(() => {
        logic.unmount()
        localStorage.clear()
    })

    describe('filterTestAccounts', () => {
        it('defaults to true', () => {
            expectLogic(logic).toMatchValues({
                filterTestAccounts: true,
            })
        })

        it('can be toggled off', async () => {
            await expectLogic(logic, () => {
                logic.actions.setFilterTestAccounts(false)
            }).toMatchValues({
                filterTestAccounts: false,
            })
        })

        it('can be toggled on', async () => {
            logic.actions.setFilterTestAccounts(false)

            await expectLogic(logic, () => {
                logic.actions.setFilterTestAccounts(true)
            }).toMatchValues({
                filterTestAccounts: true,
            })
        })
    })

    describe('page name', () => {
        it.each([
            ['customerAnalyticsDashboard', false, 'Customer analytics'],
            ['customerAnalyticsFeed', false, 'Customer analytics'],
            ['customerAnalyticsDashboard', true, 'Overview'],
            ['customerAnalyticsFeed', true, 'Feed'],
        ])('names the %s breadcrumb when today-rail-nav is %s', (sceneKey, todayRailNav, name) => {
            featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.TODAY_RAIL_NAV]: todayRailNav })
            sceneLogic.actions.setScene(Scene.CustomerAnalytics, sceneKey, emptySceneParams)

            expect(logic.values.breadcrumbs[0].name).toEqual(name)
        })
    })

    describe('URL sync', () => {
        it('activates the Requests tab for its scene key', () => {
            sceneLogic.actions.setScene(Scene.CustomerAnalytics, 'customerAnalyticsFeatureRequests', emptySceneParams)

            expectLogic(logic).toMatchValues({ activeTab: 'feature_requests' })
        })

        it('activates the Tasks tab for its scene key', () => {
            sceneLogic.actions.setScene(Scene.CustomerAnalytics, 'customerAnalyticsTasks', emptySceneParams)

            expectLogic(logic).toMatchValues({ activeTab: 'tasks' })
        })

        it('activates the Accounts tab for the account detail scene key', () => {
            sceneLogic.actions.setScene(Scene.CustomerAnalyticsAccount, 'customerAnalyticsAccount', emptySceneParams)

            expectLogic(logic).toMatchValues({ activeTab: 'accounts' })
        })

        // Regression: the date and test-account writers rebuilt the URL from the path and the
        // search params alone, so they silently dropped the Accounts tab's `#view=` state.
        it('keeps the view hash when writing filter_test_accounts back to the URL', () => {
            router.actions.push(urls.customerAnalyticsAccounts(), {}, { view: { assignmentStatus: 'assigned' } })

            logic.actions.setFilterTestAccounts(false)

            expect(router.values.hashParams.view).toEqual({ assignmentStatus: 'assigned' })
        })

        it('reads filter_test_accounts from URL', () => {
            expectLogic(logic).toMatchValues({
                filterTestAccounts: true,
            })

            router.actions.push(urls.customerAnalytics(), {
                filter_test_accounts: 'false',
            })

            expectLogic(logic).toMatchValues({
                filterTestAccounts: false,
            })
        })
    })
})