import { screen, waitFor } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { HttpResponse } from 'msw' import { useEffect, useState } from 'react' import { CommandHeader, CommandMenuInput, CommandMenuList, CommandWrapper, useCommands, useSetCommandMenuOpen, useSetQuery, } from 'ui-patterns/CommandMenu' import { beforeEach, describe, expect, it, vi } from 'vitest' import { useDatabaseEntitySearchCommands } from './DatabaseEntitySearchCommands' import type { EntitySearchData } from '@/data/database/entity-search-query' import { customRender } from '@/tests/lib/custom-render' import { addAPIMock } from '@/tests/lib/msw' const { mockUseSelectedProjectQuery } = vi.hoisted(() => ({ mockUseSelectedProjectQuery: vi.fn(), })) vi.mock('@/hooks/misc/useSelectedProject', () => ({ useSelectedProjectQuery: mockUseSelectedProjectQuery, })) const SEARCH_RESULT: EntitySearchData = { tables: [ { id: 101, schema: 'public', name: 'blog_posts', rls_enabled: true, policy_count: 4 }, { id: 102, schema: 'blog', name: 'blog_posts_archive', rls_enabled: false, policy_count: 1 }, ], policies: [ { id: 201, name: 'Authors can edit their posts', command: 'UPDATE', schema: 'public', table: 'blog_posts', table_id: 101, }, { id: 202, name: 'Anyone can read archived posts', command: 'SELECT', schema: 'blog', table: 'blog_posts_archive', table_id: 102, }, ], } /** Renders the registered command sections as a plain list, driven by a text input. */ const CommandMenuHarness = () => { useDatabaseEntitySearchCommands() const setQuery = useSetQuery() const setOpen = useSetCommandMenuOpen() const sections = useCommands() const [value, setValue] = useState('') useEffect(() => setOpen(true), [setOpen]) return ( <> { setValue(event.target.value) setQuery(event.target.value) }} /> {sections.map((section) => (
{section.commands.map((command) => (
{command.name} {command.badge?.()} {'route' in command && {command.route}}
))}
))} ) } describe('useDatabaseEntitySearchCommands', () => { let searchRequests: string[] beforeEach(() => { searchRequests = [] mockUseSelectedProjectQuery.mockReturnValue({ data: { id: 1, ref: 'default', name: 'default', connectionString: 'encrypted' }, }) addAPIMock({ method: 'post', path: '/platform/pg-meta/:ref/query', response: async ({ request }) => { const { query } = (await request.json()) as { query: string } searchRequests.push(query) return HttpResponse.json([SEARCH_RESULT]) }, }) }) it('lists matching tables with their policy count and RLS status', async () => { customRender() await userEvent.type(screen.getByRole('textbox', { name: 'Search' }), 'blog_posts') const section = await screen.findByRole('region', { name: 'Tables' }) expect(section).toHaveTextContent('public.blog_posts') expect(section).toHaveTextContent('4 policies • RLS enabled') expect(section).toHaveTextContent('blog.blog_posts_archive') expect(section).toHaveTextContent('1 policy • RLS disabled') expect(section).toHaveTextContent('/project/default/editor/101?schema=public') }) it('lists matching policies with their command and table, linking to the policy editor', async () => { customRender() await userEvent.type(screen.getByRole('textbox', { name: 'Search' }), 'blog_posts') const section = await screen.findByRole('region', { name: 'Policies' }) expect(section).toHaveTextContent('Authors can edit their posts') expect(section).toHaveTextContent('UPDATE on public.blog_posts') // The policies page lists policies per schema, so the link has to carry it expect(section).toHaveTextContent('/project/default/database/policies?schema=public&edit=201') expect(section).toHaveTextContent('/project/default/database/policies?schema=blog&edit=202') }) it('debounces typing into a single database query', async () => { customRender() await userEvent.type(screen.getByRole('textbox', { name: 'Search' }), 'blog_posts') await screen.findByRole('region', { name: 'Tables' }) expect(searchRequests).toHaveLength(1) }) it('does not query the database for short searches', async () => { customRender() await userEvent.type(screen.getByRole('textbox', { name: 'Search' }), 'bl') // Wait past the debounce window before asserting nothing was sent await new Promise((resolve) => setTimeout(resolve, 500)) expect(searchRequests).toHaveLength(0) expect(screen.queryByRole('region', { name: 'Tables' })).not.toBeInTheDocument() expect(screen.queryByRole('region', { name: 'Policies' })).not.toBeInTheDocument() }) it('does not show results from a previous project while searching another', async () => { let project = { id: 1, ref: 'default', name: 'default', connectionString: 'encrypted' } mockUseSelectedProjectQuery.mockImplementation(() => ({ data: project })) let resolveSecondSearch: (() => void) | undefined addAPIMock({ method: 'post', path: '/platform/pg-meta/:ref/query', response: async ({ params }) => { // Hold the second project's search in flight until the test releases it if (params.ref === 'other') { await new Promise((resolve) => (resolveSecondSearch = resolve)) } return HttpResponse.json([SEARCH_RESULT]) }, }) const { rerender } = customRender() const input = screen.getByRole('textbox', { name: 'Search' }) await userEvent.type(input, 'blog') await screen.findByRole('region', { name: 'Tables' }) project = { id: 2, ref: 'other', name: 'other', connectionString: 'encrypted' } rerender() await userEvent.type(input, '_posts') await waitFor(() => expect(resolveSecondSearch).toBeDefined()) expect(screen.queryByRole('region', { name: 'Tables' })).not.toBeInTheDocument() resolveSecondSearch?.() const section = await screen.findByRole('region', { name: 'Tables' }) expect(section).toHaveTextContent('/project/other/editor/101?schema=public') }) }) /** Renders the real command menu list, to check how results interact with its empty state. */ const CommandMenuListHarness = () => { useDatabaseEntitySearchCommands() const setOpen = useSetCommandMenuOpen() useEffect(() => setOpen(true), [setOpen]) return ( ) } describe('useDatabaseEntitySearchCommands in the command menu list', () => { beforeEach(() => { mockUseSelectedProjectQuery.mockReturnValue({ data: { id: 1, ref: 'default', name: 'default', connectionString: 'encrypted' }, }) }) it('hides the empty state once table results arrive', async () => { addAPIMock({ method: 'post', path: '/platform/pg-meta/:ref/query', response: () => HttpResponse.json([SEARCH_RESULT]), }) customRender() await userEvent.type(screen.getByRole('combobox'), 'blog_posts') expect(await screen.findByText('4 policies • RLS enabled')).toBeInTheDocument() await waitFor(() => expect(screen.queryByText('No results found.')).not.toBeInTheDocument()) }) it('lists tables above policies even when a policy matches the search better', async () => { addAPIMock({ method: 'post', path: '/platform/pg-meta/:ref/query', response: () => HttpResponse.json([ { tables: [SEARCH_RESULT.tables[0]], policies: [{ ...SEARCH_RESULT.policies[0], name: 'blog_posts_owner_update' }], }, ]), }) customRender() await userEvent.type(screen.getByRole('combobox'), 'blog_posts') await screen.findByText('UPDATE on public.blog_posts') const [first, second] = screen.getAllByText(/^(Tables|Policies)$/) expect(first).toHaveTextContent('Tables') expect(second).toHaveTextContent('Policies') }) })