2020-04-11 03:37:30 +08:00
|
|
|
import { patchProp } from '../src/patchProp'
|
2020-04-02 09:59:42 +08:00
|
|
|
|
|
|
|
const timeout = () => new Promise(r => setTimeout(r))
|
2019-10-10 03:05:21 +08:00
|
|
|
|
2020-04-11 03:23:01 +08:00
|
|
|
describe(`runtime-dom: events patching`, () => {
|
2019-10-11 10:02:41 +08:00
|
|
|
it('should assign event handler', async () => {
|
2019-10-10 03:05:21 +08:00
|
|
|
const el = document.createElement('div')
|
2023-01-26 15:25:55 +08:00
|
|
|
const fn = vi.fn()
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el, 'onClick', null, fn)
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
expect(fn).toHaveBeenCalledTimes(3)
|
|
|
|
})
|
|
|
|
|
2019-10-11 10:02:41 +08:00
|
|
|
it('should update event handler', async () => {
|
2019-10-10 03:05:21 +08:00
|
|
|
const el = document.createElement('div')
|
2023-01-26 15:25:55 +08:00
|
|
|
const prevFn = vi.fn()
|
|
|
|
const nextFn = vi.fn()
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el, 'onClick', null, prevFn)
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el, 'onClick', prevFn, nextFn)
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
expect(prevFn).toHaveBeenCalledTimes(1)
|
|
|
|
expect(nextFn).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
2019-11-27 07:06:55 +08:00
|
|
|
it('should support multiple event handlers', async () => {
|
2019-10-10 03:05:21 +08:00
|
|
|
const el = document.createElement('div')
|
2023-01-26 15:25:55 +08:00
|
|
|
const fn1 = vi.fn()
|
|
|
|
const fn2 = vi.fn()
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el, 'onClick', null, [fn1, fn2])
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
expect(fn1).toHaveBeenCalledTimes(1)
|
|
|
|
expect(fn2).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
2019-11-27 07:06:55 +08:00
|
|
|
it('should unassign event handler', async () => {
|
2019-10-10 03:05:21 +08:00
|
|
|
const el = document.createElement('div')
|
2023-01-26 15:25:55 +08:00
|
|
|
const fn = vi.fn()
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el, 'onClick', null, fn)
|
|
|
|
patchProp(el, 'onClick', fn, null)
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
expect(fn).not.toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
2020-07-14 23:48:05 +08:00
|
|
|
it('should support event option modifiers', async () => {
|
2019-10-10 03:05:21 +08:00
|
|
|
const el = document.createElement('div')
|
2023-01-26 15:25:55 +08:00
|
|
|
const fn = vi.fn()
|
2020-07-15 01:20:59 +08:00
|
|
|
patchProp(el, 'onClickOnceCapture', null, fn)
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
expect(fn).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
2019-10-11 10:02:41 +08:00
|
|
|
it('should unassign event handler with options', async () => {
|
2019-10-10 03:05:21 +08:00
|
|
|
const el = document.createElement('div')
|
2023-01-26 15:25:55 +08:00
|
|
|
const fn = vi.fn()
|
2020-07-15 01:20:59 +08:00
|
|
|
patchProp(el, 'onClickCapture', null, fn)
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2020-07-15 01:20:59 +08:00
|
|
|
expect(fn).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
patchProp(el, 'onClickCapture', fn, null)
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-07-15 01:20:59 +08:00
|
|
|
await timeout()
|
|
|
|
expect(fn).toHaveBeenCalledTimes(1)
|
2019-10-10 03:05:21 +08:00
|
|
|
})
|
2020-04-07 23:34:54 +08:00
|
|
|
|
2020-04-10 23:57:07 +08:00
|
|
|
it('should support native onclick', async () => {
|
2020-04-07 23:34:54 +08:00
|
|
|
const el = document.createElement('div')
|
|
|
|
|
2020-04-10 23:57:07 +08:00
|
|
|
// string should be set as attribute
|
2023-01-26 21:35:50 +08:00
|
|
|
const fn = ((el as any).spy = vi.fn())
|
|
|
|
patchProp(el, 'onclick', null, 'this.spy(1)')
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-07 23:34:54 +08:00
|
|
|
await timeout()
|
2020-04-10 23:57:07 +08:00
|
|
|
expect(fn).toHaveBeenCalledWith(1)
|
2020-04-07 23:34:54 +08:00
|
|
|
|
2023-01-26 15:25:55 +08:00
|
|
|
const fn2 = vi.fn()
|
2023-01-26 21:35:50 +08:00
|
|
|
patchProp(el, 'onclick', 'this.spy(1)', fn2)
|
2022-10-14 16:00:03 +08:00
|
|
|
const event = new Event('click')
|
2020-04-07 23:34:54 +08:00
|
|
|
el.dispatchEvent(event)
|
|
|
|
await timeout()
|
|
|
|
expect(fn).toHaveBeenCalledTimes(1)
|
2020-04-10 23:57:07 +08:00
|
|
|
expect(fn2).toHaveBeenCalledWith(event)
|
2020-04-07 23:34:54 +08:00
|
|
|
})
|
2020-04-15 22:35:34 +08:00
|
|
|
|
|
|
|
it('should support stopImmediatePropagation on multiple listeners', async () => {
|
|
|
|
const el = document.createElement('div')
|
2023-01-26 15:25:55 +08:00
|
|
|
const fn1 = vi.fn((e: Event) => {
|
2020-04-15 22:35:34 +08:00
|
|
|
e.stopImmediatePropagation()
|
|
|
|
})
|
2023-01-26 15:25:55 +08:00
|
|
|
const fn2 = vi.fn()
|
2020-04-15 22:35:34 +08:00
|
|
|
patchProp(el, 'onClick', null, [fn1, fn2])
|
2022-10-14 16:00:03 +08:00
|
|
|
el.dispatchEvent(new Event('click'))
|
2020-04-15 22:35:34 +08:00
|
|
|
await timeout()
|
|
|
|
expect(fn1).toHaveBeenCalledTimes(1)
|
|
|
|
expect(fn2).toHaveBeenCalledTimes(0)
|
|
|
|
})
|
2020-08-04 05:55:22 +08:00
|
|
|
|
|
|
|
// #1747
|
|
|
|
it('should handle same computed handler function being bound on multiple targets', async () => {
|
|
|
|
const el1 = document.createElement('div')
|
|
|
|
const el2 = document.createElement('div')
|
|
|
|
|
2022-10-14 16:00:03 +08:00
|
|
|
// const event = new Event('click')
|
2023-01-26 15:25:55 +08:00
|
|
|
const prevFn = vi.fn()
|
|
|
|
const nextFn = vi.fn()
|
2020-08-04 05:55:22 +08:00
|
|
|
|
|
|
|
patchProp(el1, 'onClick', null, prevFn)
|
|
|
|
patchProp(el2, 'onClick', null, prevFn)
|
|
|
|
|
2022-10-14 16:00:03 +08:00
|
|
|
el1.dispatchEvent(new Event('click'))
|
|
|
|
el2.dispatchEvent(new Event('click'))
|
2020-08-04 05:55:22 +08:00
|
|
|
await timeout()
|
|
|
|
expect(prevFn).toHaveBeenCalledTimes(2)
|
|
|
|
expect(nextFn).toHaveBeenCalledTimes(0)
|
|
|
|
|
|
|
|
patchProp(el1, 'onClick', prevFn, nextFn)
|
|
|
|
patchProp(el2, 'onClick', prevFn, nextFn)
|
|
|
|
|
2022-10-14 16:00:03 +08:00
|
|
|
el1.dispatchEvent(new Event('click'))
|
|
|
|
el2.dispatchEvent(new Event('click'))
|
2020-08-04 05:55:22 +08:00
|
|
|
await timeout()
|
|
|
|
expect(prevFn).toHaveBeenCalledTimes(2)
|
|
|
|
expect(nextFn).toHaveBeenCalledTimes(2)
|
|
|
|
|
2022-10-14 16:00:03 +08:00
|
|
|
el1.dispatchEvent(new Event('click'))
|
|
|
|
el2.dispatchEvent(new Event('click'))
|
2020-08-04 05:55:22 +08:00
|
|
|
await timeout()
|
|
|
|
expect(prevFn).toHaveBeenCalledTimes(2)
|
|
|
|
expect(nextFn).toHaveBeenCalledTimes(4)
|
|
|
|
})
|
2021-02-09 14:58:36 +08:00
|
|
|
|
2022-10-14 16:00:03 +08:00
|
|
|
// vuejs/vue#6566
|
|
|
|
it('should not fire handler attached by the event itself', async () => {
|
|
|
|
const el = document.createElement('div')
|
|
|
|
const child = document.createElement('div')
|
|
|
|
el.appendChild(child)
|
|
|
|
document.body.appendChild(el)
|
2023-01-26 15:25:55 +08:00
|
|
|
const childFn = vi.fn()
|
|
|
|
const parentFn = vi.fn()
|
2022-10-14 16:00:03 +08:00
|
|
|
|
|
|
|
patchProp(child, 'onClick', null, () => {
|
|
|
|
childFn()
|
|
|
|
patchProp(el, 'onClick', null, parentFn)
|
|
|
|
})
|
|
|
|
|
|
|
|
await timeout()
|
2023-02-01 15:59:50 +08:00
|
|
|
child.dispatchEvent(new Event('click', { bubbles: true }))
|
|
|
|
|
2022-10-14 16:00:03 +08:00
|
|
|
expect(childFn).toHaveBeenCalled()
|
|
|
|
expect(parentFn).not.toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
2021-02-09 14:58:36 +08:00
|
|
|
// #2841
|
|
|
|
test('should patch event correctly in web-components', async () => {
|
|
|
|
class TestElement extends HTMLElement {
|
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.customElements.define('test-element', TestElement)
|
|
|
|
const testElement = document.createElement('test-element', {
|
|
|
|
is: 'test-element'
|
|
|
|
})
|
2023-01-26 15:25:55 +08:00
|
|
|
const fn1 = vi.fn()
|
|
|
|
const fn2 = vi.fn()
|
2021-02-09 14:58:36 +08:00
|
|
|
|
|
|
|
// in webComponents, @foo-bar will patch prop 'onFooBar'
|
|
|
|
// and @foobar will patch prop 'onFoobar'
|
|
|
|
|
|
|
|
patchProp(testElement, 'onFooBar', null, fn1)
|
|
|
|
testElement.dispatchEvent(new CustomEvent('foo-bar'))
|
|
|
|
expect(fn1).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
patchProp(testElement, 'onFoobar', null, fn2)
|
|
|
|
testElement.dispatchEvent(new CustomEvent('foobar'))
|
|
|
|
expect(fn2).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
2019-10-10 03:05:21 +08:00
|
|
|
})
|