2019-09-03 00:09:29 +08:00
|
|
|
import {
|
|
|
|
h,
|
|
|
|
applyDirectives,
|
|
|
|
ref,
|
|
|
|
render,
|
|
|
|
nodeOps,
|
|
|
|
DirectiveHook,
|
|
|
|
VNode,
|
|
|
|
DirectiveBinding,
|
|
|
|
nextTick
|
|
|
|
} from '@vue/runtime-test'
|
2019-09-07 00:58:31 +08:00
|
|
|
import { currentInstance, ComponentInternalInstance } from '../src/component'
|
2019-09-03 00:09:29 +08:00
|
|
|
|
2019-09-01 10:18:21 +08:00
|
|
|
describe('directives', () => {
|
2019-09-03 00:09:29 +08:00
|
|
|
it('should work', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
|
|
|
|
function assertBindings(binding: DirectiveBinding) {
|
|
|
|
expect(binding.value).toBe(count.value)
|
|
|
|
expect(binding.arg).toBe('foo')
|
|
|
|
expect(binding.instance).toBe(_instance && _instance.renderProxy)
|
|
|
|
expect(binding.modifiers && binding.modifiers.ok).toBe(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
const beforeMount = jest.fn(((el, binding, vnode, prevVNode) => {
|
|
|
|
expect(el.tag).toBe('div')
|
|
|
|
// should not be inserted yet
|
|
|
|
expect(el.parentNode).toBe(null)
|
|
|
|
expect(root.children.length).toBe(0)
|
|
|
|
|
|
|
|
assertBindings(binding)
|
|
|
|
|
|
|
|
expect(vnode).toBe(_vnode)
|
|
|
|
expect(prevVNode).toBe(null)
|
|
|
|
}) as DirectiveHook)
|
|
|
|
|
|
|
|
const mounted = jest.fn(((el, binding, vnode, prevVNode) => {
|
|
|
|
expect(el.tag).toBe('div')
|
|
|
|
// should be inserted now
|
|
|
|
expect(el.parentNode).toBe(root)
|
|
|
|
expect(root.children[0]).toBe(el)
|
|
|
|
|
|
|
|
assertBindings(binding)
|
|
|
|
|
|
|
|
expect(vnode).toBe(_vnode)
|
|
|
|
expect(prevVNode).toBe(null)
|
|
|
|
}) as DirectiveHook)
|
|
|
|
|
|
|
|
const beforeUpdate = jest.fn(((el, binding, vnode, prevVNode) => {
|
|
|
|
expect(el.tag).toBe('div')
|
|
|
|
expect(el.parentNode).toBe(root)
|
|
|
|
expect(root.children[0]).toBe(el)
|
|
|
|
|
|
|
|
// node should not have been updated yet
|
|
|
|
expect(el.children[0].text).toBe(`${count.value - 1}`)
|
|
|
|
|
|
|
|
assertBindings(binding)
|
|
|
|
|
|
|
|
expect(vnode).toBe(_vnode)
|
|
|
|
expect(prevVNode).toBe(_prevVnode)
|
|
|
|
}) as DirectiveHook)
|
|
|
|
|
|
|
|
const updated = jest.fn(((el, binding, vnode, prevVNode) => {
|
|
|
|
expect(el.tag).toBe('div')
|
|
|
|
expect(el.parentNode).toBe(root)
|
|
|
|
expect(root.children[0]).toBe(el)
|
|
|
|
|
|
|
|
// node should have been updated
|
|
|
|
expect(el.children[0].text).toBe(`${count.value}`)
|
|
|
|
|
|
|
|
assertBindings(binding)
|
|
|
|
|
|
|
|
expect(vnode).toBe(_vnode)
|
|
|
|
expect(prevVNode).toBe(_prevVnode)
|
|
|
|
}) as DirectiveHook)
|
|
|
|
|
|
|
|
const beforeUnmount = jest.fn(((el, binding, vnode, prevVNode) => {
|
|
|
|
expect(el.tag).toBe('div')
|
|
|
|
// should be removed now
|
|
|
|
expect(el.parentNode).toBe(root)
|
|
|
|
expect(root.children[0]).toBe(el)
|
|
|
|
|
|
|
|
assertBindings(binding)
|
|
|
|
|
|
|
|
expect(vnode).toBe(_vnode)
|
|
|
|
expect(prevVNode).toBe(null)
|
|
|
|
}) as DirectiveHook)
|
|
|
|
|
|
|
|
const unmounted = jest.fn(((el, binding, vnode, prevVNode) => {
|
|
|
|
expect(el.tag).toBe('div')
|
|
|
|
// should have been removed
|
|
|
|
expect(el.parentNode).toBe(null)
|
|
|
|
expect(root.children.length).toBe(0)
|
|
|
|
|
|
|
|
assertBindings(binding)
|
|
|
|
|
|
|
|
expect(vnode).toBe(_vnode)
|
|
|
|
expect(prevVNode).toBe(null)
|
|
|
|
}) as DirectiveHook)
|
|
|
|
|
2019-09-07 00:58:31 +08:00
|
|
|
let _instance: ComponentInternalInstance | null = null
|
2019-09-03 00:09:29 +08:00
|
|
|
let _vnode: VNode | null = null
|
|
|
|
let _prevVnode: VNode | null = null
|
|
|
|
const Comp = {
|
|
|
|
setup() {
|
|
|
|
_instance = currentInstance
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
_prevVnode = _vnode
|
|
|
|
_vnode = applyDirectives(h('div', count.value), [
|
2019-09-04 06:11:04 +08:00
|
|
|
[
|
|
|
|
{
|
|
|
|
beforeMount,
|
|
|
|
mounted,
|
|
|
|
beforeUpdate,
|
|
|
|
updated,
|
|
|
|
beforeUnmount,
|
|
|
|
unmounted
|
|
|
|
},
|
|
|
|
// value
|
|
|
|
count.value,
|
|
|
|
// argument
|
|
|
|
'foo',
|
|
|
|
// modifiers
|
|
|
|
{ ok: true }
|
|
|
|
]
|
2019-09-03 00:09:29 +08:00
|
|
|
])
|
|
|
|
return _vnode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Comp), root)
|
|
|
|
|
|
|
|
expect(beforeMount).toHaveBeenCalled()
|
|
|
|
expect(mounted).toHaveBeenCalled()
|
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(beforeUpdate).toHaveBeenCalled()
|
|
|
|
expect(updated).toHaveBeenCalled()
|
|
|
|
|
|
|
|
render(null, root)
|
|
|
|
expect(beforeUnmount).toHaveBeenCalled()
|
|
|
|
expect(unmounted).toHaveBeenCalled()
|
|
|
|
})
|
2019-10-16 14:12:26 +08:00
|
|
|
|
|
|
|
it('should work with a function directive', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
|
|
|
|
function assertBindings(binding: DirectiveBinding) {
|
|
|
|
expect(binding.value).toBe(count.value)
|
|
|
|
expect(binding.arg).toBe('foo')
|
|
|
|
expect(binding.instance).toBe(_instance && _instance.renderProxy)
|
|
|
|
expect(binding.modifiers && binding.modifiers.ok).toBe(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn = jest.fn(((el, binding, vnode, prevVNode) => {
|
|
|
|
expect(el.tag).toBe('div')
|
|
|
|
expect(el.parentNode).toBe(root)
|
|
|
|
|
|
|
|
assertBindings(binding)
|
|
|
|
|
|
|
|
expect(vnode).toBe(_vnode)
|
|
|
|
expect(prevVNode).toBe(_prevVnode)
|
|
|
|
}) as DirectiveHook)
|
|
|
|
|
|
|
|
let _instance: ComponentInternalInstance | null = null
|
|
|
|
let _vnode: VNode | null = null
|
|
|
|
let _prevVnode: VNode | null = null
|
|
|
|
const Comp = {
|
|
|
|
setup() {
|
|
|
|
_instance = currentInstance
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
_prevVnode = _vnode
|
|
|
|
_vnode = applyDirectives(h('div', count.value), [
|
|
|
|
[
|
|
|
|
fn,
|
|
|
|
// value
|
|
|
|
count.value,
|
|
|
|
// argument
|
|
|
|
'foo',
|
|
|
|
// modifiers
|
|
|
|
{ ok: true }
|
|
|
|
]
|
|
|
|
])
|
|
|
|
return _vnode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Comp), root)
|
|
|
|
|
|
|
|
expect(fn).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(fn).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
2019-09-01 10:18:21 +08:00
|
|
|
})
|