| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- import {
- h,
- withDirectives,
- ref,
- render,
- nodeOps,
- DirectiveHook,
- VNode,
- DirectiveBinding,
- nextTick
- } from '@vue/runtime-test'
- import { currentInstance, ComponentInternalInstance } from '../src/component'
- describe('directives', () => {
- 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.proxy)
- 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)
- const dir = {
- beforeMount,
- mounted,
- beforeUpdate,
- updated,
- beforeUnmount,
- unmounted
- }
- let _instance: ComponentInternalInstance | null = null
- let _vnode: VNode | null = null
- let _prevVnode: VNode | null = null
- const Comp = {
- setup() {
- _instance = currentInstance
- },
- render() {
- _prevVnode = _vnode
- _vnode = withDirectives(h('div', count.value), [
- [
- dir,
- // value
- count.value,
- // argument
- 'foo',
- // modifiers
- { ok: true }
- ]
- ])
- return _vnode
- }
- }
- const root = nodeOps.createElement('div')
- render(h(Comp), root)
- expect(beforeMount).toHaveBeenCalledTimes(1)
- expect(mounted).toHaveBeenCalledTimes(1)
- count.value++
- await nextTick()
- expect(beforeUpdate).toHaveBeenCalledTimes(1)
- expect(updated).toHaveBeenCalledTimes(1)
- render(null, root)
- expect(beforeUnmount).toHaveBeenCalledTimes(1)
- expect(unmounted).toHaveBeenCalledTimes(1)
- })
- 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.proxy)
- 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 = withDirectives(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)
- })
- it('should work on component vnode', 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.proxy)
- 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.type).toBe(_vnode!.type)
- 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.type).toBe(_vnode!.type)
- 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.type).toBe(_vnode!.type)
- expect(prevVNode!.type).toBe(_prevVnode!.type)
- }) 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.type).toBe(_vnode!.type)
- expect(prevVNode!.type).toBe(_prevVnode!.type)
- }) 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.type).toBe(_vnode!.type)
- 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.type).toBe(_vnode!.type)
- expect(prevVNode).toBe(null)
- }) as DirectiveHook)
- const dir = {
- beforeMount,
- mounted,
- beforeUpdate,
- updated,
- beforeUnmount,
- unmounted
- }
- let _instance: ComponentInternalInstance | null = null
- let _vnode: VNode | null = null
- let _prevVnode: VNode | null = null
- const Child = (props: { count: number }) => {
- _prevVnode = _vnode
- _vnode = h('div', props.count)
- return _vnode
- }
- const Comp = {
- setup() {
- _instance = currentInstance
- },
- render() {
- return withDirectives(h(Child, { count: count.value }), [
- [
- dir,
- // value
- count.value,
- // argument
- 'foo',
- // modifiers
- { ok: true }
- ]
- ])
- }
- }
- const root = nodeOps.createElement('div')
- render(h(Comp), root)
- expect(beforeMount).toHaveBeenCalledTimes(1)
- expect(mounted).toHaveBeenCalledTimes(1)
- count.value++
- await nextTick()
- expect(beforeUpdate).toHaveBeenCalledTimes(1)
- expect(updated).toHaveBeenCalledTimes(1)
- render(null, root)
- expect(beforeUnmount).toHaveBeenCalledTimes(1)
- expect(unmounted).toHaveBeenCalledTimes(1)
- })
- })
|