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.3 KB
Raw Blame
import { MOCK_DEFAULT_TEAM } from 'lib/api.mock'

import '@testing-library/jest-dom'

import { cleanup, render, screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { BindLogic } from 'kea'

import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { dashboardLogic } from 'scenes/dashboard/dashboardLogic'

import { initKeaTests } from '~/test/init'
import { AccessControlLevel, DashboardType } from '~/types'

import { AddWidgetModal } from './AddWidgetModal'

type AddWidgetModalProps = React.ComponentProps<typeof AddWidgetModal>

jest.mock('./previews/dashboardWidgetPreviews', () => ({
    DASHBOARD_WIDGET_PREVIEWS: {
        error_tracking_list: () => <div data-attr="error-tracking-preview">Preview</div>,
    },
}))

const MOCK_DASHBOARD: DashboardType = {
    id: 5,
    name: 'Test Dashboard',
    description: 'A test dashboard',
    pinned: false,
    tiles: [],
    tags: [],
    created_at: '2020-01-01T00:00:00Z',
    created_by: {
        id: 1,
        first_name: 'Test',
        last_name: 'User',
        email: '[email protected]',
        uuid: 'abc',
        distinct_id: 'test-distinct-id',
    },
    last_accessed_at: '2020-01-01T00:00:00Z',
    is_shared: false,
    deleted: false,
    creation_mode: 'default',
    user_access_level: AccessControlLevel.Editor,
    filters: {},
    variables: {},
}

function renderAddWidgetModal(props: Partial<AddWidgetModalProps> = {}): ReturnType<typeof dashboardLogic.build> {
    const logic = dashboardLogic({ id: MOCK_DASHBOARD.id, dashboard: MOCK_DASHBOARD })
    logic.mount()

    render(
        <BindLogic logic={dashboardLogic} props={{ id: MOCK_DASHBOARD.id, dashboard: MOCK_DASHBOARD }}>
            <AddWidgetModal isOpen onClose={jest.fn()} onAdd={jest.fn()} {...props} />
        </BindLogic>
    )

    return logic
}

describe('AddWidgetModal', () => {
    beforeEach(() => {
        initKeaTests(true, { ...MOCK_DEFAULT_TEAM, autocapture_exceptions_opt_in: false })
    })

    afterEach(() => {
        cleanup()
    })

    it('renders grouped widget options with section headings', () => {
        renderAddWidgetModal()

        expect(
            screen.getByText(/Bring context from your different PostHog products into one dashboard/i)
        ).toBeInTheDocument()
        expect(screen.getByText('Error tracking', { selector: 'h5' })).toBeInTheDocument()
        expect(screen.getByLabelText('Top issues')).toBeInTheDocument()
        expect(screen.getByText(/Ranked list of the most impactful error tracking issues/i)).toBeInTheDocument()
        expect(screen.queryByText('Notebook widget')).not.toBeInTheDocument()
    })

    it.each([
        [false, ['Activity', 'Error tracking', 'Experiments', 'Logs', 'Session replay', 'Support', 'Surveys']],
        [
            true,
            ['Activity', 'Error tracking', 'Experiments', 'Logs', 'Metrics', 'Session replay', 'Support', 'Surveys'],
        ],
    ])('shows alphabetized product badges when the metrics flag is %s', (metricsEnabled, expectedBadges) => {
        featureFlagLogic.mount()
        featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.METRICS]: metricsEnabled })
        renderAddWidgetModal()

        const badges = screen.getByTestId('dashboard-widget-product-badges')
        expect(Array.from(badges.children).map((badge) => badge.textContent)).toEqual(expectedBadges)
        expect(!!screen.queryByText('Metrics', { selector: 'h5' })).toEqual(metricsEnabled)
    })

    it('filters widget sections to the selected product badges', async () => {
        renderAddWidgetModal()

        const badges = screen.getByTestId('dashboard-widget-product-badges')
        const sectionHeadings = (): string[] =>
            screen.getAllByRole('heading', { level: 5 }).map((heading) => heading.textContent ?? '')

        await userEvent.click(within(badges).getByText('Logs'))
        expect(sectionHeadings()).toEqual(['Logs'])

        await userEvent.click(within(badges).getByText('Error tracking'))
        expect(sectionHeadings()).toEqual(expect.arrayContaining(['Error tracking', 'Logs']))
        expect(sectionHeadings()).toHaveLength(2)

        await userEvent.click(within(badges).getByText('Logs'))
        await userEvent.click(within(badges).getByText('Error tracking'))
        expect(sectionHeadings()).toHaveLength(8)
    })

    it('allows multi-select checkbox behavior within grouped layout', async () => {
        renderAddWidgetModal()

        const topIssuesCard = screen.getByLabelText('Top issues')

        expect(topIssuesCard).toHaveAttribute('aria-checked', 'false')

        await userEvent.click(topIssuesCard)
        expect(topIssuesCard).toHaveAttribute('aria-checked', 'true')

        await userEvent.click(topIssuesCard)
        expect(topIssuesCard).toHaveAttribute('aria-checked', 'false')
    })

    it('submits all selected widget types', async () => {
        const onAdd = jest.fn().mockResolvedValue(undefined)
        const onClose = jest.fn()

        renderAddWidgetModal({ onAdd, onClose })

        await userEvent.click(screen.getByLabelText('Top issues'))
        await userEvent.click(screen.getByTestId('add-widget-submit'))

        expect(onAdd).toHaveBeenCalledWith([expect.objectContaining({ widgetType: 'error_tracking_list' })])
        expect(onClose).toHaveBeenCalled()
    })

    it('still shows previews when exception autocapture is disabled', () => {
        renderAddWidgetModal()

        expect(screen.getByTestId('error-tracking-preview')).toBeInTheDocument()
        expect(screen.queryByText("You haven't captured any exceptions")).not.toBeInTheDocument()
    })

    it('shows a group-level product nudge when the setup requirement is unmet', () => {
        renderAddWidgetModal()

        expect(screen.getByText(/Explore error tracking/i).closest('a')).toHaveAttribute(
            'href',
            'https://posthog.com/docs/error-tracking'
        )
    })

    it('does not show the nudge once the setup requirement is met', () => {
        initKeaTests(true, { ...MOCK_DEFAULT_TEAM, autocapture_exceptions_opt_in: true })
        renderAddWidgetModal()

        expect(screen.queryByText(/Explore error tracking/i)).not.toBeInTheDocument()
    })

    it('does not nudge for product areas without a setup requirement', () => {
        renderAddWidgetModal()

        expect(screen.queryByText(/Explore experiments/i)).not.toBeInTheDocument()
    })

    it('collapses and expands a section when its header is clicked', async () => {
        renderAddWidgetModal()

        expect(screen.getByLabelText('Top issues')).toBeInTheDocument()

        await userEvent.click(screen.getByText('Error tracking', { selector: 'h5' }))
        expect(screen.queryByLabelText('Top issues')).not.toBeInTheDocument()

        await userEvent.click(screen.getByText('Error tracking', { selector: 'h5' }))
        expect(screen.getByLabelText('Top issues')).toBeInTheDocument()
    })

    it('resets collapsed sections when the modal is reopened', async () => {
        const logic = renderAddWidgetModal()

        await userEvent.click(screen.getByText('Error tracking', { selector: 'h5' }))
        expect(logic.values.addWidgetCollapsedGroups).toContain('error_tracking')

        logic.actions.setAddWidgetModalOpen(true)
        expect(logic.values.addWidgetCollapsedGroups).toEqual([])
    })
})