vue3-yuanma/packages/runtime-dom/__tests__/modules/events.spec.ts

118 lines
3.2 KiB
TypeScript
Raw Normal View History

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))
describe(`events`, () => {
2019-10-11 10:02:41 +08:00
it('should assign event handler', async () => {
const el = document.createElement('div')
const event = new Event('click')
const fn = jest.fn()
patchEvent(el, 'click', null, fn, null)
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
expect(fn).toHaveBeenCalledTimes(3)
})
2019-10-11 10:02:41 +08:00
it('should update event handler', async () => {
const el = document.createElement('div')
const event = new Event('click')
const prevFn = jest.fn()
const nextFn = jest.fn()
patchEvent(el, 'click', null, prevFn, null)
el.dispatchEvent(event)
patchEvent(el, 'click', prevFn, nextFn, null)
2020-04-02 09:59:42 +08:00
await timeout()
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
expect(prevFn).toHaveBeenCalledTimes(1)
expect(nextFn).toHaveBeenCalledTimes(2)
})
it('should support multiple event handlers', async () => {
const el = document.createElement('div')
const event = new Event('click')
const fn1 = jest.fn()
const fn2 = jest.fn()
patchEvent(el, 'click', null, [fn1, fn2], null)
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
expect(fn1).toHaveBeenCalledTimes(1)
expect(fn2).toHaveBeenCalledTimes(1)
})
it('should unassign event handler', async () => {
const el = document.createElement('div')
const event = new Event('click')
const fn = jest.fn()
patchEvent(el, 'click', null, fn, null)
patchEvent(el, 'click', fn, null, null)
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
expect(fn).not.toHaveBeenCalled()
})
2019-10-11 10:02:41 +08:00
it('should support event options', async () => {
const el = document.createElement('div')
const event = new Event('click')
const fn = jest.fn()
const nextValue = {
handler: fn,
options: {
once: true
}
}
patchEvent(el, 'click', null, nextValue, null)
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
expect(fn).toHaveBeenCalledTimes(1)
})
2019-10-11 10:02:41 +08:00
it('should support varying event options', async () => {
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
}
}
patchEvent(el, 'click', null, prevFn, null)
patchEvent(el, 'click', prevFn, nextValue, null)
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
expect(prevFn).not.toHaveBeenCalled()
expect(nextFn).toHaveBeenCalledTimes(1)
})
2019-10-11 10:02:41 +08:00
it('should unassign event handler with options', async () => {
const el = document.createElement('div')
const event = new Event('click')
const fn = jest.fn()
const nextValue = {
handler: fn,
options: {
once: true
}
}
patchEvent(el, 'click', null, nextValue, null)
patchEvent(el, 'click', nextValue, null, null)
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
el.dispatchEvent(event)
2020-04-02 09:59:42 +08:00
await timeout()
expect(fn).not.toHaveBeenCalled()
})
})