import { act, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { mockAnimationsApi } from 'jsdom-testing-mocks'
import { Button, SheetTitle } from 'ui'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { MobileSheetProvider, useMobileSheet } from './MobileSheetContext'
import { StudioMobileSheetNav } from './StudioMobileSheetNav'
import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
import { sidebarManagerState } from '@/state/sidebar-manager-state'
import { customRender } from '@/tests/lib/custom-render'
mockAnimationsApi()
const IssuesMobileMenu = ({
namedContent,
}: {
namedContent?: 'search' | typeof SIDEBAR_KEYS.HELP_PANEL
}) => {
const { setContent } = useMobileSheet()
return (
<>
>
)
}
afterEach(() => {
vi.unstubAllGlobals()
sidebarManagerState.unregisterSidebar(SIDEBAR_KEYS.HELP_PANEL)
})
describe('StudioMobileSheetNav', () => {
it.each(['search', SIDEBAR_KEYS.HELP_PANEL] as const)(
'closes %s content for query navigation while Issues menu filtering is preserved',
async (namedContent) => {
vi.stubGlobal('innerWidth', 390)
sidebarManagerState.registerSidebar(SIDEBAR_KEYS.HELP_PANEL, () => (
Help
))
const user = userEvent.setup()
const { router } = customRender(
,
{ initialUrl: '/project/default/issues?q=function' }
)
await user.click(screen.getByRole('button', { name: 'Open issues menu' }))
expect(await screen.findByRole('dialog')).toBeInTheDocument()
await act(async () => {
await router.navigate({ href: '/project/default/issues?category=security' })
})
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
}
)
it('keeps the Issues menu open for filter updates and closes for an issue link', async () => {
vi.stubGlobal('innerWidth', 390)
const user = userEvent.setup()
const { router } = customRender(
,
{ initialUrl: '/project/default/issues' }
)
await user.click(screen.getByRole('button', { name: 'Open issues menu' }))
expect(await screen.findByRole('dialog', { name: 'Issues menu' })).toBeInTheDocument()
await act(async () => {
await router.navigate({ href: '/project/default/issues?q=function' })
})
expect(screen.getByRole('dialog', { name: 'Issues menu' })).toBeInTheDocument()
await act(async () => {
await router.navigate({ href: '/project/default/issues' })
})
expect(screen.getByRole('dialog', { name: 'Issues menu' })).toBeInTheDocument()
await act(async () => {
await router.navigate({ href: '/project/default/issues/function_search_path_mutable' })
})
await waitFor(() =>
expect(screen.queryByRole('dialog', { name: 'Issues menu' })).not.toBeInTheDocument()
)
})
})