Skip to content

Commit 0918e3d

Browse files
ocavuebotocavue
andauthored
test: check link-hover taps by the handlers after it (#786)
Co-authored-by: ocavue <ocavue@gmail.com>
1 parent 4dcad10 commit 0918e3d

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

‎packages/core/src/extensions/link-hover.test.tsx‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { definePlugin, Priority, withPriority } from '@prosekit/core'
2+
import { Plugin } from '@prosekit/pm/state'
13
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
24
import { page } from 'vitest/browser'
35

@@ -17,6 +19,26 @@ function applyHoverable(markdown: string, onHoverChange: LinkHoverHandler) {
1719
return fixture
1820
}
1921

22+
/**
23+
* Records the `defaultPrevented` state of each `mousedown` that reaches a
24+
* lowest-priority handler, after every other `handleDOMEvents.mousedown`.
25+
*/
26+
function recordPassedMouseDowns(fixture: ReturnType<typeof setupFixture>): boolean[] {
27+
const passed: boolean[] = []
28+
const plugin = new Plugin({
29+
props: {
30+
handleDOMEvents: {
31+
mousedown: (_view, event) => {
32+
passed.push(event.defaultPrevented)
33+
return false
34+
},
35+
},
36+
},
37+
})
38+
fixture.editor.use(withPriority(definePlugin(plugin), Priority.lowest))
39+
return passed
40+
}
41+
2042
function buildMouse(type: 'mousedown' | 'mouseup' | 'click', clientX: number, clientY: number) {
2143
return new MouseEvent(type, { bubbles: true, cancelable: true, clientX, clientY })
2244
}
@@ -190,11 +212,13 @@ describe('Markdown-link tap callback', () => {
190212
const link = fixture.view.dom.querySelector(`.${LINK_TEXT_MARK_CLASS}`)
191213
const paragraph = fixture.view.dom.querySelector('p')
192214
if (!link || !paragraph) throw new Error('missing target')
215+
const passed = recordPassedMouseDowns(fixture)
193216

194217
dispatchTap(link)
195-
const { mousedown } = dispatchTap(paragraph)
218+
dispatchTap(paragraph)
196219

197-
expect(mousedown.defaultPrevented).toBe(false)
220+
// Only the tap outside links reaches the handlers after this extension.
221+
expect(passed).toEqual([false])
198222
expect(onHoverChange.mock.calls.map(([hit]) => hit?.payload.href)).toEqual([
199223
'https://example.com',
200224
undefined,
@@ -206,12 +230,13 @@ describe('Markdown-link tap callback', () => {
206230
using fixture = applyHoverable('[Docs](https://example.com)', onHoverChange)
207231
const link = fixture.view.dom.querySelector(`.${LINK_TEXT_MARK_CLASS}`)
208232
if (!link) throw new Error('missing link')
233+
const passed = recordPassedMouseDowns(fixture)
209234

210235
for (const pointerType of ['mouse', 'pen']) {
211-
const { mousedown, click } = dispatchTap(link, pointerType)
212-
expect(mousedown.defaultPrevented).toBe(false)
236+
const { click } = dispatchTap(link, pointerType)
213237
expect(click.defaultPrevented).toBe(false)
214238
}
239+
expect(passed).toEqual([false, false])
215240
expect(onHoverChange).not.toHaveBeenCalled()
216241
})
217242

0 commit comments

Comments
 (0)