Files
supabase/apps/studio/components/layouts/ObservabilityLayout/ObservabilityMenu.utils.test.tsx
Alaister YoungandAlaister Young 57d40e4705 [FE-5109] (part 3) refactor(studio): migrate layout components off next/router (#54055)
Part 3 of a 6-PR stack migrating all `next/router` and
`next/compat/router` imports in `apps/studio` onto TanStack Router hooks
and removing the router shims.

| Part | PR | Scope |
| --- | --- | --- |
| 1 | #54053 | Core navigation helpers, `hooks/**`, `lib/**`,
`data/prefetchers/**` |
| 2 | #54054 | `components/ui/**` |
| **3** | **#54055 ← this PR** | **`components/layouts/**` and
`state/tabs.tsx`** |
| 4 | #54056 | Database, Branching, Compute, Edge Functions, Explorer,
SQL Editor, Storage, Table Editor |
| 5 | #54057 | Remaining `components/interfaces/**` and
`QueryInsightsTable` router events |
| 6 | #54058 | Remove `next/router` & `next/compat/router` shims,
`_router-events.ts`, Vite aliases, and test mocks |

Migrates all `apps/studio/components/layouts/**` files and the studio
tab router integration off `next/router`.

**Added:**

- `useTabRouter()` in `apps/studio/state/tabs.tsx` so tab state actions
navigate via TanStack's `useNavigate` + `navigateToUrl` instead of
taking a `NextRouter` instance

**Changed:**

- Replaced `useRouter` across all layout directories (`AccountLayout`,
`AdvisorsLayout`, `AppLayout`, `AuthLayout`, `BranchLayout`,
`ComputeLayout`, `DatabaseLayout`, `DefaultLayout`,
`EdgeFunctionsLayout`, `ExplorerLayout`, `LogsLayout`, `Navigation/*`,
`ObservabilityLayout`, `OrganizationLayout`, `ProjectLayout`,
`ProjectNeedsSecuring`, `ProjectSettingsLayout`, `RealtimeLayout`,
`SQLEditorLayout`, `SignInLayout`, and `Tabs`) with TanStack Router
hooks (`useNavigate`, `useLocation`, `useRoutePattern`) and `useParams`
from `common`
- Updated `ProjectDropdown.utils.ts` and `ProjectDropdown.test.ts` to
work with TanStack `$param` route templates
- Updated layout component tests (`AccountLayout.selfhosted.test.tsx`,
`AdvisorsNavigationShortcuts.test.tsx`, `BranchLink.test.tsx`,
`ExplorerLayout.test.tsx`, `ExplorerNavChats.test.tsx`,
`ObservabilityMenu.utils.test.tsx`,
`MobileIssuesNavigation.platform.test.tsx`,
`ProjectLayout/index.test.tsx`, `ProjectNeedsSecuring.test.tsx`,
`ProjectDropdown.test.tsx`)

**Simplified:**

- `OrganizationLayout` and `SettingsLayout` match against
`useRoutePattern()` instead of the real pathname. An org slug ending in
a settings segment (e.g. `acmeapps`) no longer gets the settings title.
- `ProjectSettings.Commands` opens the Connect sheet through nuqs, the
same way `Connect.Commands` does
- `OrgMenuContent`, `OrganizationSettingsMenu` and `useTabRouter` read
`orgSlug`/`schema` from `useParams` instead of casting `location.search`
- Removed the dead `getPathnameWithoutQuery` helpers and their tests
- `usePreservedQueryParams` builds its string in the `useLocation`
selector (no `useMemo`), and `UnhealthyState` only navigates when `ref`
is set
- Moved `MobileIssuesNavigation.platform.test.tsx` onto a TanStack test
router. It now covers the active-route logic again.
- Tests use the shared `createTestRouter` and `customRender` router

Addresses
[FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks)

## To test

- [ ] Switching projects, organizations, and branches from the top
header dropdowns preserves the current sub-route where supported
- [ ] Opening, switching, and closing Table Editor / SQL Editor tabs
updates the URL and active tab cleanly
- [ ] Active sidebar item highlighting works across Database, Auth, Edge
Functions, Observability, Logs, Realtime, Explorer, and Project Settings
layouts
- [ ] Mobile navigation menu and floating toolbar sheets open and
navigate properly

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-12-01 14:30:25 +08:00

291 lines
10 KiB
TypeScript

import { useFlag, useParams } from 'common'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import {
useGenerateCustomReportsMenu,
useGenerateObservabilityMenu,
} from './ObservabilityMenu.utils'
import { useSupamonitorStatus } from '@/components/interfaces/QueryPerformance/hooks/useSupamonitorStatus'
import { useContentQuery } from '@/data/content/content-query'
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
import { useIsWarehouseEnabled } from '@/hooks/misc/useIsWarehouseEnabled'
import { customRenderHook as renderHook } from '@/tests/lib/custom-render'
const { REF, mockIsPlatform } = vi.hoisted(() => ({
REF: 'project-ref',
mockIsPlatform: { value: true },
}))
vi.mock('@/lib/constants', async () => {
const actual = await vi.importActual<Record<string, unknown>>('@/lib/constants')
return {
...actual,
get IS_PLATFORM() {
return mockIsPlatform.value
},
}
})
vi.mock('common', async (importOriginal) => {
const actual = await importOriginal<typeof import('common')>()
return {
...actual,
useFlag: vi.fn().mockReturnValue(false),
useParams: vi.fn().mockReturnValue({ ref: REF }),
}
})
vi.mock('@/components/interfaces/QueryPerformance/hooks/useSupamonitorStatus', () => ({
useSupamonitorStatus: vi.fn(),
}))
vi.mock('@/hooks/misc/useIsFeatureEnabled', () => ({
useIsFeatureEnabled: vi.fn(),
}))
vi.mock('@/data/content/content-query', () => ({
useContentQuery: vi.fn(),
}))
vi.mock('@/hooks/misc/useIsWarehouseEnabled', () => ({
useIsWarehouseEnabled: vi.fn().mockReturnValue(false),
}))
describe('useGenerateObservabilityMenu', () => {
it.each([true, false])('shows Warehouse only with access: %s', (enabled) => {
vi.mocked(useIsWarehouseEnabled).mockReturnValue(enabled)
const { result } = renderHook(() => useGenerateObservabilityMenu())
expect(
result.current.flatMap((section) => section.items).some((item) => item.key === 'warehouse')
).toBe(enabled)
})
beforeEach(() => {
vi.mocked(useIsWarehouseEnabled).mockReturnValue(false)
mockIsPlatform.value = true
vi.mocked(useFlag).mockReturnValue(false)
vi.mocked(useParams).mockReturnValue({ ref: REF })
vi.mocked(useSupamonitorStatus).mockReturnValue({
isSupamonitorEnabled: false,
isLoading: false,
})
vi.mocked(useIsFeatureEnabled).mockReturnValue(true)
})
const getGeneralKeys = () => {
const { result } = renderHook(() => useGenerateObservabilityMenu())
return result.current
.find((section) => section.title === 'GENERAL')
?.items.map((item) => item.key)
}
it('includes Overview only when the observabilityOverview flag is enabled', () => {
vi.mocked(useFlag).mockReturnValue(false)
expect(getGeneralKeys()).not.toContain('observability')
vi.mocked(useFlag).mockReturnValue(true)
expect(getGeneralKeys()).toContain('observability')
})
it('shows Query Performance or Query Insights depending on supamonitor', () => {
vi.mocked(useSupamonitorStatus).mockReturnValue({
isSupamonitorEnabled: false,
isLoading: false,
})
expect(getGeneralKeys()).toContain('query-performance')
expect(getGeneralKeys()).not.toContain('query-insights')
vi.mocked(useSupamonitorStatus).mockReturnValue({
isSupamonitorEnabled: true,
isLoading: false,
})
expect(getGeneralKeys()).toContain('query-insights')
expect(getGeneralKeys()).not.toContain('query-performance')
})
it('includes API Gateway and the PRODUCT section on platform', () => {
vi.mocked(useIsWarehouseEnabled).mockReturnValue(false)
mockIsPlatform.value = true
const { result } = renderHook(() => useGenerateObservabilityMenu())
const general = result.current.find((section) => section.title === 'GENERAL')
expect(general?.items.some((item) => item.key === 'api-overview')).toBe(true)
expect(result.current.some((section) => section.title === 'PRODUCT')).toBe(true)
})
it('excludes API Gateway and the PRODUCT section in self-hosted mode', () => {
mockIsPlatform.value = false
const { result } = renderHook(() => useGenerateObservabilityMenu())
const general = result.current.find((section) => section.title === 'GENERAL')
expect(general?.items.some((item) => item.key === 'api-overview')).toBe(false)
expect(result.current.some((section) => section.title === 'PRODUCT')).toBe(false)
expect(result.current.length).toBe(1)
})
it('includes Storage in the PRODUCT section only when project_storage:all is enabled', () => {
const getProductKeys = () => {
const { result } = renderHook(() => useGenerateObservabilityMenu())
return result.current
.find((section) => section.title === 'PRODUCT')
?.items.map((item) => item.key)
}
vi.mocked(useIsFeatureEnabled).mockReturnValue(true)
expect(getProductKeys()).toContain('storage')
expect(useIsFeatureEnabled).toHaveBeenCalledWith('project_storage:all')
vi.mocked(useIsFeatureEnabled).mockReturnValue(false)
expect(getProductKeys()).not.toContain('storage')
})
it('always includes Database, Data API, Auth, Edge Functions, and Realtime in the PRODUCT section', () => {
const { result } = renderHook(() => useGenerateObservabilityMenu())
const product = result.current.find((section) => section.title === 'PRODUCT')
expect(product?.items.map((item) => item.key)).toEqual(
expect.arrayContaining(['database', 'postgrest', 'auth', 'edge-functions', 'realtime'])
)
})
it('builds menu item URLs using the project ref', () => {
const { result } = renderHook(() => useGenerateObservabilityMenu())
const product = result.current.find((section) => section.title === 'PRODUCT')
const database = product?.items.find((item) => item.key === 'database')
expect(database?.url).toBe(`/project/${REF}/observability/database`)
})
it('preserves date range and helper query params across menu item URLs', () => {
const { result } = renderHook(() => useGenerateObservabilityMenu(), {
initialUrl: `/project/${REF}/observability?its=2024-01-01&ite=2024-01-31&isHelper=true&helperText=hello`,
})
const product = result.current.find((section) => section.title === 'PRODUCT')
const database = product?.items.find((item) => item.key === 'database')
expect(database?.url).toBe(
`/project/${REF}/observability/database?its=2024-01-01&ite=2024-01-31&isHelper=true&helperText=hello`
)
})
it('ignores query param values that are not plain strings (e.g. duplicated keys)', () => {
const { result } = renderHook(() => useGenerateObservabilityMenu(), {
initialUrl: `/project/${REF}/observability?its=2024-01-01&its=2024-02-01`,
})
const product = result.current.find((section) => section.title === 'PRODUCT')
const database = product?.items.find((item) => item.key === 'database')
expect(database?.url).toBe(`/project/${REF}/observability/database`)
})
})
describe('useGenerateCustomReportsMenu', () => {
const buildReport = (overrides: Record<string, unknown> = {}) => ({
id: 'report-1',
name: 'Report',
description: '',
type: 'report',
content: {},
...overrides,
})
it('returns an empty array and reports loading while content is pending', () => {
vi.mocked(useContentQuery).mockReturnValue({ data: undefined, isPending: true } as any)
const { result } = renderHook(() => useGenerateCustomReportsMenu())
expect(result.current.isLoading).toBe(true)
expect(result.current.data).toEqual([])
})
it('returns an empty array when there is no content', () => {
vi.mocked(useContentQuery).mockReturnValue({ data: undefined, isPending: false } as any)
const { result } = renderHook(() => useGenerateCustomReportsMenu())
expect(result.current.isLoading).toBe(false)
expect(result.current.data).toEqual([])
})
it('filters out non-report content', () => {
vi.mocked(useContentQuery).mockReturnValue({
data: {
cursor: null,
content: [
buildReport({ id: 'r1', name: 'A Report' }),
{ id: 'sql-1', name: 'A snippet', description: '', type: 'sql', content: {} },
],
},
isPending: false,
} as any)
const { result } = renderHook(() => useGenerateCustomReportsMenu())
expect(result.current.data).toHaveLength(1)
expect(result.current.data[0].id).toBe('r1')
})
it('sorts reports alphabetically by name', () => {
vi.mocked(useContentQuery).mockReturnValue({
data: {
cursor: null,
content: [
buildReport({ id: 'r2', name: 'Zebra' }),
buildReport({ id: 'r1', name: 'Apple' }),
],
},
isPending: false,
} as any)
const { result } = renderHook(() => useGenerateCustomReportsMenu())
expect(result.current.data.map((item) => item.name)).toEqual(['Apple', 'Zebra'])
})
it('builds report URLs preserving query params and defaults a missing description to an empty string', () => {
vi.mocked(useContentQuery).mockReturnValue({
data: {
cursor: null,
content: [buildReport({ id: 'r1', name: 'Report', description: undefined })],
},
isPending: false,
} as any)
const { result } = renderHook(() => useGenerateCustomReportsMenu(), {
initialUrl: `/project/${REF}/observability?its=2024-01-01`,
})
expect(result.current.data[0].url).toBe(`/project/${REF}/observability/r1?its=2024-01-01`)
expect(result.current.data[0].description).toBe('')
})
it('falls back to an index-based key when the report id is missing', () => {
vi.mocked(useContentQuery).mockReturnValue({
data: {
cursor: null,
content: [buildReport({ id: undefined, name: 'Report' })],
},
isPending: false,
} as any)
const { result } = renderHook(() => useGenerateCustomReportsMenu())
expect(result.current.data[0].key).toBe('0-report')
})
it('marks every report item as having dropdown actions and carries the raw report', () => {
const report = buildReport({ id: 'r1', name: 'Report' })
vi.mocked(useContentQuery).mockReturnValue({
data: { cursor: null, content: [report] },
isPending: false,
} as any)
const { result } = renderHook(() => useGenerateCustomReportsMenu())
expect(result.current.data[0].hasDropdownActions).toBe(true)
expect(result.current.data[0].report).toEqual(report)
})
})