fix(editor): Task 4 round 1, swallow release, menu tests
This commit is contained in:
1 parent
ca5804a8b7
commit
dfc8965844
2 files changed
+80
-8
No files matched your search
@@ -54,7 +54,10 @@ function useMoreEscape(open: boolean, close: () => void) {
|
|||||||
}, [open, close])
|
}, [open, close])
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Swallows the click that completes a closing tap, once, so the page never sees it. */
|
/**
|
||||||
|
* Swallows the click that completes a closing tap, once, so the page never sees it. A press
|
||||||
|
* that yields no click (cancelled, context menu, drag) releases it at once.
|
||||||
|
*/
|
||||||
function swallowNextClick() {
|
function swallowNextClick() {
|
||||||
const stop = (event: Event) => {
|
const stop = (event: Event) => {
|
||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
@@ -65,8 +68,12 @@ function swallowNextClick() {
|
|||||||
const release = () => {
|
const release = () => {
|
||||||
window.clearTimeout(timer)
|
window.clearTimeout(timer)
|
||||||
document.removeEventListener('click', stop, true)
|
document.removeEventListener('click', stop, true)
|
||||||
|
document.removeEventListener('pointercancel', release, true)
|
||||||
|
document.removeEventListener('contextmenu', release, true)
|
||||||
}
|
}
|
||||||
document.addEventListener('click', stop, true)
|
document.addEventListener('click', stop, true)
|
||||||
|
document.addEventListener('pointercancel', release, true)
|
||||||
|
document.addEventListener('contextmenu', release, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
/** « Plus »: the dropdown overlay and every way it closes. */
|
/** « Plus »: the dropdown overlay and every way it closes. */
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
import { act, cleanup, render, screen, waitFor } from '@testing-library/react'
|
import {
|
||||||
|
act,
|
||||||
|
cleanup,
|
||||||
|
fireEvent,
|
||||||
|
render,
|
||||||
|
screen,
|
||||||
|
waitFor,
|
||||||
|
} from '@testing-library/react'
|
||||||
import userEvent from '@testing-library/user-event'
|
import userEvent from '@testing-library/user-event'
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import EditorTabs from '@/components/editor/EditorTabs'
|
import EditorTabs from '@/components/editor/EditorTabs'
|
||||||
@@ -18,8 +25,15 @@ const list = () => document.getElementById('editor-tabs-more')
|
|||||||
const gone = () => waitFor(() => expect(list()).toBeNull())
|
const gone = () => waitFor(() => expect(list()).toBeNull())
|
||||||
|
|
||||||
/** The row in a page: a body that records the presses that reach it. */
|
/** The row in a page: a body that records the presses that reach it. */
|
||||||
function Page({ seen }: { seen: (kind: string) => void }) {
|
function Page({
|
||||||
const [active, setActive] = useState<EditorTabId>('annotations')
|
seen,
|
||||||
|
forced,
|
||||||
|
}: {
|
||||||
|
seen: (kind: string) => void
|
||||||
|
forced?: EditorTabId
|
||||||
|
}) {
|
||||||
|
const [chosen, setActive] = useState<EditorTabId>('annotations')
|
||||||
|
const active = forced ?? chosen
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
onPointerDown={() => seen('pointerdown')}
|
onPointerDown={() => seen('pointerdown')}
|
||||||
@@ -38,8 +52,10 @@ function Page({ seen }: { seen: (kind: string) => void }) {
|
|||||||
|
|
||||||
function mount() {
|
function mount() {
|
||||||
const seen = vi.fn()
|
const seen = vi.fn()
|
||||||
render(<Page seen={seen} />)
|
const view = render(<Page seen={seen} />)
|
||||||
return { seen, user: userEvent.setup() }
|
const force = (tab: EditorTabId) =>
|
||||||
|
view.rerender(<Page seen={seen} forced={tab} />)
|
||||||
|
return { seen, force, view, user: userEvent.setup() }
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('the overlay', () => {
|
describe('the overlay', () => {
|
||||||
@@ -59,7 +75,7 @@ describe('the overlay', () => {
|
|||||||
await user.click(more())
|
await user.click(more())
|
||||||
expect(list()).not.toHaveAttribute('inert')
|
expect(list()).not.toHaveAttribute('inert')
|
||||||
await user.click(more())
|
await user.click(more())
|
||||||
if (list()) expect(list()).toHaveAttribute('inert')
|
expect(list()).toHaveAttribute('inert')
|
||||||
await gone()
|
await gone()
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -75,7 +91,40 @@ describe('the overlay', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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', () => {
|
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 () => {
|
it('a press outside the row closes the menu and never reaches the page', async () => {
|
||||||
const { user, seen } = mount()
|
const { user, seen } = mount()
|
||||||
await user.click(more())
|
await user.click(more())
|
||||||
@@ -159,10 +208,26 @@ describe('the platform back request', () => {
|
|||||||
expect(StubWatcher.made[0].destroyed).toBe(true)
|
expect(StubWatcher.made[0].destroyed).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('works without CloseWatcher', async () => {
|
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)
|
vi.stubGlobal('CloseWatcher', undefined)
|
||||||
const { user } = mount()
|
const { user } = mount()
|
||||||
await user.click(more())
|
await user.click(more())
|
||||||
expect(more()).toHaveAttribute('aria-expanded', 'true')
|
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')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
Reference in new issue
Block a user