169 lines
5.5 KiB
TypeScript
169 lines
5.5 KiB
TypeScript
/** « Plus »: the dropdown overlay and every way it closes. */
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { act, cleanup, 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 }: { seen: (kind: string) => void }) {
|
|
const [active, setActive] = useState<EditorTabId>('annotations')
|
|
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()
|
|
render(<Page seen={seen} />)
|
|
return { seen, 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())
|
|
if (list()) 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('outside presses', () => {
|
|
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('works without CloseWatcher', async () => {
|
|
vi.stubGlobal('CloseWatcher', undefined)
|
|
const { user } = mount()
|
|
await user.click(more())
|
|
expect(more()).toHaveAttribute('aria-expanded', 'true')
|
|
})
|
|
})
|