mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
Part 2 of a 6-PR stack migrating all `next/link` imports in `apps/studio` onto TanStack Router's `Link` (or plain `<a>` for known external URLs) and removing `compat/next/link.tsx`. | Part | PR | Scope | | --- | --- | --- | | 1 | #54304 | `data/prefetchers/PrefetchableLink.tsx` and `components/ui/**` | | **2** | **#54305 ← this PR** | **`components/layouts/**` and top-level shell components** | | 3 | #54306 | Database, TableGridEditor, Storage, SQLEditor, BranchManagement, Branching, Functions, Realtime | | 4 | #54307 | Integrations, Settings, Observability, LogDrains, Issues, Linter, Reports, QueryPerformance, QuerySources, ConfigDrift, DiskManagement, Compute | | 5 | #54308 | Remaining `components/interfaces/**` (Org, Billing, Auth, Support, Home, SignIn, ConnectSheet, etc.) | | 6 | #54309 | Remove `compat/next/link.tsx` shim, Vite alias, and update basePath tests/docs | Migrates all `apps/studio/components/layouts/**` files and top-level navigation shell components off `next/link`. **Changed:** - Replaced `next/link` across `components/layouts/**` (`AccountLayout`, `AppLayout`, `AuthLayout`, `BranchLayout`, `DatabaseLayout`, `EdgeFunctionsLayout`, `ExplorerLayout`, `IssuesLayout`, `LogsLayout`, `Navigation`, `ObservabilityLayout`, `ProjectLayout`, `ProjectNeedsSecuring`, `SQLEditorLayout`, `SignInLayout`, `TableEditorLayout`, `Tabs`) and top-level shell components (`Sidebar.tsx`, `UserDropdown.tsx`, `LocalDropdown.tsx`, `HomePageActions.tsx`) - Removed `vi.mock('next/link', ...)` from `LocalDropdown.test.tsx` and `ProjectNeedsSecuring.test.tsx` - Added accessible labels where external icon/logo links switched from `<Link>` to plain `<a>` (`AdminStudioButton.tsx`, `ForgotPasswordLayout.tsx`) Addresses [FE-5108](https://linear.app/supabase/issue/FE-5108/replace-the-nextlink-shim-with-tanstack-link) ## To test - [ ] Navigate across top header, left sidebar, user/local dropdown, and project/org/branch switchers - [ ] Verify mobile navigation menu items and recent-tab links navigate with expected query params - [ ] Verify sign-in and forgot-password layout links work cleanly --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
243 lines
6.9 KiB
TypeScript
243 lines
6.9 KiB
TypeScript
import { screen } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import type { ReactElement, ReactNode } from 'react'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { LocalDropdown } from './LocalDropdown'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
|
|
const {
|
|
mockSetTheme,
|
|
mockSetLastRoute,
|
|
mockToggleFeaturePreviewModal,
|
|
mockEnableToolbar,
|
|
mockDismissDevToolbar,
|
|
mockSetDevToolbarOpen,
|
|
mockUseDevToolbar,
|
|
} = vi.hoisted(() => ({
|
|
mockSetTheme: vi.fn(),
|
|
mockSetLastRoute: vi.fn(),
|
|
mockToggleFeaturePreviewModal: vi.fn(),
|
|
mockEnableToolbar: vi.fn(),
|
|
mockDismissDevToolbar: vi.fn(),
|
|
mockSetDevToolbarOpen: vi.fn(),
|
|
mockUseDevToolbar: vi.fn(() => ({
|
|
isAvailable: false,
|
|
isEnabled: false,
|
|
isOpen: false,
|
|
setIsOpen: mockSetDevToolbarOpen,
|
|
enableToolbar: mockEnableToolbar,
|
|
dismissToolbar: mockDismissDevToolbar,
|
|
events: [],
|
|
setEvents: vi.fn(),
|
|
})),
|
|
}))
|
|
|
|
const renderLocalDropdown = (initialUrl = '/project/default/editor') =>
|
|
customRender(<LocalDropdown />, { initialUrl })
|
|
|
|
vi.mock('next-themes', () => ({
|
|
useTheme: () => ({
|
|
theme: 'dark',
|
|
setTheme: mockSetTheme,
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/state/app-state', () => ({
|
|
useAppStateSnapshot: () => ({
|
|
setLastRouteBeforeVisitingAccountPage: mockSetLastRoute,
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/components/ui/ProfileImage', () => ({
|
|
ProfileImage: () => <div>Avatar</div>,
|
|
}))
|
|
|
|
vi.mock('./App/FeaturePreview/FeaturePreviewContext', () => ({
|
|
useFeaturePreviewModal: () => ({
|
|
toggleFeaturePreviewModal: mockToggleFeaturePreviewModal,
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/lib/telemetry/track', () => ({ useTrack: () => vi.fn() }))
|
|
|
|
vi.mock('dev-tools', () => ({
|
|
useDevToolbar: () => mockUseDevToolbar(),
|
|
}))
|
|
|
|
vi.mock('ui', async (importOriginal) => {
|
|
const React = await import('react')
|
|
|
|
return {
|
|
...(await importOriginal<typeof import('ui')>()),
|
|
Button: ({
|
|
children,
|
|
...props
|
|
}: React.ButtonHTMLAttributes<HTMLButtonElement> & { children?: ReactNode }) => (
|
|
<button tabIndex={0} {...props}>
|
|
{children}
|
|
</button>
|
|
),
|
|
cn: (...classes: Array<string | false | null | undefined>) => classes.filter(Boolean).join(' '),
|
|
DropdownMenu: ({ children }: { children: ReactNode }) => <div>{children}</div>,
|
|
DropdownMenuTrigger: ({ children }: { children: ReactNode }) => <div>{children}</div>,
|
|
DropdownMenuContent: ({ children }: { children: ReactNode }) => <div>{children}</div>,
|
|
DropdownMenuGroup: ({ children }: { children: ReactNode }) => <div>{children}</div>,
|
|
DropdownMenuItem: ({
|
|
children,
|
|
asChild,
|
|
onClick,
|
|
onSelect,
|
|
}: {
|
|
children: ReactNode
|
|
asChild?: boolean
|
|
onClick?: () => void
|
|
onSelect?: () => void
|
|
}) =>
|
|
asChild ? (
|
|
<div>{children}</div>
|
|
) : (
|
|
<button
|
|
tabIndex={0}
|
|
onClick={() => {
|
|
onClick?.()
|
|
onSelect?.()
|
|
}}
|
|
>
|
|
{children}
|
|
</button>
|
|
),
|
|
DropdownMenuLabel: ({ children }: { children: ReactNode }) => <div>{children}</div>,
|
|
DropdownMenuCheckboxItem: ({
|
|
children,
|
|
checked,
|
|
onCheckedChange,
|
|
}: {
|
|
children: ReactNode
|
|
checked?: boolean
|
|
onCheckedChange?: (checked: boolean) => void
|
|
}) => (
|
|
<button tabIndex={0} onClick={() => onCheckedChange?.(!checked)}>
|
|
{children}
|
|
</button>
|
|
),
|
|
DropdownMenuSeparator: () => <hr />,
|
|
DropdownMenuRadioGroup: ({
|
|
children,
|
|
onValueChange,
|
|
}: {
|
|
children: ReactNode
|
|
onValueChange: (value: string) => void
|
|
}) => (
|
|
<div>
|
|
{React.Children.map(children, (child: ReactNode) =>
|
|
React.isValidElement<{ value: string; onClick?: () => void }>(child)
|
|
? React.cloneElement(child, {
|
|
onClick: () => onValueChange(child.props.value),
|
|
})
|
|
: (child as ReactElement)
|
|
)}
|
|
</div>
|
|
),
|
|
DropdownMenuRadioItem: ({
|
|
children,
|
|
onClick,
|
|
}: {
|
|
children: ReactNode
|
|
onClick?: () => void
|
|
}) => (
|
|
<button tabIndex={0} onClick={onClick}>
|
|
{children}
|
|
</button>
|
|
),
|
|
Tooltip: ({ children }: { children: ReactNode }) => <div>{children}</div>,
|
|
TooltipContent: ({ children }: { children: ReactNode }) => <div>{children}</div>,
|
|
TooltipTrigger: ({ children }: { children: ReactNode }) => <div>{children}</div>,
|
|
singleThemes: [
|
|
{ value: 'dark', name: 'Dark' },
|
|
{ value: 'light', name: 'Light' },
|
|
],
|
|
}
|
|
})
|
|
|
|
describe('LocalDropdown', () => {
|
|
beforeEach(() => {
|
|
mockUseDevToolbar.mockReturnValue({
|
|
isAvailable: false,
|
|
isEnabled: false,
|
|
isOpen: false,
|
|
setIsOpen: mockSetDevToolbarOpen,
|
|
enableToolbar: mockEnableToolbar,
|
|
dismissToolbar: mockDismissDevToolbar,
|
|
events: [],
|
|
setEvents: vi.fn(),
|
|
})
|
|
})
|
|
|
|
it('shows Preferences, removes Command menu, and keeps theme controls wired', async () => {
|
|
const user = userEvent.setup()
|
|
|
|
renderLocalDropdown('/project/default/editor?schema=public#keys')
|
|
|
|
expect(screen.getByText('Preferences')).toBeInTheDocument()
|
|
expect(screen.queryByText('Command menu')).not.toBeInTheDocument()
|
|
expect(screen.getByText('Theme')).toBeInTheDocument()
|
|
expect(screen.queryByText('Dev toolbar')).not.toBeInTheDocument()
|
|
|
|
await user.click(screen.getByText('Preferences'))
|
|
expect(mockSetLastRoute).toHaveBeenCalledWith('/project/default/editor?schema=public#keys')
|
|
|
|
await user.click(screen.getByText('Feature previews'))
|
|
expect(mockToggleFeaturePreviewModal).toHaveBeenCalledWith(true)
|
|
|
|
await user.click(screen.getByText('Light'))
|
|
expect(mockSetTheme).toHaveBeenCalledWith('light')
|
|
})
|
|
|
|
it('toggles Dev toolbar visibility from the menu', async () => {
|
|
mockUseDevToolbar.mockReturnValue({
|
|
isAvailable: true,
|
|
isEnabled: false,
|
|
isOpen: false,
|
|
setIsOpen: mockSetDevToolbarOpen,
|
|
enableToolbar: mockEnableToolbar,
|
|
dismissToolbar: mockDismissDevToolbar,
|
|
events: [],
|
|
setEvents: vi.fn(),
|
|
})
|
|
|
|
const user = userEvent.setup()
|
|
|
|
renderLocalDropdown()
|
|
|
|
expect(screen.getByText('Local tools')).toBeInTheDocument()
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Dev toolbar' }))
|
|
|
|
expect(mockEnableToolbar).toHaveBeenCalled()
|
|
expect(mockDismissDevToolbar).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('hides Dev toolbar from the menu when toggled off', async () => {
|
|
mockUseDevToolbar.mockReturnValue({
|
|
isAvailable: true,
|
|
isEnabled: true,
|
|
isOpen: false,
|
|
setIsOpen: mockSetDevToolbarOpen,
|
|
enableToolbar: mockEnableToolbar,
|
|
dismissToolbar: mockDismissDevToolbar,
|
|
events: [],
|
|
setEvents: vi.fn(),
|
|
})
|
|
|
|
const user = userEvent.setup()
|
|
|
|
renderLocalDropdown()
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Dev toolbar' }))
|
|
|
|
expect(mockDismissDevToolbar).toHaveBeenCalled()
|
|
expect(mockEnableToolbar).not.toHaveBeenCalled()
|
|
})
|
|
})
|