2019-10-15 05:46:30 +08:00
|
|
|
import { patchEvent } from '../../src/modules/events'
|
2020-04-02 09:59:42 +08:00
|
|
|
|
|
|
|
const timeout = () => new Promise(r => setTimeout(r))
|
2019-10-10 03:05:21 +08:00
|
|
|
|
|
|
|
describe(`events`, () => {
|
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')
|
|
|
|
const event = new Event('click')
|
|
|
|
const fn = jest.fn()
|
2020-04-07 23:34:54 +08:00
|
|
|
patchEvent(el, 'onClick', null, fn, null)
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
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')
|
|
|
|
const event = new Event('click')
|
|
|
|
const prevFn = jest.fn()
|
|
|
|
const nextFn = jest.fn()
|
2020-04-07 23:34:54 +08:00
|
|
|
patchEvent(el, 'onClick', null, prevFn, null)
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
2020-04-07 23:34:54 +08:00
|
|
|
patchEvent(el, 'onClick', prevFn, nextFn, null)
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
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')
|
|
|
|
const event = new Event('click')
|
|
|
|
const fn1 = jest.fn()
|
|
|
|
const fn2 = jest.fn()
|
2020-04-07 23:34:54 +08:00
|
|
|
patchEvent(el, 'onClick', null, [fn1, fn2], null)
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
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')
|
|
|
|
const event = new Event('click')
|
|
|
|
const fn = jest.fn()
|
2020-04-07 23:34:54 +08:00
|
|
|
patchEvent(el, 'onClick', null, fn, null)
|
|
|
|
patchEvent(el, 'onClick', fn, null, null)
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
expect(fn).not.toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
2019-10-11 10:02:41 +08:00
|
|
|
it('should support event options', async () => {
|
2019-10-10 03:05:21 +08:00
|
|
|
const el = document.createElement('div')
|
|
|
|
const event = new Event('click')
|
|
|
|
const fn = jest.fn()
|
|
|
|
const nextValue = {
|
|
|
|
handler: fn,
|
|
|
|
options: {
|
|
|
|
once: true
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 23:34:54 +08:00
|
|
|
patchEvent(el, 'onClick', null, nextValue, null)
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
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 support varying event options', async () => {
|
2019-10-10 03:05:21 +08:00
|
|
|
const el = document.createElement('div')
|
|
|
|
const event = new Event('click')
|
|
|
|
const prevFn = jest.fn()
|
|
|
|
const nextFn = jest.fn()
|
|
|
|
const nextValue = {
|
|
|
|
handler: nextFn,
|
|
|
|
options: {
|
|
|
|
once: true
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 23:34:54 +08:00
|
|
|
patchEvent(el, 'onClick', null, prevFn, null)
|
|
|
|
patchEvent(el, 'onClick', prevFn, nextValue, null)
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
expect(prevFn).not.toHaveBeenCalled()
|
|
|
|
expect(nextFn).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')
|
|
|
|
const event = new Event('click')
|
|
|
|
const fn = jest.fn()
|
|
|
|
const nextValue = {
|
|
|
|
handler: fn,
|
|
|
|
options: {
|
|
|
|
once: true
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 23:34:54 +08:00
|
|
|
patchEvent(el, 'onClick', null, nextValue, null)
|
|
|
|
patchEvent(el, 'onClick', nextValue, null, null)
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
el.dispatchEvent(event)
|
2020-04-02 09:59:42 +08:00
|
|
|
await timeout()
|
2019-10-10 03:05:21 +08:00
|
|
|
expect(fn).not.toHaveBeenCalled()
|
|
|
|
})
|
2020-04-07 23:34:54 +08:00
|
|
|
|
|
|
|
it('should assign native onclick attribute', async () => {
|
|
|
|
const el = document.createElement('div')
|
|
|
|
const event = new Event('click')
|
|
|
|
const fn = ((window as any)._nativeClickSpy = jest.fn())
|
|
|
|
|
|
|
|
patchEvent(el, 'onclick', null, '_nativeClickSpy()' as any)
|
|
|
|
el.dispatchEvent(event)
|
|
|
|
await timeout()
|
|
|
|
expect(fn).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
const fn2 = jest.fn()
|
|
|
|
patchEvent(el, 'onclick', null, fn2)
|
|
|
|
el.dispatchEvent(event)
|
|
|
|
await timeout()
|
|
|
|
expect(fn).toHaveBeenCalledTimes(1)
|
|
|
|
expect(fn2).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
2019-10-10 03:05:21 +08:00
|
|
|
})
|