// @vitest-environment jsdom import { cleanup, fireEvent, render, screen } from '@testing-library/react' import * as option1NavItems from '~/features/IaNav/option1.constants' import { useNavItems } from '~/features/IaNav/useNavItems' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { DocsSidebarNav, DocsSidebarNavProvider, DocsSidebarNavTop, DocsSidebarResources, } from './DocsSidebarNav' import { useDocsSidebarNav } from './DocsSidebarNavContext' import { MenuId } from './NavigationMenu' import * as currentNavItems from './NavigationMenu.constants' import { getListedMenuId } from './NavigationMenu.utils' const navigation = vi.hoisted(() => ({ pathname: '/guides/database/overview' })) vi.mock('~/features/IaNav/IaNavRouteProvider', () => ({ useDocsPathname: () => navigation.pathname, })) vi.mock('~/features/IaNav/useIaNavReady', () => ({ useIaNavReady: () => true })) vi.mock('~/features/IaNav/useNavItems', () => ({ useNavItems: vi.fn() })) // the guide tree imports the IaNav barrel, and the variant hook there loads browser-only modules vi.mock('~/features/IaNav/useIaNavVariant', () => ({ useIaNavVariant: () => 'control' })) vi.mock('next/navigation', () => ({ usePathname: () => navigation.pathname, })) const LevelDepth = () =>

{`depth ${useDocsSidebarNav().stack.length}`}

const sidebar = ( menuId = getListedMenuId({ pathname: navigation.pathname, navItems: vi.mocked(useNavItems)() }) ) => ( page tree ) const renderSidebar = (menuId?: MenuId) => render(sidebar(menuId)) describe('DocsSidebarNav', () => { afterEach(cleanup) beforeEach(() => { navigation.pathname = '/guides/database/overview' vi.mocked(useNavItems).mockReturnValue(currentNavItems) }) it('starts at the section list when no product holds the page', () => { navigation.pathname = '/' renderSidebar(MenuId.Contributing) expect(screen.getByText('depth 1')).toBeTruthy() expect(screen.queryByRole('navigation', { name: 'Sidebar levels' })).toBeNull() }) it('lists Reference with the other sections and opens it with a way back', () => { navigation.pathname = '/' vi.mocked(useNavItems).mockReturnValue(option1NavItems) renderSidebar(MenuId.Contributing) fireEvent.click(screen.getByRole('button', { name: 'Reference' })) expect(screen.getByRole('button', { name: 'Reference, back to Home' })).toBeTruthy() expect(screen.getByRole('link', { name: 'JavaScript' })).toBeTruthy() }) it('opens the levels of the page from the option-1 nav, with a getting started link its tree leaves out', () => { navigation.pathname = '/guides/local-development' vi.mocked(useNavItems).mockReturnValue(option1NavItems) renderSidebar() expect( screen.getByRole('button', { name: 'Local development and CLI, back to Develop and deploy' }) ).toBeTruthy() expect(screen.getByRole('link', { name: 'Getting started' }).getAttribute('href')).toBe( '/guides/local-development' ) }) it('picks the option-1 product whose link is the page when products share a path', () => { navigation.pathname = '/guides/platform/backups' vi.mocked(useNavItems).mockReturnValue(option1NavItems) renderSidebar() expect( screen.getByRole('button', { name: 'Backups and upgrades, back to Run and manage' }) ).toBeTruthy() }) it.each([ ['/guides/realtime/broadcast', 'Realtime, back to Products'], ['/guides/graphql/views', 'REST and GraphQL APIs, back to Products'], ['/guides/observability/cli-logs', 'Logs and monitoring, back to Run and manage'], ['/guides/observability/log-drains', 'Logs and monitoring, back to Run and manage'], ['/guides/deployment/terraform/tutorial', 'Terraform, back to Develop and deploy'], ])('opens the option-1 product whose tree holds %s', (pathname, backButtonName) => { navigation.pathname = pathname vi.mocked(useNavItems).mockReturnValue(option1NavItems) renderSidebar() expect(screen.getByRole('button', { name: backButtonName })).toBeTruthy() }) it.each([ ['/guides/platform/read-replicas', 'Database, back to Products'], ['/guides/api/rest/generating-types', 'Local development and CLI, back to Develop and deploy'], ])('opens %s in the option-1 section whose tree lists it', (pathname, backButtonName) => { navigation.pathname = pathname vi.mocked(useNavItems).mockReturnValue(option1NavItems) renderSidebar() expect(screen.getByRole('button', { name: backButtonName })).toBeTruthy() }) it('marks only the product of the page as current when products share a path', () => { navigation.pathname = '/guides/platform/backups' vi.mocked(useNavItems).mockReturnValue(option1NavItems) renderSidebar() fireEvent.click( screen.getByRole('button', { name: 'Backups and upgrades, back to Run and manage' }) ) const currentLinks = screen .getAllByRole('link') .filter((link) => link.getAttribute('aria-current') === 'page') expect(currentLinks.map((link) => link.textContent)).toEqual(['Backups and upgrades']) }) it('keeps the level when a modified click opens a product in another tab', () => { renderSidebar() fireEvent.click(screen.getByRole('button', { name: 'Database, back to Products' })) fireEvent.click(screen.getByRole('link', { name: 'Auth' }), { metaKey: true }) expect(screen.getByRole('button', { name: 'Products, back to Home' })).toBeTruthy() }) it('links the resources home when the tree lists only the glossary', () => { navigation.pathname = '/guides/resources/glossary' renderSidebar() expect(screen.getByRole('link', { name: 'Getting started' }).getAttribute('href')).toBe( '/guides/resources' ) }) it('names the resources level after its tree on the resources home', () => { navigation.pathname = '/guides/resources' renderSidebar() expect(screen.getByRole('button', { name: 'Resources, back to Home' })).toBeTruthy() }) it('opens the levels of the new page after browsing away from the last one', () => { const { rerender } = renderSidebar() fireEvent.click(screen.getByRole('button', { name: 'Database, back to Products' })) navigation.pathname = '/guides/auth' rerender(sidebar()) expect(screen.getByRole('button', { name: 'Auth, back to Products' })).toBeTruthy() }) it('adds no getting started link when the tree already lists the home page', () => { renderSidebar() expect(screen.queryByRole('link', { name: 'Getting started' })).toBeNull() }) it('goes back one level and names the level it goes to', () => { renderSidebar() fireEvent.click(screen.getByRole('button', { name: 'Database, back to Products' })) expect(screen.getByText('depth 2')).toBeTruthy() expect(screen.getByRole('button', { name: 'Products, back to Home' })).toBeTruthy() }) it('keeps keyboard focus in the sidebar when the focused back button goes away', () => { renderSidebar() fireEvent.click(screen.getByRole('button', { name: 'Database, back to Products' })) const backButton = screen.getByRole('button', { name: 'Products, back to Home' }) backButton.focus() fireEvent.click(backButton) expect(backButton.isConnected).toBe(false) expect(document.activeElement).not.toBe(document.body) }) it('links a product row to its overview and opens its pages', () => { renderSidebar() fireEvent.click(screen.getByRole('button', { name: 'Database, back to Products' })) const authRow = screen.getByRole('link', { name: 'Auth' }) fireEvent.click(authRow) expect(authRow.getAttribute('href')).toBe('/guides/auth') expect(screen.getByRole('link', { name: 'Password-based' })).toBeTruthy() }) it('opens reference pages on the reference list with a way back to the sections', () => { navigation.pathname = '/reference/javascript' renderSidebar() expect(screen.getByText('depth 2')).toBeTruthy() expect(screen.getByRole('link', { name: 'JavaScript' })).toBeTruthy() expect(screen.getByRole('button', { name: 'Reference, back to Home' })).toBeTruthy() }) it.each([ ['/reference/server/introduction', MenuId.RefServerV1, '/reference/server'], ['/reference/cli/supabase-login', MenuId.RefCli, '/reference/cli/introduction'], ])( 'marks the library of %s as current in the option-1 reference list', (pathname, menuId, libraryHref) => { navigation.pathname = pathname vi.mocked(useNavItems).mockReturnValue(option1NavItems) renderSidebar(menuId) const currentLinks = screen .getAllByRole('link') .filter((link) => link.hasAttribute('aria-current')) expect(currentLinks.map((link) => link.getAttribute('href'))).toEqual([libraryHref]) } ) it('opens a reference with its nav in the sidebar as a level under the reference list', () => { navigation.pathname = '/reference/api/introduction' vi.mocked(useNavItems).mockReturnValue(option1NavItems) renderSidebar(MenuId.RefApi) expect(screen.getByRole('button', { name: 'Management API, back to Reference' })).toBeTruthy() expect(screen.getByText('page tree')).toBeTruthy() expect(screen.queryByRole('link', { name: 'JavaScript' })).toBeNull() }) it('puts the knowledge base first in the footer and leaves out the changelog', () => { render() const footerLinks = screen.getAllByRole('link') expect(footerLinks[0].getAttribute('href')).toBe('https://supabase.com/kb') expect(footerLinks.map((link) => link.textContent)).not.toContain('Changelog') }) it.each([ ['/guides/resources', []], ['/guides/resources/glossary', ['Glossary']], ])('marks a footer link current only on its own page (%s)', (pathname, currentLabels) => { navigation.pathname = pathname render() const currentLinks = screen .getAllByRole('link') .filter((link) => link.getAttribute('aria-current') === 'page') expect(currentLinks.map((link) => link.textContent)).toEqual(currentLabels) }) })