fix(editor): bubble clears the strip's commands, announces through a steady region

This commit is contained in:
bchanot committed 2026-11-19 21:04:21 +01:00
1 parent 49f0144729
commit a9252375e1
7 files changed
+313 -56

No files matched your search

@@ -0,0 +1,121 @@
/** The held-reason bubble (D1): its closing rules and its place, apart from the editor. */
import { afterEach, describe, expect, it } from 'vitest'
import { act, cleanup, render, renderHook, screen } from '@testing-library/react'
import HeldReasonBubble from '@/components/editor/HeldReasonBubble'
import { useHeldBubble } from '@/components/editor/useHeldBubble'
afterEach(() => {
cleanup()
document.body.innerHTML = ''
})
function opened(tab = 'annotations') {
const button = document.createElement('button')
document.body.append(button)
const ref = { current: button }
const hook = renderHook(
({ held, tab }) => useHeldBubble(held, tab, ref),
{ initialProps: { held: true, tab } },
)
act(() => hook.result.current.show())
expect(hook.result.current.open).toBe(true)
return { hook, button }
}
const key = (target: EventTarget, init: KeyboardEventInit = {}) => {
const event = new KeyboardEvent('keydown', {
key: 'Escape',
bubbles: true,
cancelable: true,
...init,
})
act(() => {
target.dispatchEvent(event)
})
return event
}
describe('closing', () => {
it('closes when the tab prop changes, with no press or focus move', () => {
const { hook } = opened()
hook.rerender({ held: true, tab: 'general' })
expect(hook.result.current.open).toBe(false)
})
for (const type of ['scroll', 'wheel', 'touchmove']) {
it(`closes on ${type}`, () => {
const { hook } = opened()
act(() => {
document.dispatchEvent(new Event(type, { bubbles: true }))
})
expect(hook.result.current.open).toBe(false)
})
}
it('closes when a dialog opens', async () => {
const { hook } = opened()
const dialog = document.createElement('dialog')
document.body.append(dialog)
await act(async () => {
dialog.setAttribute('open', '')
await Promise.resolve()
})
expect(hook.result.current.open).toBe(false)
})
it('Escape closes it and is taken from everything else', () => {
const { hook, button } = opened()
const event = key(button)
expect(hook.result.current.open).toBe(false)
expect(event.defaultPrevented).toBe(true)
expect(key(button).defaultPrevented).toBe(false)
})
it('Escape typed in a text field closes it and stays the field\'s', () => {
const { hook } = opened()
const field = document.createElement('input')
document.body.append(field)
const event = key(field)
expect(hook.result.current.open).toBe(false)
expect(event.defaultPrevented).toBe(false)
})
})
describe('the place', () => {
const rect = (left: number, right: number, top = 0, bottom = 40) =>
({ left, right, top, bottom, width: right - left, height: bottom - top }) as DOMRect
function anchor() {
const button = document.createElement('button')
document.body.append(button)
button.getBoundingClientRect = () => rect(900, 960, 10, 50)
Object.defineProperty(document.documentElement, 'clientWidth', {
configurable: true,
value: 1024,
})
return button
}
const strip = (commandsLeft: number) => {
const el = document.createElement('div')
el.dataset.testid = 'edit-strip'
const commands = document.createElement('div')
el.append(commands)
el.getBoundingClientRect = () => rect(100, 980, 70, 134)
commands.getBoundingClientRect = () => rect(commandsLeft, 970, 74, 130)
document.body.append(el)
}
it('sits 4 px under its button, right edge on the button, without a strip', () => {
render(<HeldReasonBubble anchor={anchor()} text="why" />)
const bubble = screen.getByTestId('history-hold-bubble')
expect(bubble.style.getPropertyValue('--bubble-top')).toBe('54px')
expect(bubble.style.right).toBe('64px')
expect(bubble.style.maxWidth).toBe('min(18rem, 952px)')
})
it('ends left of the strip\'s commands when the strip is shown', () => {
strip(760)
render(<HeldReasonBubble anchor={anchor()} text="why" />)
const bubble = screen.getByTestId('history-hold-bubble')
expect(bubble.style.right).toBe('268px')
})
})
@@ -790,10 +790,44 @@ describe('why a button is held (point 6)', () => {
stacks()
pend()
await user.click(redoButton())
expect(bubble()).toHaveAttribute('role', 'status')
expect(bubble()).toHaveAttribute('aria-hidden', 'true')
return user
}
it('is announced by the button\'s always-mounted status region, once per press', async () => {
const user = await openAnnotations()
stacks()
pend()
const region = screen.getByTestId('history-status-redo')
expect(region).toHaveAttribute('role', 'status')
expect(region).toBeEmptyDOMElement()
await user.click(redoButton())
expect(region).toBe(screen.getByTestId('history-status-redo'))
expect(region).toHaveTextContent(PENDING)
const first = region.firstElementChild
await user.click(redoButton())
expect(region).toHaveTextContent(PENDING)
expect(region.children).toHaveLength(1)
expect(region.firstElementChild).not.toBe(first)
await user.pointer({ keys: '[MouseLeft]', target: document.body })
expect(region).toBeEmptyDOMElement()
})
it('a second Escape drops the pending work, as before', async () => {
const user = await openAnnotations()
const list = screen.getByRole('list', { name: 'Annotations de la vue' })
await user.click(within(list).getAllByRole('button')[0]!)
stacks()
pend()
redoButton().focus()
await user.keyboard('{Enter}')
await user.keyboard('{Escape}')
noMessage()
expect(useAnnotationWork.getState().preview).not.toBeNull()
await user.keyboard('{Escape}')
expect(useAnnotationWork.getState().preview).toBeNull()
})
it('is portalled out of the row, so nothing in the layout can move', async () => {
await heldRedo()
expect(screen.getByTestId('history-buttons')).not.toContainElement(
@@ -860,7 +894,7 @@ describe('why a button is held (point 6)', () => {
expect(bubble()).toHaveTextContent(PENDING)
expect(bubble().style.getPropertyValue('--bubble-top')).toBe('54px')
expect(bubble().style.right).toBe('64px')
expect(bubble().style.maxWidth).toBe('min(22rem, 952px)')
expect(bubble().style.maxWidth).toBe('min(18rem, 952px)')
delete (document.documentElement as { clientWidth?: number }).clientWidth
})
})