import {screen, within} from '@testing-library/react' import userEvent from '@testing-library/user-event' import {beforeEach, describe, expect, it, vi} from 'vitest' import {ReleaseContext, SchemeContext} from '../context' import {TaxonomyPortProvider} from '../seams/TaxonomyPortContext' import {createFakeDataPort} from '../test/FakeDataPort' import {renderWithUi} from '../test/renderWithUi' import type {ConceptSchemeDocument, DocumentConcepts} from '../types' import {Hierarchy} from './Hierarchy' const mocks = vi.hoisted(() => ({usePaneRouter: vi.fn()})) vi.mock('sanity/structure', async (importOriginal) => ({ ...(await importOriginal()), usePaneRouter: mocks.usePaneRouter, })) beforeEach(() => { vi.clearAllMocks() mocks.usePaneRouter.mockReturnValue({routerPanesState: [[{id: 'scheme-1'}]], groupIndex: 0}) }) const scheme = { displayed: {_id: 'scheme-1', _type: 'skosConceptScheme', title: 'Test Scheme'}, } as unknown as ConceptSchemeDocument /** * Animals ─┬─ Birds ── Falcons * └─ Mammals */ const tree = (): DocumentConcepts => ({ topConcepts: [ { id: 'animals', prefLabel: 'Animals', childConcepts: [ { id: 'birds', prefLabel: 'Birds', level: 1, childConcepts: [{id: 'falcons', prefLabel: 'Falcons', level: 2}], }, {id: 'mammals', prefLabel: 'Mammals', level: 1}, ], }, ], concepts: [], }) /** * Studio sets `displayed._id` from the selected perspective, so a scheme open * in a release carries the version id — before any version document exists. */ const schemeInRelease = (release: string) => ({ displayed: { _id: `versions.${release}.scheme-1`, _type: 'skosConceptScheme', title: 'Test Scheme', }, }) as unknown as ConceptSchemeDocument function renderHierarchy( port: ReturnType, releaseContext: string | undefined = undefined, schemeDocument: ConceptSchemeDocument = scheme, ) { return renderWithUi( , ) } /** Open the row menu for a concept and choose Move to…, returning the dialog. */ async function openMoveDialog(user: ReturnType, label: string) { await user.click(screen.getByRole('button', {name: `More actions for ${label}`})) await user.click(await screen.findByText('Move to…')) return screen.getByRole('dialog') } /** * A menu's items are not in the DOM until it is opened, and once opened they sit * in a container @sanity/ui leaves hidden without a layout engine to position * against. So presence after opening is the signal here, not visibility. */ async function openRowMenu(user: ReturnType, label: string) { await user.click(screen.getByRole('button', {name: `More actions for ${label}`})) } describe('the move command, where it is offered', () => { it('offers a move on a nested concept', () => { renderHierarchy(createFakeDataPort({tree: tree()})) expect(screen.getByRole('button', {name: 'More actions for Falcons'})).toBeInTheDocument() }) it('gives a top concept a menu, but no move — it has no broader term to leave', async () => { const user = userEvent.setup() renderHierarchy(createFakeDataPort({tree: tree()})) await openRowMenu(user, 'Animals') expect(await screen.findByText('Remove from scheme')).toBeInTheDocument() expect(screen.queryByText('Move to…')).not.toBeInTheDocument() }) it('does not offer one in the published perspective', () => { renderHierarchy(createFakeDataPort({tree: tree()}), 'published') expect(screen.queryByRole('button', {name: 'More actions for Falcons'})).not.toBeInTheDocument() }) it('offers move inside a release, which the edit action can now target', async () => { // `EditAction` names a release version through `versionId`, creating it // from the published document — so a move is no longer drafts-only. const user = userEvent.setup() renderHierarchy(createFakeDataPort({tree: tree()}), 'rSummerDrop') await openRowMenu(user, 'Falcons') expect(await screen.findByText('Remove from scheme')).toBeInTheDocument() expect(screen.getByText('Move to…')).toBeInTheDocument() }) }) describe('the move command, choosing a destination', () => { it('routes a chosen destination through the port as a move plan', async () => { const user = userEvent.setup() const port = createFakeDataPort({tree: tree()}) renderHierarchy(port) const dialog = await openMoveDialog(user, 'Falcons') await user.click(within(dialog).getByRole('button', {name: /^Mammals/})) expect(port.appliedPlans).toHaveLength(1) expect(port.appliedPlans[0]).toMatchObject({ kind: 'move', concept: {publishedId: 'falcons'}, unsetBroader: 'broader[_ref=="birds"]', insertBroader: {_ref: 'mammals', _type: 'reference'}, }) }) it('targets the release version of the concept when the scheme is open in one', async () => { // The affordance showing is not the point — this is the write that used to // be refused, and the document it now names. const user = userEvent.setup() const port = createFakeDataPort({tree: tree()}) renderHierarchy(port, 'rSummerDrop', schemeInRelease('rSummerDrop')) const dialog = await openMoveDialog(user, 'Falcons') await user.click(within(dialog).getByRole('button', {name: /^Mammals/})) expect(port.appliedPlans).toHaveLength(1) expect(port.appliedPlans[0]).toMatchObject({ kind: 'move', concept: {publishedId: 'falcons', targetId: 'versions.rSummerDrop.falcons'}, insertBroader: {_ref: 'mammals'}, }) }) it('re-parents the tree optimistically, before the listener catches up', async () => { const user = userEvent.setup() // The fake's watch data stays static, standing in for the refetch lag, so // anything that moves on screen moved because of the optimistic layer. renderHierarchy(createFakeDataPort({tree: tree()})) const dialog = await openMoveDialog(user, 'Falcons') await user.click(within(dialog).getByRole('button', {name: /^Mammals/})) const mammalsRow = screen.getByRole('button', {name: /^Mammals/}).closest('.open') expect(mammalsRow).toContainElement(screen.getByRole('button', {name: /^Falcons/})) }) it('explains a destination the hierarchy cannot take, and applies nothing', async () => { const user = userEvent.setup() const port = createFakeDataPort({tree: tree()}) renderHierarchy(port) const dialog = await openMoveDialog(user, 'Falcons') // Birds is where Falcons already sits. await user.click(within(dialog).getByRole('button', {name: /^Birds/})) expect(await within(dialog).findByText('This concept is already here.')).toBeVisible() expect(port.appliedPlans).toHaveLength(0) expect(screen.getByRole('dialog')).toBeInTheDocument() }) it('refuses a concept dropped onto itself', async () => { const user = userEvent.setup() const port = createFakeDataPort({tree: tree()}) renderHierarchy(port) const dialog = await openMoveDialog(user, 'Falcons') await user.click(within(dialog).getByRole('button', {name: /^Falcons/})) expect( await within(dialog).findByText('A concept cannot be its own broader term.'), ).toBeVisible() expect(port.appliedPlans).toHaveLength(0) }) it('drops the broader term when asked, with nothing to insert', async () => { const user = userEvent.setup() const port = createFakeDataPort({tree: tree()}) renderHierarchy(port) const dialog = await openMoveDialog(user, 'Falcons') await user.click(within(dialog).getByRole('button', {name: /as a broader term/})) expect(port.appliedPlans[0]).toMatchObject({ kind: 'move', unsetBroader: 'broader[_ref=="birds"]', insertBroader: null, }) }) }) /** The same scheme, plus an orphan: a member with no broader term in the scheme. */ const treeWithOrphan = (): DocumentConcepts => ({ ...tree(), concepts: [{id: 'unfiled', prefLabel: 'Unfiled', level: 0, isOrphan: true}], }) describe('the move command, for an orphan', () => { it('offers a move, so an orphan can be brought into the hierarchy', () => { renderHierarchy(createFakeDataPort({tree: treeWithOrphan()})) expect(screen.getByRole('button', {name: 'More actions for Unfiled'})).toBeInTheDocument() }) it('routes the new broader term through the port with nothing to unset', async () => { const user = userEvent.setup() const port = createFakeDataPort({tree: treeWithOrphan()}) renderHierarchy(port) const dialog = await openMoveDialog(user, 'Unfiled') await user.click(within(dialog).getByRole('button', {name: /^Mammals/})) expect(port.appliedPlans).toHaveLength(1) expect(port.appliedPlans[0]).toMatchObject({ kind: 'move', concept: {publishedId: 'unfiled'}, unsetBroader: null, insertBroader: {_ref: 'mammals', _type: 'reference'}, }) }) it('takes the orphan out of the root list optimistically', async () => { const user = userEvent.setup() renderHierarchy(createFakeDataPort({tree: treeWithOrphan()})) const dialog = await openMoveDialog(user, 'Unfiled') await user.click(within(dialog).getByRole('button', {name: /^Mammals/})) // One rendering left, and it is nested rather than at the root. const occurrences = screen.getAllByRole('button', {name: /^Unfiled/}) expect(occurrences).toHaveLength(1) expect(screen.getByRole('button', {name: /^Mammals/}).closest('.open')).toContainElement( occurrences[0], ) }) it('offers no way to drop a broader term the orphan does not have', async () => { const user = userEvent.setup() renderHierarchy(createFakeDataPort({tree: treeWithOrphan()})) const dialog = await openMoveDialog(user, 'Unfiled') expect( within(dialog).queryByRole('button', {name: /as a broader term/}), ).not.toBeInTheDocument() }) }) /** * The handle is aria-hidden on purpose — dragging is a pointer accelerator and * the row menu is the accessible path — so it is found by its icon rather than * by role. The gesture itself needs layout, which jsdom has none of; what is * worth pinning is which rows can be grabbed, and when the grip shows itself. */ const rowOf = (label: string) => screen.getByRole('button', {name: new RegExp(`^${label}`)}).closest('.open') as HTMLElement /** A row's own line. Rows nested beneath it sit outside this element. */ const lineOf = (row: HTMLElement) => row.querySelector(':scope > [data-ui="Card"]') as HTMLElement const handleIn = (row: HTMLElement) => lineOf(row).querySelector('[data-sanity-icon="drag-handle"]')?.closest('button') ?? null describe('the drag handle, where it is offered', () => { it('reveals a grip on a nested concept and on an orphan when the row is hovered', async () => { const user = userEvent.setup() renderHierarchy(createFakeDataPort({tree: treeWithOrphan()})) for (const label of ['Birds', 'Falcons', 'Mammals', 'Unfiled']) { const row = rowOf(label) const handle = handleIn(row)! expect(handle).not.toBeVisible() await user.hover(lineOf(row)) expect(handle).toBeVisible() } }) it('leaves the root edge flush when every row there is a top concept', () => { // Nothing at this root can be dragged, so a reserved column would be dead space. renderHierarchy(createFakeDataPort({tree: tree()})) expect(handleIn(rowOf('Animals'))).toBeNull() }) it('holds the slot open on a top concept once the root also holds an orphan', async () => { const user = userEvent.setup() renderHierarchy(createFakeDataPort({tree: treeWithOrphan()})) // The slot exists so a root of top concepts and orphans shares a left edge… const animals = rowOf('Animals') expect(handleIn(animals)).toBeInTheDocument() // …but a top concept cannot be dragged, so hovering it reveals nothing. await user.hover(lineOf(animals)) expect(handleIn(animals)).not.toBeVisible() }) it('offers none at all in the published perspective', () => { const {container} = renderHierarchy(createFakeDataPort({tree: treeWithOrphan()}), 'published') expect(container.querySelectorAll('[data-sanity-icon="drag-handle"]')).toHaveLength(0) }) it('offers them inside a release, where dragging now reaches the version', () => { const {container} = renderHierarchy(createFakeDataPort({tree: treeWithOrphan()}), 'rSummerDrop') expect(container.querySelectorAll('[data-sanity-icon="drag-handle"]').length).toBeGreaterThan(0) }) }) describe('the drag handle, when it shows itself', () => { it('holds the space open so the label does not shift when it appears', () => { renderHierarchy(createFakeDataPort({tree: tree()})) // Hidden by visibility rather than removed, so it still occupies its slot. const handle = handleIn(rowOf('Falcons'))! expect(handle).toBeInTheDocument() expect(handle.style.visibility).toBe('hidden') }) }) describe('the row menu, across the kinds of row', () => { it.each(['Unfiled', 'Falcons'])('offers both commands on %s', async (label) => { const user = userEvent.setup() renderHierarchy(createFakeDataPort({tree: treeWithOrphan()})) await openRowMenu(user, label) expect(await screen.findByText('Move to…')).toBeInTheDocument() expect(screen.getByText('Remove from scheme')).toBeInTheDocument() }) it('offers no menu at all in the published perspective', () => { renderHierarchy(createFakeDataPort({tree: treeWithOrphan()}), 'published') expect(screen.queryByRole('button', {name: /^More actions/})).not.toBeInTheDocument() }) })