mirror of
https://gitee.com/vuejs/vue.git
synced 2024-12-05 05:27:59 +08:00
directive option tests
This commit is contained in:
parent
99a96d3c37
commit
d62eacd1da
@ -206,7 +206,7 @@ const defaultStrat = function (parentVal: any, childVal: any): any {
|
|||||||
* Make sure component options get converted to actual
|
* Make sure component options get converted to actual
|
||||||
* constructors.
|
* constructors.
|
||||||
*/
|
*/
|
||||||
function guardComponents (options: Object) {
|
function normalizeComponents (options: Object) {
|
||||||
if (options.components) {
|
if (options.components) {
|
||||||
const components = options.components
|
const components = options.components
|
||||||
let def
|
let def
|
||||||
@ -230,7 +230,7 @@ function guardComponents (options: Object) {
|
|||||||
* Ensure all props option syntax are normalized into the
|
* Ensure all props option syntax are normalized into the
|
||||||
* Object-based format.
|
* Object-based format.
|
||||||
*/
|
*/
|
||||||
function guardProps (options: Object) {
|
function normalizeProps (options: Object) {
|
||||||
const props = options.props
|
const props = options.props
|
||||||
if (!props) return
|
if (!props) return
|
||||||
const res = {}
|
const res = {}
|
||||||
@ -261,12 +261,13 @@ function guardProps (options: Object) {
|
|||||||
/**
|
/**
|
||||||
* Normalize raw function directives into object format.
|
* Normalize raw function directives into object format.
|
||||||
*/
|
*/
|
||||||
function guardDirectives (options: Object) {
|
function normalizeDirectives (options: Object) {
|
||||||
const dirs = options.directives
|
const dirs = options.directives
|
||||||
if (dirs) {
|
if (dirs) {
|
||||||
for (const key in dirs) {
|
for (const key in dirs) {
|
||||||
if (typeof dirs[key] === 'function') {
|
const def = dirs[key]
|
||||||
dirs[key] = { update: dirs[key] }
|
if (typeof def === 'function') {
|
||||||
|
dirs[key] = { bind: def, update: def }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -281,9 +282,9 @@ export function mergeOptions (
|
|||||||
child: Object,
|
child: Object,
|
||||||
vm?: Component
|
vm?: Component
|
||||||
): Object {
|
): Object {
|
||||||
guardComponents(child)
|
normalizeComponents(child)
|
||||||
guardProps(child)
|
normalizeProps(child)
|
||||||
guardDirectives(child)
|
normalizeDirectives(child)
|
||||||
if (process.env.NODE_ENV !== 'production') {
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
if (child.propsData && !vm) {
|
if (child.propsData && !vm) {
|
||||||
warn('propsData can only be used as an instantiation option.')
|
warn('propsData can only be used as an instantiation option.')
|
||||||
|
92
test/unit/features/options/directives.spec.js
Normal file
92
test/unit/features/options/directives.spec.js
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
import Vue from 'vue'
|
||||||
|
|
||||||
|
describe('Options directives', () => {
|
||||||
|
it('basic usage', done => {
|
||||||
|
const bindSpy = jasmine.createSpy('bind')
|
||||||
|
const updateSpy = jasmine.createSpy('update')
|
||||||
|
const postupdateSpy = jasmine.createSpy('postupdate')
|
||||||
|
const unbindSpy = jasmine.createSpy('unbind')
|
||||||
|
|
||||||
|
const assertContext = (el, binding, vnode) => {
|
||||||
|
expect(vnode.context).toBe(vm)
|
||||||
|
expect(binding.arg).toBe('arg')
|
||||||
|
expect(binding.modifiers).toEqual({ hello: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
const vm = new Vue({
|
||||||
|
template: '<div v-if="ok" v-test:arg.hello="a">{{ msg }}</div>',
|
||||||
|
data: {
|
||||||
|
msg: 'hi',
|
||||||
|
a: 'foo',
|
||||||
|
ok: true
|
||||||
|
},
|
||||||
|
directives: {
|
||||||
|
test: {
|
||||||
|
bind (el, binding, vnode) {
|
||||||
|
bindSpy()
|
||||||
|
assertContext(el, binding, vnode)
|
||||||
|
expect(binding.value).toBe('foo')
|
||||||
|
expect(binding.oldValue).toBeUndefined()
|
||||||
|
},
|
||||||
|
update (el, binding, vnode, oldVnode) {
|
||||||
|
updateSpy()
|
||||||
|
assertContext(el, binding, vnode)
|
||||||
|
expect(el).toBe(vm.$el)
|
||||||
|
expect(oldVnode).toBe(vm._vnode)
|
||||||
|
expect(oldVnode).not.toBe(vnode)
|
||||||
|
expect(binding.value).toBe('bar')
|
||||||
|
expect(binding.oldValue).toBe('foo')
|
||||||
|
},
|
||||||
|
postupdate (el, binding, vnode) {
|
||||||
|
postupdateSpy()
|
||||||
|
assertContext(el, binding, vnode)
|
||||||
|
},
|
||||||
|
unbind (el, binding, vnode) {
|
||||||
|
unbindSpy()
|
||||||
|
assertContext(el, binding, vnode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
vm.$mount()
|
||||||
|
expect(bindSpy).toHaveBeenCalled()
|
||||||
|
expect(updateSpy).not.toHaveBeenCalled()
|
||||||
|
expect(postupdateSpy).not.toHaveBeenCalled()
|
||||||
|
expect(unbindSpy).not.toHaveBeenCalled()
|
||||||
|
vm.a = 'bar'
|
||||||
|
waitForUpdate(() => {
|
||||||
|
expect(updateSpy).toHaveBeenCalled()
|
||||||
|
expect(postupdateSpy).toHaveBeenCalled()
|
||||||
|
expect(unbindSpy).not.toHaveBeenCalled()
|
||||||
|
vm.msg = 'bye'
|
||||||
|
}).then(() => {
|
||||||
|
expect(postupdateSpy.calls.count()).toBe(2)
|
||||||
|
vm.ok = false
|
||||||
|
}).then(() => {
|
||||||
|
expect(unbindSpy).toHaveBeenCalled()
|
||||||
|
}).then(done)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('function shorthand', done => {
|
||||||
|
const spy = jasmine.createSpy('directive')
|
||||||
|
const vm = new Vue({
|
||||||
|
template: '<div v-test:arg.hello="a"></div>',
|
||||||
|
data: { a: 'foo' },
|
||||||
|
directives: {
|
||||||
|
test (el, binding, vnode) {
|
||||||
|
expect(vnode.context).toBe(vm)
|
||||||
|
expect(binding.arg).toBe('arg')
|
||||||
|
expect(binding.modifiers).toEqual({ hello: true })
|
||||||
|
spy(binding.value, binding.oldValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
vm.$mount()
|
||||||
|
expect(spy).toHaveBeenCalledWith('foo', undefined)
|
||||||
|
vm.a = 'bar'
|
||||||
|
waitForUpdate(() => {
|
||||||
|
expect(spy).toHaveBeenCalledWith('bar', 'foo')
|
||||||
|
}).then(done)
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user