I1 scroll restored at once on a Back into layers; I2 Aller a opens the target dialog in one push; M1-M9, criteria 8 and 17 (left-draft notice), entry caps, no exhaustive-deps disables, locals within 5.
249 lines
8.1 KiB
TypeScript
249 lines
8.1 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} />
|
|
<button type="button">outside</button>
|
|
</div>
|
|
<button type="button" onClick={() => setActive('general')}>
|
|
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 an absolute full-width overlay under the row and closes on a second click', 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: 'Vérification' }))
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false')
|
|
})
|
|
})
|
|
|
|
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: 'Vérification' }))
|
|
expect(screen.getByRole('tab', { name: 'Vérification' })).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 the focus moves out of the trigger and the list', async () => {
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
const item = screen.getByRole('button', { name: 'Constats' })
|
|
act(() => item.focus())
|
|
expect(more()).toHaveAttribute('aria-expanded', 'true')
|
|
act(() => more().focus())
|
|
expect(more()).toHaveAttribute('aria-expanded', 'true')
|
|
act(() => screen.getByRole('button', { name: 'outside' }).focus())
|
|
expect(more()).toHaveAttribute('aria-expanded', 'false')
|
|
await gone()
|
|
})
|
|
})
|