2014-03-06 12:44:16 +08:00
|
|
|
describe('Binding', function () {
|
2013-08-29 02:18:03 +08:00
|
|
|
|
2013-12-25 04:34:22 +08:00
|
|
|
var Binding = require('vue/src/binding'),
|
|
|
|
nextTick = require('vue/src/utils').nextTick
|
2013-11-13 02:45:54 +08:00
|
|
|
|
2013-08-29 02:18:03 +08:00
|
|
|
describe('instantiation', function () {
|
|
|
|
|
|
|
|
it('should have root==true with a root key', function () {
|
|
|
|
var b = new Binding(null, 'test')
|
|
|
|
assert.ok(b.root)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should have root==false with a non-root key', function () {
|
|
|
|
var b = new Binding(null, 'test.key')
|
|
|
|
assert.ok(!b.root)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should have root==false if its key is an expression', function () {
|
|
|
|
var b = new Binding(null, 'test', true)
|
|
|
|
assert.ok(!b.root)
|
|
|
|
})
|
|
|
|
|
2014-02-14 14:51:23 +08:00
|
|
|
it('should have dirs, subs and deps as Arrays', function () {
|
2013-08-29 02:18:03 +08:00
|
|
|
var b = new Binding(null, 'test')
|
2014-02-14 14:51:23 +08:00
|
|
|
assert.ok(Array.isArray(b.dirs), 'dirs')
|
2013-08-29 10:46:13 +08:00
|
|
|
assert.ok(Array.isArray(b.subs), 'subs')
|
|
|
|
assert.ok(Array.isArray(b.deps), 'deps')
|
2013-08-29 02:18:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('.update()', function () {
|
|
|
|
|
|
|
|
var b = new Binding(null, 'test'),
|
|
|
|
val = 123,
|
|
|
|
updated = 0,
|
|
|
|
pubbed = false,
|
|
|
|
numInstances = 3,
|
|
|
|
instance = {
|
2014-03-28 01:02:22 +08:00
|
|
|
$update: function (value) {
|
2013-08-29 02:18:03 +08:00
|
|
|
updated += value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var i = 0; i < numInstances; i++) {
|
2014-02-14 14:51:23 +08:00
|
|
|
b.dirs.push(instance)
|
2013-08-29 02:18:03 +08:00
|
|
|
}
|
|
|
|
b.pub = function () {
|
|
|
|
pubbed = true
|
|
|
|
}
|
2013-12-25 04:34:22 +08:00
|
|
|
|
|
|
|
before(function (done) {
|
|
|
|
b.update(val)
|
|
|
|
nextTick(function () {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2013-08-29 02:18:03 +08:00
|
|
|
|
|
|
|
it('should set the binding\'s value', function () {
|
2013-08-29 10:46:13 +08:00
|
|
|
assert.strictEqual(b.value, val)
|
2013-08-29 02:18:03 +08:00
|
|
|
})
|
|
|
|
|
2014-02-14 14:51:23 +08:00
|
|
|
it('should update the binding\'s directives', function () {
|
2013-08-29 10:46:13 +08:00
|
|
|
assert.strictEqual(updated, val * numInstances)
|
2013-08-29 02:18:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should call the binding\'s pub() method', function () {
|
|
|
|
assert.ok(pubbed)
|
|
|
|
})
|
|
|
|
|
2014-01-27 12:04:22 +08:00
|
|
|
it('should not set the value if it is computed unless a function', function () {
|
|
|
|
var b1 = new Binding(null, 'test'),
|
|
|
|
b2 = new Binding(null, 'test', false, true)
|
|
|
|
b1.isComputed = true
|
|
|
|
b2.isComputed = true
|
|
|
|
var ov = { $get: function () {} }
|
|
|
|
b1.value = ov
|
|
|
|
b2.value = function () {}
|
|
|
|
b1.update(1)
|
|
|
|
b2.update(1)
|
|
|
|
assert.strictEqual(b1.value, ov)
|
|
|
|
assert.strictEqual(b2.value, 1)
|
|
|
|
})
|
|
|
|
|
2013-08-29 02:18:03 +08:00
|
|
|
})
|
|
|
|
|
2014-01-27 12:04:22 +08:00
|
|
|
describe('.val()', function () {
|
|
|
|
|
|
|
|
it('should return the raw value for non-computed and function bindings', function () {
|
|
|
|
var b1 = new Binding(null, 'test'),
|
|
|
|
b2 = new Binding(null, 'test', false, true)
|
|
|
|
b2.isComputed = true
|
|
|
|
b1.value = 1
|
|
|
|
b2.value = 2
|
|
|
|
assert.strictEqual(b1.val(), 1)
|
|
|
|
assert.strictEqual(b2.val(), 2)
|
|
|
|
})
|
2013-08-29 02:18:03 +08:00
|
|
|
|
2014-01-27 12:04:22 +08:00
|
|
|
it('should return computed value for computed bindings', function () {
|
|
|
|
var b = new Binding(null, 'test')
|
|
|
|
b.isComputed = true
|
|
|
|
b.value = {
|
|
|
|
$get: function () {
|
|
|
|
return 3
|
2013-08-29 02:18:03 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-27 12:04:22 +08:00
|
|
|
assert.strictEqual(b.val(), 3)
|
2013-12-25 04:34:22 +08:00
|
|
|
})
|
2013-08-29 02:18:03 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('.pub()', function () {
|
|
|
|
|
|
|
|
var b = new Binding(null, 'test'),
|
|
|
|
refreshed = 0,
|
|
|
|
numSubs = 3,
|
|
|
|
sub = {
|
2014-01-27 12:04:22 +08:00
|
|
|
update: function () {
|
2013-08-29 02:18:03 +08:00
|
|
|
refreshed++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var i = 0; i < numSubs; i++) {
|
|
|
|
b.subs.push(sub)
|
|
|
|
}
|
|
|
|
b.pub()
|
|
|
|
|
2014-01-27 12:04:22 +08:00
|
|
|
it('should call update() of all subscribers', function () {
|
2013-08-29 10:46:13 +08:00
|
|
|
assert.strictEqual(refreshed, numSubs)
|
2013-08-29 02:18:03 +08:00
|
|
|
})
|
|
|
|
|
2013-08-27 07:28:03 +08:00
|
|
|
})
|
2013-08-29 02:18:03 +08:00
|
|
|
|
|
|
|
describe('.unbind()', function () {
|
|
|
|
|
|
|
|
var b = new Binding(null, 'test'),
|
|
|
|
unbound = 0,
|
|
|
|
numInstances = 3,
|
|
|
|
instance = {
|
2014-03-28 01:02:22 +08:00
|
|
|
$unbind: function () {
|
2013-08-29 02:18:03 +08:00
|
|
|
unbound++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var i = 0; i < numInstances; i++) {
|
2014-02-14 14:51:23 +08:00
|
|
|
b.dirs.push(instance)
|
2013-08-29 02:18:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// mock deps
|
|
|
|
var dep1 = { subs: [1, 2, 3, b] },
|
|
|
|
dep2 = { subs: [2, b, 4, 6] }
|
|
|
|
b.deps.push(dep1, dep2)
|
|
|
|
|
|
|
|
b.unbind()
|
|
|
|
|
2014-02-14 14:51:23 +08:00
|
|
|
it('should call unbind() of all directives', function () {
|
2013-08-29 10:46:13 +08:00
|
|
|
assert.strictEqual(unbound, numInstances)
|
2013-08-29 02:18:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove itself from the subs list of all its dependencies', function () {
|
2013-08-29 10:46:13 +08:00
|
|
|
var notInSubs1 = dep1.subs.indexOf(b) === -1,
|
|
|
|
notInSubs2 = dep2.subs.indexOf(b) === -1
|
|
|
|
assert.ok(notInSubs1)
|
|
|
|
assert.ok(notInSubs2)
|
2013-08-29 02:18:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2013-08-27 07:28:03 +08:00
|
|
|
})
|