feat(editor): Plus dropdown overlay with back request, sticky sidebar
This commit is contained in:
1 parent
8a60128311
commit
ca5804a8b7
8 files changed
+536
-55
No files matched your search
@@ -0,0 +1,168 @@
|
||||
/** « 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')
|
||||
})
|
||||
})
|
||||
@@ -173,7 +173,9 @@ describe('tabs', () => {
|
||||
])
|
||||
await user.click(rest[1])
|
||||
expect(screen.getByTestId('editor-tab-findings')).toBeInTheDocument()
|
||||
expect(document.getElementById('editor-tabs-more')).toBeNull()
|
||||
await waitFor(() =>
|
||||
expect(document.getElementById('editor-tabs-more')).toBeNull(),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -179,6 +179,14 @@ describe('AppLayout, editor sidebar from 1 280 px', () => {
|
||||
expect(topBarMenu()).toHaveClass('lg:hidden')
|
||||
})
|
||||
|
||||
it('keeps the sidebar fixed while the content scrolls, with its own scroll', () => {
|
||||
mount('/app/admin/sheets/s-1/draft')
|
||||
expect(sidebar()).toHaveClass('sticky', 'top-0', 'h-dvh', 'self-start', 'overflow-y-auto')
|
||||
cleanup()
|
||||
mount('/app/admin/sheets/s-1')
|
||||
expect(sidebar()).toHaveClass('sticky', 'top-0', 'h-dvh', 'self-start', 'overflow-y-auto')
|
||||
})
|
||||
|
||||
it('keeps the drawer open at 1 180 px in the editor, closes it from 1 280 px', async () => {
|
||||
const user = userEvent.setup()
|
||||
const fire = stubScreen(73.75)
|
||||
|
||||
Reference in new issue
Block a user