Skip to content

Commit fa53393

Browse files
author
lmhcoding
committed
Merge branch 'test-useEventRef'
2 parents d99595e + ac66dde commit fa53393

File tree

9 files changed

+95
-40
lines changed

9 files changed

+95
-40
lines changed

tests/useDebounce.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useDebounce } from '../src/useDebounce'
2-
import invokeHook from './util/invokeHook'
2+
import { invokeHook } from './util'
33

44
beforeEach(() => {
55
jest.useFakeTimers()

tests/useEvent.test.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,7 @@ import { VueWrapper } from '@vue/test-utils'
22
import { nextTick, onMounted, Ref, ref } from 'vue'
33
import { IEventTarget, useEvent } from '../src/useEvent'
44
import { Target } from '../src/util'
5-
import invokeHook from './util/invokeHook'
6-
7-
function patchEventTarget () {
8-
const div: any = document.createElement('div')
9-
let proto = Object.getPrototypeOf(div)
10-
while (proto) {
11-
// eslint-disable-next-line no-prototype-builtins
12-
if (proto.hasOwnProperty('addEventListener')) {
13-
const originAdd = proto.addEventListener
14-
const originRemove = proto.removeEventListener
15-
const addEventListener = jest.fn((event, cb, options) => {
16-
originAdd.call(
17-
addEventListener.mock.instances[0],
18-
event,
19-
cb,
20-
options
21-
)
22-
})
23-
const removeEventListener = jest.fn((event, cb, options) => {
24-
originRemove.call(
25-
removeEventListener.mock.instances[0],
26-
event,
27-
cb,
28-
options
29-
)
30-
})
31-
proto.addEventListener = addEventListener
32-
proto.removeEventListener = removeEventListener
33-
return [addEventListener, removeEventListener]
34-
}
35-
proto = Object.getPrototypeOf(proto)
36-
}
37-
return []
38-
}
5+
import { invokeHook, patchEventTarget } from './util'
396

407
const [add, remove] = patchEventTarget()
418
const handler = jest.fn()

tests/useEventRef.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { VueWrapper } from '@vue/test-utils'
2+
import { useEventRef } from '../src/useEventRef'
3+
import { invokeHook, patchEventTarget } from './util'
4+
5+
const [add, remove] = patchEventTarget()
6+
const handler = jest.fn()
7+
8+
describe('test useEventRef', () => {
9+
let wrapper: VueWrapper<any>
10+
let clear: () => void
11+
beforeEach(() => {
12+
wrapper = invokeHook(() => {
13+
const [target, clearFn] = useEventRef(
14+
'click',
15+
handler,
16+
true
17+
)
18+
clear = clearFn
19+
return {
20+
test: target
21+
}
22+
})
23+
})
24+
test('addEventListener should be called after mounted', () => {
25+
expect(add).toHaveBeenCalledTimes(1)
26+
expect(add).toHaveBeenCalledWith('click', handler, true)
27+
})
28+
test('callback should be called after firing an event', () => {
29+
const target = wrapper.find('#test')
30+
target.trigger('click')
31+
expect(handler).toHaveBeenCalledTimes(1)
32+
})
33+
test('removeEventListener should be called after invoking clear', () => {
34+
clear()
35+
expect(remove).toHaveBeenCalledTimes(1)
36+
expect(remove).toHaveBeenCalledWith('click', handler, true)
37+
})
38+
test('callback should not be called after invoking clear', () => {
39+
const target = wrapper.find('#test')
40+
clear()
41+
target.trigger('click')
42+
expect(handler).not.toBeCalled()
43+
})
44+
test('event should be removed after unmounted', () => {
45+
const targetDiv = wrapper.find('#test')
46+
wrapper.unmount()
47+
expect(remove).toHaveBeenCalledTimes(1)
48+
expect(remove).toHaveBeenCalledWith('click', handler, true)
49+
targetDiv.trigger('click')
50+
expect(handler).not.toBeCalled()
51+
})
52+
})

tests/useInterval.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useInterval } from '../src/useInterval'
2-
import invokeHook from './util/invokeHook'
2+
import { invokeHook } from './util'
33

44
let callback: Function | undefined
55

tests/useStorage.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Ref } from 'vue'
22
import { useStorage } from '../src/useStorage'
3-
import invokeHook from './util/invokeHook'
3+
import { invokeHook } from './util'
44

55
function testStorageWithSimpleVal (storage: Storage) {
66
describe('test useStorage when storage is storage and val is not an Object', () => {

tests/useTimeout.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useTimeout } from '../src/useTimeout'
2-
import invokeHook from './util/invokeHook'
2+
import { invokeHook } from './util'
33

44
beforeEach(() => {
55
jest.useFakeTimers()

tests/util/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './invokeHook'
2+
export * from './patchEventTarget'

tests/util/invokeHook.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { shallowMount } from '@vue/test-utils'
22
import { defineComponent } from 'vue'
33

4-
export default function invokeHooks (setup: () => any) {
4+
const defaultTemplate = '<div ref="test" data-test="test" id="test">test</div>'
5+
6+
export function invokeHook (setup: () => any, template = defaultTemplate) {
57
document.body.innerHTML = `
68
<div id="app"></div>
79
`
810
const App = defineComponent({
9-
template: '<div ref="test" data-test="test" id="test">test</div>',
11+
template,
1012
setup
1113
})
1214
// @ts-ignore

tests/util/patchEventTarget.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export function patchEventTarget () {
2+
const div: any = document.createElement('div')
3+
let proto = Object.getPrototypeOf(div)
4+
while (proto) {
5+
// eslint-disable-next-line no-prototype-builtins
6+
if (proto.hasOwnProperty('addEventListener')) {
7+
const originAdd = proto.addEventListener
8+
const originRemove = proto.removeEventListener
9+
const addEventListener = jest.fn((event, cb, options) => {
10+
originAdd.call(
11+
addEventListener.mock.instances[0],
12+
event,
13+
cb,
14+
options
15+
)
16+
})
17+
const removeEventListener = jest.fn((event, cb, options) => {
18+
originRemove.call(
19+
removeEventListener.mock.instances[0],
20+
event,
21+
cb,
22+
options
23+
)
24+
})
25+
proto.addEventListener = addEventListener
26+
proto.removeEventListener = removeEventListener
27+
return [addEventListener, removeEventListener]
28+
}
29+
proto = Object.getPrototypeOf(proto)
30+
}
31+
return []
32+
}

0 commit comments

Comments
 (0)