vue2/test/unit/specs/directive_spec.js

192 lines
4.2 KiB
JavaScript
Raw Permalink Normal View History

var Vue = require('src')
var Directive = require('src/directive')
2014-08-19 06:03:01 +08:00
var nextTick = Vue.nextTick
describe('Directive', function () {
var el, vm, def
2014-08-19 06:03:01 +08:00
beforeEach(function () {
el = document.createElement('div')
2014-08-19 06:03:01 +08:00
def = {
params: ['foo', 'keBab'],
paramWatchers: {
foo: jasmine.createSpy('foo')
},
2014-08-19 06:03:01 +08:00
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,
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) {
2015-09-04 22:04:56 +08:00
var d = new Directive({
name: 'test',
def: def,
2014-08-19 06:03:01 +08:00
expression: 'a',
modifiers: {
literal: false
},
2015-09-04 22:04:56 +08:00
filters: [{ name: 'test' }]
}, vm, el)
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.expression).toBe('a')
2015-09-04 22:04:56 +08:00
expect(d.literal).toBe(false)
2014-08-19 06:03:01 +08:00
// init calls
expect(def.bind).toHaveBeenCalled()
expect(def.update).toHaveBeenCalledWith(2)
expect(d._bound).toBe(true)
vm.a = 2
nextTick(function () {
expect(def.update).toHaveBeenCalledWith(4, 2)
// teardown
d._teardown()
expect(def.unbind).toHaveBeenCalled()
expect(d._bound).toBe(false)
expect(d._watcher).toBe(null)
2014-08-19 06:03:01 +08:00
done()
})
})
2015-09-04 22:04:56 +08:00
it('literal', function () {
var d = new Directive({
name: 'test',
expression: 'a',
raw: 'a',
def: def,
modifiers: {
literal: true
}
2015-09-04 22:04:56 +08:00
}, vm, el)
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()
2015-09-04 22:04:56 +08:00
expect(d.update).toHaveBeenCalledWith('a')
2014-08-19 06:03:01 +08:00
})
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()
}
}
2015-09-04 22:04:56 +08:00
var d = new Directive({
name: 'test',
2014-09-15 10:38:40 +08:00
expression: 'a++',
2015-09-04 22:04:56 +08:00
filters: [{name: 'test'}],
def: def
}, vm, el)
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
}
}
2015-09-04 22:04:56 +08:00
var d = new Directive({
name: 'test',
2014-08-19 06:03:01 +08:00
expression: 'a',
2015-09-04 22:04:56 +08:00
filters: [{name: 'test'}],
def: def
}, vm, el)
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 () {
// 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-09-04 22:04:56 +08:00
var d = new Directive({
name: 'test',
expression: 'b',
def: def
}, vm, el)
2015-08-31 01:26:20 +08:00
d._bind()
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 () {
2015-09-04 22:04:56 +08:00
var d = new Directive({
name: 'test',
expression: 'a',
def: def.update
}, vm, el)
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()
})
it('static params', function () {
el.setAttribute('foo', 'hello')
el.setAttribute('ke-bab', 'yo')
var d = new Directive({
name: 'test',
def: def,
expression: 'a'
}, vm, el)
d._bind()
expect(d.params.foo).toBe('hello')
expect(d.params.keBab).toBe('yo')
})
it('dynamic params', function (done) {
el.setAttribute(':foo', 'a')
el.setAttribute(':ke-bab', '123')
var d = new Directive({
name: 'test',
def: def,
expression: 'a'
}, vm, el)
d._bind()
expect(d.params.foo).toBe(vm.a)
expect(d.params.keBab).toBe(123)
vm.a = 2
nextTick(function () {
expect(def.paramWatchers.foo).toHaveBeenCalledWith(2, 1)
expect(d.params.foo).toBe(vm.a)
done()
})
})
2015-07-02 20:59:20 +08:00
})