DirectiveHook,
VNode,
DirectiveBinding,
- nextTick
+ nextTick,
+ Directive,
+ ComponentPublicInstance,
+ getCurrentInstance
} from '@vue/runtime-test'
import { currentInstance, ComponentInternalInstance } from '../src/component'
expect(beforeUpdate).toHaveBeenCalledTimes(1)
expect(count.value).toBe(1)
})
+
+ test('should have currentInstance available', async () => {
+ let instance: ComponentInternalInstance | null
+ let bindingInstance: ComponentPublicInstance | null
+ const beforeMount: Directive = (_, binding) => {
+ instance = getCurrentInstance()
+ bindingInstance = binding.instance
+ }
+ const App = {
+ render() {
+ return withDirectives(h('p', 'Test'), [
+ [
+ {
+ beforeMount
+ }
+ ]
+ ])
+ }
+ }
+
+ const root = nodeOps.createElement('div')
+ render(h(App), root)
+ expect(instance!).not.toBe(null)
+ expect(instance!.proxy).toBe(bindingInstance!)
+ })
})
import { VNode } from './vnode'
import { isFunction, EMPTY_OBJ, makeMap } from '@vue/shared'
import { warn } from './warning'
-import { ComponentInternalInstance, Data } from './component'
+import {
+ ComponentInternalInstance,
+ Data,
+ setCurrentInstance,
+ unsetCurrentInstance
+} from './component'
import { currentRenderingInstance } from './componentRenderContext'
import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
import { ComponentPublicInstance } from './componentPublicInstance'
// disable tracking inside all lifecycle hooks
// since they can potentially be called inside effects.
pauseTracking()
+ instance && setCurrentInstance(instance)
callWithAsyncErrorHandling(hook, instance, ErrorCodes.DIRECTIVE_HOOK, [
vnode.el,
binding,
vnode,
prevVNode
])
+ unsetCurrentInstance()
resetTracking()
}
}