319 lines
10 KiB
TypeScript
319 lines
10 KiB
TypeScript
/** « Plus »: the dropdown overlay and every way it closes. */
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
act,
|
|
cleanup,
|
|
fireEvent,
|
|
render,
|
|
screen,
|
|
waitFor,
|
|
} from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { useState } from 'react'
|
|
import EditorTabs from '@/components/editor/EditorTabs'
|
|
import type { EditorTabId } from '@/editor/tabs'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
|
|
beforeEach(() => useLocaleStore.setState({ locale: 'fr' }))
|
|
afterEach(() => {
|
|
cleanup()
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
const more = () => screen.getByRole('button', { name: 'Plus' })
|
|
const list = () => document.getElementById('editor-tabs-more')
|
|
const gone = () => waitFor(() => expect(list()).toBeNull())
|
|
|
|
/** The row in a page: a body that records the presses that reach it. */
|
|
function Page({
|
|
seen,
|
|
forced,
|
|
}: {
|
|
seen: (kind: string) => void
|
|
forced?: EditorTabId
|
|
}) {
|
|
const [chosen, setActive] = useState<EditorTabId>('annotations')
|
|
const active = forced ?? chosen
|
|
return (
|
|
<div
|
|
onPointerDown={() => seen('pointerdown')}
|
|
onClick={() => seen('click')}
|
|
>
|
|
<div data-editor-workspace>
|
|
<EditorTabs active={active} tier="small" onSelect={setActive} />
|
|
<div
|
|
id={`editor-panel-${active}`}
|
|
role="tabpanel"
|
|
tabIndex={-1}
|
|
aria-label={active}
|
|
/>
|
|
<button type="button">outside</button>
|
|
</div>
|
|
<button type="button" onClick={() => setActive('findings')}>
|
|
elsewhere
|
|
</button>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function mount() {
|
|
const seen = vi.fn()
|
|
const view = render(<Page seen={seen} />)
|
|
const force = (tab: EditorTabId) =>
|
|
view.rerender(<Page seen={seen} forced={tab} />)
|
|
return { seen, force, view, user: userEvent.setup() }
|
|
}
|
|
|
|
describe('the overlay', () => {
|
|
it('opens as a full-width overlay under the row, a second click closes', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
expect(more()).toHaveAttribute('aria-expanded', 'true')
|
|
expect(list()).toHaveClass('absolute', 'inset-x-0', 'top-full', 'z-20')
|
|
expect(document.querySelector('[data-editor-tabs]')).toHaveClass('relative')
|
|
await user.click(more())
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false')
|
|
await gone()
|
|
})
|
|
|
|
it('a closing list is inert', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
expect(list()).not.toHaveAttribute('inert')
|
|
await user.click(more())
|
|
expect(list()).toHaveAttribute('inert')
|
|
await gone()
|
|
})
|
|
|
|
it('closes on a selection and on a tab change made elsewhere', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
await user.click(screen.getByRole('button', { name: 'Constats' }))
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false')
|
|
await gone()
|
|
await user.click(more())
|
|
await user.click(screen.getByRole('tab', { name: 'Général' }))
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false')
|
|
})
|
|
})
|
|
|
|
describe('the vertical list', () => {
|
|
it('stacks the entries in tab order, the active one marked', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
expect(list()).toHaveClass('flex-col')
|
|
const rows = [...list()!.querySelectorAll('button')]
|
|
expect(rows.map((row) => row.id)).toEqual([
|
|
'editor-tab-findings',
|
|
'editor-tab-references',
|
|
'editor-tab-verification',
|
|
])
|
|
expect(list()!.querySelector('[aria-current]')).toBeNull()
|
|
rows.forEach((row) => expect(row).toHaveClass('min-h-12', 'w-full'))
|
|
rows.forEach((row) => expect(row.className).toContain('hover:bg-'))
|
|
await user.click(rows[0])
|
|
await user.click(more())
|
|
const current = list()!.querySelectorAll('[aria-current="true"]')
|
|
expect(current).toHaveLength(1)
|
|
expect(current[0].id).toBe('editor-tab-findings')
|
|
expect(current[0].querySelector('svg')).not.toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('the panel takes the focus after a pick', () => {
|
|
it('Enter on an entry focuses the panel of that tab once the list is gone', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
screen.getByRole('button', { name: 'Constats' }).focus()
|
|
await user.keyboard('{Enter}')
|
|
await gone()
|
|
expect(document.activeElement).toBe(
|
|
document.getElementById('editor-panel-findings'),
|
|
)
|
|
})
|
|
|
|
it('a pointer pick does the same', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
await user.click(screen.getByRole('button', { name: 'Constats' }))
|
|
expect(document.activeElement).toBe(
|
|
document.getElementById('editor-panel-findings'),
|
|
)
|
|
})
|
|
|
|
it('Escape still returns the focus to « Plus »', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
await user.keyboard('{Escape}')
|
|
expect(document.activeElement).toBe(more())
|
|
})
|
|
})
|
|
|
|
describe('the render-time reset', () => {
|
|
it('a new active tab closes the open menu with no click in the row', async () => {
|
|
const { user, force } = mount()
|
|
await user.click(more())
|
|
expect(more()).toHaveAttribute('aria-expanded', 'true')
|
|
act(() => force('general'))
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false')
|
|
await gone()
|
|
})
|
|
})
|
|
|
|
describe('outside presses', () => {
|
|
it('a cancelled closing press does not swallow the next real click', async () => {
|
|
const { user, seen } = mount()
|
|
await user.click(more())
|
|
const target = screen.getByRole('button', { name: 'elsewhere' })
|
|
fireEvent.pointerDown(target)
|
|
fireEvent.pointerCancel(target)
|
|
seen.mockClear()
|
|
fireEvent.click(target)
|
|
expect(seen).toHaveBeenCalledWith('click')
|
|
})
|
|
|
|
it('a context menu after the closing press releases the swallow too', async () => {
|
|
const { user, seen } = mount()
|
|
await user.click(more())
|
|
const target = screen.getByRole('button', { name: 'elsewhere' })
|
|
fireEvent.pointerDown(target)
|
|
fireEvent.contextMenu(target)
|
|
seen.mockClear()
|
|
fireEvent.click(target)
|
|
expect(seen).toHaveBeenCalledWith('click')
|
|
})
|
|
|
|
it('a press outside the row closes the menu and never reaches the page', async () => {
|
|
const { user, seen } = mount()
|
|
await user.click(more())
|
|
seen.mockClear()
|
|
await user.click(screen.getByRole('button', { name: 'elsewhere' }))
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false')
|
|
expect(seen).not.toHaveBeenCalled()
|
|
expect(screen.getByRole('tab', { name: 'Annotations' })).toHaveAttribute(
|
|
'aria-selected',
|
|
'true',
|
|
)
|
|
})
|
|
|
|
it('the next press after the closing tap acts normally', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
await user.click(screen.getByRole('button', { name: 'elsewhere' }))
|
|
await user.click(screen.getByRole('button', { name: 'elsewhere' }))
|
|
expect(more()).toHaveClass('text-[var(--color-accent-strong)]')
|
|
})
|
|
|
|
it('a press inside the row closes the menu and goes on to act', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
await user.click(screen.getByRole('tab', { name: 'Général' }))
|
|
expect(screen.getByRole('tab', { name: 'Général' })).toHaveAttribute(
|
|
'aria-selected',
|
|
'true',
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('Escape', () => {
|
|
it('closes the menu and focuses « Plus »', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
await user.keyboard('{Escape}')
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false')
|
|
expect(document.activeElement).toBe(more())
|
|
})
|
|
|
|
it('leaves the menu to an open modal dialog', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
const dialog = document.createElement('dialog')
|
|
dialog.setAttribute('open', '')
|
|
document.body.append(dialog)
|
|
await user.keyboard('{Escape}')
|
|
expect(more()).toHaveAttribute('aria-expanded', 'true')
|
|
dialog.remove()
|
|
})
|
|
})
|
|
|
|
describe('the platform back request', () => {
|
|
class StubWatcher {
|
|
static made: StubWatcher[] = []
|
|
onclose: (() => void) | null = null
|
|
destroyed = false
|
|
constructor() {
|
|
StubWatcher.made.push(this)
|
|
}
|
|
destroy() {
|
|
this.destroyed = true
|
|
}
|
|
}
|
|
|
|
beforeEach(() => {
|
|
StubWatcher.made = []
|
|
vi.stubGlobal('CloseWatcher', StubWatcher)
|
|
})
|
|
|
|
it('creates a watcher on open, closes on its request, destroys it on close', async () => {
|
|
const { user } = mount()
|
|
expect(StubWatcher.made).toHaveLength(0)
|
|
await user.click(more())
|
|
expect(StubWatcher.made).toHaveLength(1)
|
|
act(() => StubWatcher.made[0].onclose?.())
|
|
await waitFor(() =>
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false'),
|
|
)
|
|
expect(StubWatcher.made[0].destroyed).toBe(true)
|
|
})
|
|
|
|
it('destroys the watcher on unmount and on a tab-change close', async () => {
|
|
const first = mount()
|
|
await first.user.click(more())
|
|
first.view.unmount()
|
|
expect(StubWatcher.made[0].destroyed).toBe(true)
|
|
const second = mount()
|
|
await second.user.click(more())
|
|
act(() => second.force('general'))
|
|
expect(StubWatcher.made[1].destroyed).toBe(true)
|
|
})
|
|
|
|
it('works without CloseWatcher: Escape and an outside press still close', async () => {
|
|
vi.stubGlobal('CloseWatcher', undefined)
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
expect(more()).toHaveAttribute('aria-expanded', 'true')
|
|
await user.keyboard('{Escape}')
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false')
|
|
await user.click(more())
|
|
await user.click(screen.getByRole('button', { name: 'elsewhere' }))
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false')
|
|
})
|
|
})
|
|
|
|
describe('final review: keyboard focus leaving « Plus » (M-8)', () => {
|
|
it('closes when Tab moves the focus out of the trigger and the list', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
const items = [...list()!.querySelectorAll('button')]
|
|
act(() => more().focus())
|
|
await user.tab()
|
|
expect(items[0]).toHaveFocus()
|
|
expect(more()).toHaveAttribute('aria-expanded', 'true')
|
|
act(() => items.at(-1)!.focus())
|
|
await user.tab()
|
|
expect(document.activeElement?.closest('#editor-tabs-more')).toBeNull()
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false')
|
|
await gone()
|
|
})
|
|
|
|
it('a focus placed elsewhere keeps it open: Escape closes and returns the focus', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
act(() => screen.getByRole('button', { name: 'outside' }).focus())
|
|
expect(more()).toHaveAttribute('aria-expanded', 'true')
|
|
await user.keyboard('{Escape}')
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false')
|
|
expect(more()).toHaveFocus()
|
|
})
|
|
})
|