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.

6.5 KB
Raw Blame
import '@testing-library/jest-dom'

import { cleanup, render, screen } from '@testing-library/react'
import { Provider } from 'kea'
import { router } from 'kea-router'
import type { ReactNode } from 'react'

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 { initKeaTests } from '~/test/init'

import { CustomerAnalyticsScene } from './CustomerAnalyticsScene'
import { customerAnalyticsSceneLogic } from './customerAnalyticsSceneLogic'

jest.mock('~/layout/scenes/components/FeaturePreviewSceneGate', () => ({
    FeaturePreviewSceneGate: ({ children }: { children: ReactNode }) => <>{children}</>,
}))
jest.mock('~/layout/scenes/components/SceneContent', () => ({
    SceneContent: ({ children }: { children: ReactNode }) => <>{children}</>,
}))
jest.mock('~/layout/scenes/components/SceneTitleSection', () => ({
    SceneTitleSection: ({ name, description }: { name: string; description?: string }) => (
        <>
            <h1>{name}</h1>
            {description && <p>{description}</p>}
        </>
    ),
}))
jest.mock('lib/components/NotFound', () => ({
    NotFound: () => <div data-attr="page-not-found" />,
}))
jest.mock('./CustomerAnalyticsFilters', () => ({ CustomerAnalyticsFilters: () => null }))
jest.mock('./components/AccountNotes/AccountNotesTabContent', () => ({ AccountNotesTabContent: () => null }))
jest.mock('./components/Accounts/AccountsTabContent', () => ({ AccountsTabContent: () => null }))
jest.mock('./components/Announcements/AnnouncementsTabContent', () => ({ AnnouncementsTabContent: () => null }))
jest.mock('./components/CustomerJourneys/CustomerJourneys', () => ({ CustomerJourneys: () => null }))
jest.mock('./components/CustomerJourneys/CustomerJourneySelect', () => ({ CustomerJourneySelect: () => null }))
jest.mock('./components/CustomerJourneys/DeleteJourneyButton', () => ({ DeleteJourneyButton: () => null }))
jest.mock('./components/CustomerTasks/CustomerTasksInbox', () => ({
    CustomerTasksInbox: () => <div data-attr="tasks-inbox" />,
}))
jest.mock('./components/FeatureRequests/FeatureRequestsTabContent', () => ({ FeatureRequestsTabContent: () => null }))
jest.mock('./components/Feed/FeedTabContent', () => ({ FeedTabContent: () => null }))
jest.mock('./components/FeedbackButton', () => ({ FeedbackButton: () => null }))
jest.mock('./components/Insights/ActiveUsersInsights', () => ({ ActiveUsersInsights: () => null }))
jest.mock('./components/Insights/SessionInsights', () => ({ SessionInsights: () => null }))
jest.mock('./components/Insights/SignupInsights', () => ({ SignupInsights: () => null }))

describe('CustomerAnalyticsScene', () => {
    beforeEach(() => {
        initKeaTests()
        featureFlagLogic.mount()
        sceneLogic.mount()
        customerAnalyticsSceneLogic.mount()
        sceneLogic.actions.setScene(Scene.CustomerAnalytics, 'customerAnalyticsTasks', emptySceneParams)
    })

    afterEach(() => {
        cleanup()
        customerAnalyticsSceneLogic.unmount()
        sceneLogic.unmount()
        featureFlagLogic.unmount()
    })

    it.each([
        [
            'customerAnalyticsTasks',
            '/customer_analytics/tasks',
            'Feature requests',
            '/customer_analytics/feature-requests',
        ],
        [
            'customerAnalyticsFeatureRequests',
            '/customer_analytics/feature-requests',
            'Tasks',
            '/customer_analytics/tasks',
        ],
    ])('does not carry task filters across tabs from %s', (scene, path, label, destination) => {
        router.actions.push(path, { status: 'open', archive: 'active', due: 'overdue', assignee: 'me' })
        sceneLogic.actions.setScene(Scene.CustomerAnalytics, scene, emptySceneParams)
        featureFlagLogic.actions.setFeatureFlags(
            [
                FEATURE_FLAGS.CUSTOMER_ANALYTICS,
                FEATURE_FLAGS.CUSTOMER_ANALYTICS_CUSTOMER_TASKS,
                FEATURE_FLAGS.CUSTOMER_ANALYTICS_FEATURE_REQUESTS,
            ],
            {
                [FEATURE_FLAGS.CUSTOMER_ANALYTICS]: true,
                [FEATURE_FLAGS.CUSTOMER_ANALYTICS_CUSTOMER_TASKS]: true,
                [FEATURE_FLAGS.CUSTOMER_ANALYTICS_FEATURE_REQUESTS]: true,
            }
        )
        render(
            <Provider>
                <CustomerAnalyticsScene />
            </Provider>
        )
        expect(screen.getByText(label).closest('a')).toHaveAttribute('href', `/project/997${destination}`)
    })

    it('hides the tab bar in the Today rail and keeps the open tab', () => {
        featureFlagLogic.actions.setFeatureFlags(
            [
                FEATURE_FLAGS.TODAY_RAIL_NAV,
                FEATURE_FLAGS.CUSTOMER_ANALYTICS,
                FEATURE_FLAGS.CUSTOMER_ANALYTICS_CUSTOMER_TASKS,
            ],
            {
                [FEATURE_FLAGS.TODAY_RAIL_NAV]: true,
                [FEATURE_FLAGS.CUSTOMER_ANALYTICS]: true,
                [FEATURE_FLAGS.CUSTOMER_ANALYTICS_CUSTOMER_TASKS]: true,
            }
        )

        render(
            <Provider>
                <CustomerAnalyticsScene />
            </Provider>
        )

        expect(document.querySelector('[data-attr="customer-analytics-tabs"]')).not.toBeInTheDocument()
        expect(screen.queryByRole('tab')).not.toBeInTheDocument()
        expect(document.querySelector('[data-attr="tasks-inbox"]')).toBeInTheDocument()
    })

    it('shows the Tasks tab and inbox only when the tasks flag is enabled', () => {
        featureFlagLogic.actions.setFeatureFlags(
            [FEATURE_FLAGS.CUSTOMER_ANALYTICS, FEATURE_FLAGS.CUSTOMER_ANALYTICS_CUSTOMER_TASKS],
            {
                [FEATURE_FLAGS.CUSTOMER_ANALYTICS]: true,
                [FEATURE_FLAGS.CUSTOMER_ANALYTICS_CUSTOMER_TASKS]: true,
            }
        )

        const { rerender } = render(
            <Provider>
                <CustomerAnalyticsScene />
            </Provider>
        )

        expect(screen.getAllByText('Tasks')).toHaveLength(2)
        expect(document.querySelector('[data-attr="tasks-inbox"]')).toBeInTheDocument()

        featureFlagLogic.actions.setFeatureFlags([FEATURE_FLAGS.CUSTOMER_ANALYTICS], {
            [FEATURE_FLAGS.CUSTOMER_ANALYTICS]: true,
        })
        rerender(
            <Provider>
                <CustomerAnalyticsScene />
            </Provider>
        )

        expect(document.querySelector('[data-attr="page-not-found"]')).toBeInTheDocument()
    })
})