Skip to content

Commit 8254b5f

Browse files
committed
test(vapor): test case for dom event handling
1 parent d487063 commit 8254b5f

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { effectScope } from '@vue/reactivity'
2+
import {
3+
delegate,
4+
delegateEvents,
5+
on,
6+
renderEffect,
7+
setDynamicEvents,
8+
} from '../../src'
9+
10+
describe('dom event', () => {
11+
delegateEvents('click')
12+
13+
test('on', () => {
14+
const el = document.createElement('div')
15+
const handler = vi.fn()
16+
on(el, 'click', handler)
17+
el.click()
18+
expect(handler).toHaveBeenCalled()
19+
})
20+
21+
test('delegate with direct attachment', () => {
22+
const el = document.createElement('div')
23+
document.body.appendChild(el)
24+
const handler = vi.fn()
25+
;(el as any).$evtclick = handler
26+
el.click()
27+
expect(handler).toHaveBeenCalled()
28+
})
29+
30+
test('delegate', () => {
31+
const el = document.createElement('div')
32+
document.body.appendChild(el)
33+
const handler = vi.fn()
34+
delegate(el, 'click', handler)
35+
el.click()
36+
expect(handler).toHaveBeenCalled()
37+
})
38+
39+
test('delegate with stopPropagation', () => {
40+
const parent = document.createElement('div')
41+
const child = document.createElement('div')
42+
parent.appendChild(child)
43+
document.body.appendChild(parent)
44+
const parentHandler = vi.fn()
45+
delegate(parent, 'click', parentHandler)
46+
const childHandler = vi.fn(e => e.stopPropagation())
47+
delegate(child, 'click', childHandler)
48+
child.click()
49+
expect(parentHandler).not.toHaveBeenCalled()
50+
expect(childHandler).toHaveBeenCalled()
51+
})
52+
53+
test('delegate with stopImmediatePropagation', () => {
54+
const parent = document.createElement('div')
55+
const child = document.createElement('div')
56+
parent.appendChild(child)
57+
document.body.appendChild(parent)
58+
const parentHandler = vi.fn()
59+
delegate(parent, 'click', parentHandler)
60+
const childHandler = vi.fn(e => e.stopImmediatePropagation())
61+
delegate(child, 'click', childHandler)
62+
child.click()
63+
expect(parentHandler).not.toHaveBeenCalled()
64+
expect(childHandler).toHaveBeenCalled()
65+
})
66+
67+
test('delegate with multiple handlers', () => {
68+
const el = document.createElement('div')
69+
document.body.appendChild(el)
70+
const handler1 = vi.fn()
71+
const handler2 = vi.fn()
72+
delegate(el, 'click', handler1)
73+
delegate(el, 'click', handler2)
74+
el.click()
75+
expect(handler1).toHaveBeenCalled()
76+
expect(handler2).toHaveBeenCalled()
77+
})
78+
79+
test('delegate with multiple handlers + stopImmediatePropagation', () => {
80+
const el = document.createElement('div')
81+
document.body.appendChild(el)
82+
const handler1 = vi.fn(e => e.stopImmediatePropagation())
83+
const handler2 = vi.fn()
84+
delegate(el, 'click', handler1)
85+
delegate(el, 'click', handler2)
86+
el.click()
87+
expect(handler1).toHaveBeenCalled()
88+
expect(handler2).not.toHaveBeenCalled()
89+
})
90+
91+
test('setDynamicEvents', () => {
92+
const el = document.createElement('div')
93+
const handler = vi.fn()
94+
const scope = effectScope()
95+
scope.run(() => {
96+
renderEffect(() => {
97+
setDynamicEvents(el, {
98+
click: handler,
99+
})
100+
})
101+
})
102+
el.click()
103+
expect(handler).toHaveBeenCalled()
104+
scope.stop()
105+
})
106+
})

packages/runtime-vapor/src/dom/event.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const delegatedEventHandler = (e: Event) => {
8989
}
9090
} else {
9191
handlers(e)
92+
if (e.cancelBubble) return
9293
}
9394
}
9495
node =
@@ -103,6 +104,6 @@ export function setDynamicEvents(
103104
events: Record<string, (...args: any[]) => any>,
104105
): void {
105106
for (const name in events) {
106-
on(el, name, () => events[name], { effect: true })
107+
on(el, name, events[name], { effect: true })
107108
}
108109
}

0 commit comments

Comments
 (0)