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.

2.6 KB
Raw Blame
import { act, render } from '@testing-library/react'

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

import { panelLayoutLogic } from '../panelLayoutLogic'
import { PanelLayoutPanels } from './PanelLayoutPanels'

jest.mock('../ProjectTree/ProjectTree', () => ({
    ProjectTree: ({ root, isActiveInPanel }: { root: string; isActiveInPanel: boolean }) => (
        <div data-attr="mock-tree" data-root={root} data-active={isActiveInPanel} />
    ),
}))
jest.mock('lib/components/NotificationsMenu/NotificationsPanel', () => ({
    NotificationsPanel: () => <div data-attr="mock-notifications" />,
}))

describe('PanelLayoutPanels', () => {
    beforeEach(() => {
        initKeaTests()
        panelLayoutLogic.mount()
    })

    function renderedTrees(container: HTMLElement): { root: string | null; hidden: boolean; active: boolean }[] {
        return Array.from(container.querySelectorAll('[data-attr="mock-tree"]')).map((tree) => ({
            root: tree.getAttribute('data-root'),
            hidden: !!tree.closest('.hidden'),
            active: tree.getAttribute('data-active') === 'true',
        }))
    }

    it('keeps the project tree mounted but hidden after switching away from it', () => {
        const { container } = render(<PanelLayoutPanels />)
        expect(renderedTrees(container)).toEqual([])

        act(() => panelLayoutLogic.actions.showLayoutPanel(true))
        act(() => panelLayoutLogic.actions.setActivePanelIdentifier('Project'))
        expect(renderedTrees(container)).toEqual([{ root: 'project://', hidden: false, active: true }])

        // Switching must not tear down the project tree — it stays in the DOM, hidden.
        act(() => panelLayoutLogic.actions.setActivePanelIdentifier('Notifications'))
        expect(renderedTrees(container)).toEqual([{ root: 'project://', hidden: true, active: false }])

        act(() => panelLayoutLogic.actions.clearActivePanelIdentifier())
        expect(renderedTrees(container)).toEqual([{ root: 'project://', hidden: true, active: false }])
    })

    it('mounts the notifications panel only while it is the active panel', () => {
        const { container } = render(<PanelLayoutPanels />)

        act(() => panelLayoutLogic.actions.setActivePanelIdentifier('Notifications'))
        expect(container.querySelector('[data-attr="mock-notifications"]')).toBeTruthy()

        // Notifications is excluded from keep-mounted: its logic drives unread/read semantics.
        act(() => panelLayoutLogic.actions.clearActivePanelIdentifier())
        expect(container.querySelector('[data-attr="mock-notifications"]')).toBeNull()
    })
})