2014-08-19 06:03:01 +08:00
|
|
|
var Vue = require('../../../src/vue')
|
|
|
|
var Directive = require('../../../src/directive')
|
|
|
|
var nextTick = Vue.nextTick
|
|
|
|
|
|
|
|
describe('Directive', function () {
|
|
|
|
|
|
|
|
var el = {} // simply a mock to be able to run in Node
|
|
|
|
var vm, def
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
def = {
|
|
|
|
bind: jasmine.createSpy('bind'),
|
|
|
|
update: jasmine.createSpy('update'),
|
|
|
|
unbind: jasmine.createSpy('unbind')
|
|
|
|
}
|
|
|
|
vm = new Vue({
|
2015-07-02 20:59:20 +08:00
|
|
|
data: {
|
|
|
|
a: 1,
|
2014-12-01 03:16:22 +08:00
|
|
|
b: { c: { d: 2 }}
|
2014-08-19 06:03:01 +08:00
|
|
|
},
|
|
|
|
filters: {
|
|
|
|
test: function (v) {
|
|
|
|
return v * 2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
directives: {
|
|
|
|
test: def
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('normal', function (done) {
|
|
|
|
var d = new Directive('test', el, vm, {
|
|
|
|
expression: 'a',
|
|
|
|
arg: 'someArg',
|
2015-07-02 20:59:20 +08:00
|
|
|
filters: [{name: 'test'}]
|
2014-08-23 03:03:52 +08:00
|
|
|
}, def)
|
2015-08-31 01:26:20 +08:00
|
|
|
d._bind()
|
2014-08-19 06:03:01 +08:00
|
|
|
// properties
|
|
|
|
expect(d.el).toBe(el)
|
|
|
|
expect(d.name).toBe('test')
|
|
|
|
expect(d.vm).toBe(vm)
|
|
|
|
expect(d.arg).toBe('someArg')
|
|
|
|
expect(d.expression).toBe('a')
|
|
|
|
// init calls
|
|
|
|
expect(def.bind).toHaveBeenCalled()
|
|
|
|
expect(def.update).toHaveBeenCalledWith(2)
|
|
|
|
expect(d._bound).toBe(true)
|
|
|
|
// update
|
|
|
|
vm.a = 2
|
|
|
|
nextTick(function () {
|
|
|
|
expect(def.update).toHaveBeenCalledWith(4, 2)
|
|
|
|
// teardown
|
|
|
|
d._teardown()
|
|
|
|
expect(def.unbind).toHaveBeenCalled()
|
|
|
|
expect(d._bound).toBe(false)
|
2014-08-29 23:26:01 +08:00
|
|
|
expect(d._watcher).toBe(null)
|
2014-08-19 06:03:01 +08:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('static literal', function () {
|
|
|
|
def.isLiteral = true
|
|
|
|
var d = new Directive('test', el, vm, {
|
|
|
|
expression: 'a'
|
2014-08-23 03:03:52 +08:00
|
|
|
}, def)
|
2015-08-31 01:26:20 +08:00
|
|
|
d._bind()
|
2014-08-19 06:03:01 +08:00
|
|
|
expect(d._watcher).toBeUndefined()
|
|
|
|
expect(d.expression).toBe('a')
|
|
|
|
expect(d.bind).toHaveBeenCalled()
|
|
|
|
expect(d.update).not.toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('static literal, interpolate with no update', function () {
|
|
|
|
def.isLiteral = true
|
|
|
|
delete def.update
|
|
|
|
var d = new Directive('test', el, vm, {
|
|
|
|
expression: '{{a}}'
|
2014-08-23 03:03:52 +08:00
|
|
|
}, def)
|
2015-08-31 01:26:20 +08:00
|
|
|
d._bind()
|
2014-08-19 06:03:01 +08:00
|
|
|
expect(d._watcher).toBeUndefined()
|
|
|
|
expect(d.expression).toBe(1)
|
|
|
|
expect(d.bind).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('dynamic literal', function (done) {
|
2014-10-07 11:43:07 +08:00
|
|
|
vm.a = '' // #468 dynamic literals with falsy initial
|
|
|
|
// should still create the watcher.
|
2014-08-19 06:03:01 +08:00
|
|
|
def.isLiteral = true
|
|
|
|
var d = new Directive('test', el, vm, {
|
|
|
|
expression: '{{a}}'
|
2014-08-23 03:03:52 +08:00
|
|
|
}, def)
|
2015-08-31 01:26:20 +08:00
|
|
|
d._bind()
|
2014-08-19 06:03:01 +08:00
|
|
|
expect(d._watcher).toBeDefined()
|
2014-10-07 11:43:07 +08:00
|
|
|
expect(d.expression).toBe('')
|
2014-08-19 06:03:01 +08:00
|
|
|
expect(def.bind).toHaveBeenCalled()
|
2014-10-07 11:43:07 +08:00
|
|
|
expect(def.update).toHaveBeenCalledWith('')
|
|
|
|
vm.a = 'aa'
|
2014-08-19 06:03:01 +08:00
|
|
|
nextTick(function () {
|
2014-10-07 11:43:07 +08:00
|
|
|
expect(def.update).toHaveBeenCalledWith('aa', '')
|
2014-08-19 06:03:01 +08:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-10-20 01:11:02 +08:00
|
|
|
it('inline statement', function () {
|
|
|
|
def.acceptStatement = true
|
2014-09-15 10:38:40 +08:00
|
|
|
var spy = jasmine.createSpy()
|
|
|
|
vm.$options.filters.test = function (fn) {
|
|
|
|
spy()
|
|
|
|
return function () {
|
|
|
|
// call it twice
|
|
|
|
fn()
|
|
|
|
fn()
|
|
|
|
}
|
|
|
|
}
|
2014-08-19 06:03:01 +08:00
|
|
|
var d = new Directive('test', el, vm, {
|
2014-09-15 10:38:40 +08:00
|
|
|
expression: 'a++',
|
2015-07-02 20:59:20 +08:00
|
|
|
filters: [{name: 'test'}]
|
2014-08-23 03:03:52 +08:00
|
|
|
}, def)
|
2015-08-31 01:26:20 +08:00
|
|
|
d._bind()
|
2014-08-19 06:03:01 +08:00
|
|
|
expect(d._watcher).toBeUndefined()
|
|
|
|
expect(d.bind).toHaveBeenCalled()
|
|
|
|
var wrappedFn = d.update.calls.argsFor(0)[0]
|
|
|
|
expect(typeof wrappedFn).toBe('function')
|
|
|
|
// test invoke the wrapped fn
|
|
|
|
wrappedFn()
|
2014-09-15 10:38:40 +08:00
|
|
|
expect(vm.a).toBe(3)
|
2014-08-19 06:03:01 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('two-way', function (done) {
|
|
|
|
def.twoWay = true
|
|
|
|
vm.$options.filters.test = {
|
|
|
|
write: function (v) {
|
|
|
|
return v * 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var d = new Directive('test', el, vm, {
|
|
|
|
expression: 'a',
|
2015-07-02 20:59:20 +08:00
|
|
|
filters: [{name: 'test'}]
|
2014-08-23 03:03:52 +08:00
|
|
|
}, def)
|
2015-08-31 01:26:20 +08:00
|
|
|
d._bind()
|
2014-08-19 06:03:01 +08:00
|
|
|
d.set(2)
|
|
|
|
expect(vm.a).toBe(6)
|
|
|
|
nextTick(function () {
|
2015-05-21 03:35:54 +08:00
|
|
|
// should have no update calls
|
|
|
|
expect(def.update.calls.count()).toBe(1)
|
|
|
|
done()
|
2014-08-19 06:03:01 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-12-01 03:08:21 +08:00
|
|
|
it('deep', function (done) {
|
|
|
|
def.deep = true
|
2015-08-31 01:26:20 +08:00
|
|
|
var d = new Directive('test', el, vm, {
|
2014-12-01 03:08:21 +08:00
|
|
|
expression: 'b'
|
|
|
|
}, def)
|
2015-08-31 01:26:20 +08:00
|
|
|
d._bind()
|
2014-12-01 03:16:22 +08:00
|
|
|
vm.b.c.d = 3
|
2014-12-01 03:08:21 +08:00
|
|
|
nextTick(function () {
|
|
|
|
expect(def.update.calls.count()).toBe(2)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-09-04 08:27:22 +08:00
|
|
|
it('function def', function () {
|
|
|
|
var d = new Directive('test', el, vm, {
|
|
|
|
expression: 'a'
|
|
|
|
}, def.update)
|
2015-08-31 01:26:20 +08:00
|
|
|
d._bind()
|
2014-09-04 08:27:22 +08:00
|
|
|
expect(d.update).toBe(def.update)
|
|
|
|
expect(def.update).toHaveBeenCalled()
|
|
|
|
})
|
2015-09-04 05:42:26 +08:00
|
|
|
|
|
|
|
it('literal (new syntax)', function () {
|
|
|
|
var d = new Directive('test', el, vm, {
|
|
|
|
raw: 'a'
|
|
|
|
}, def.update, null, null, null, null, true)
|
|
|
|
d._bind()
|
|
|
|
expect(def.update).toHaveBeenCalledWith('a')
|
|
|
|
})
|
2015-07-02 20:59:20 +08:00
|
|
|
})
|