vue2/test/unit/specs/binding.js

140 lines
3.7 KiB
JavaScript
Raw Normal View History

var Binding = require('seed/src/binding')
2013-08-27 07:28:03 +08:00
describe('UNIT: Binding', function () {
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)
})
it('should have instances, subs and deps as Arrays', function () {
var b = new Binding(null, 'test')
2013-08-29 10:46:13 +08:00
assert.ok(Array.isArray(b.instances), 'instances')
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 = {
update: function (value) {
updated += value
}
}
for (var i = 0; i < numInstances; i++) {
b.instances.push(instance)
}
b.pub = function () {
pubbed = true
}
b.update(val)
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
})
it('should update the binding\'s instances', 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)
})
})
describe('.refresh()', function () {
var b = new Binding(null, 'test'),
refreshed = 0,
numInstances = 3,
instance = {
refresh: function () {
refreshed++
}
}
for (var i = 0; i < numInstances; i++) {
b.instances.push(instance)
}
b.refresh()
it('should call refresh() of all instances', function () {
2013-08-29 10:46:13 +08:00
assert.strictEqual(refreshed, numInstances)
2013-08-29 02:18:03 +08:00
})
})
describe('.pub()', function () {
var b = new Binding(null, 'test'),
refreshed = 0,
numSubs = 3,
sub = {
refresh: function () {
refreshed++
}
}
for (var i = 0; i < numSubs; i++) {
b.subs.push(sub)
}
b.pub()
it('should call refresh() 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 = {
unbind: function () {
unbound++
}
}
for (var i = 0; i < numInstances; i++) {
b.instances.push(instance)
}
// mock deps
var dep1 = { subs: [1, 2, 3, b] },
dep2 = { subs: [2, b, 4, 6] }
b.deps.push(dep1, dep2)
b.unbind()
it('should call unbind() of all instances', 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
})