unit tests pass for computed property rewrite

This commit is contained in:
Evan You 2014-01-26 23:04:22 -05:00
parent 7a6169caf9
commit 3924044338
6 changed files with 63 additions and 97 deletions

View File

@ -1,5 +1,4 @@
var batcher = require('./batcher'), var batcher = require('./batcher'),
utils = require('./utils'),
id = 0 id = 0
/** /**
@ -40,7 +39,7 @@ BindingProto.update = function (value) {
*/ */
BindingProto._update = function () { BindingProto._update = function () {
var i = this.instances.length, var i = this.instances.length,
value = this.eval() value = this.val()
while (i--) { while (i--) {
this.instances[i].update(value) this.instances[i].update(value)
} }
@ -51,7 +50,7 @@ BindingProto._update = function () {
* Return the valuated value regardless * Return the valuated value regardless
* of whether it is computed or not * of whether it is computed or not
*/ */
BindingProto.eval = function () { BindingProto.val = function () {
return this.isComputed && !this.isFn return this.isComputed && !this.isFn
? this.value.$get() ? this.value.$get()
: this.value : this.value

View File

@ -433,7 +433,7 @@ CompilerProto.bindDirective = function (directive) {
} }
// set initial value // set initial value
directive.update(binding.eval(), true) directive.update(binding.val(), true)
} }
/** /**

View File

@ -20,8 +20,8 @@ describe('Batcher', function () {
updateCount = 0 updateCount = 0
var b1 = mockBinding(1), var b1 = mockBinding(1),
b2 = mockBinding(2) b2 = mockBinding(2)
batcher.queue(b1, 'update') batcher.queue(b1)
batcher.queue(b2, 'update') batcher.queue(b2)
assert.strictEqual(updateCount, 0) assert.strictEqual(updateCount, 0)
assert.notOk(b1.updated) assert.notOk(b1.updated)
assert.notOk(b2.updated) assert.notOk(b2.updated)
@ -40,8 +40,8 @@ describe('Batcher', function () {
updateCount = 0 updateCount = 0
var b1 = mockBinding(1), var b1 = mockBinding(1),
b2 = mockBinding(1) b2 = mockBinding(1)
batcher.queue(b1, 'update') batcher.queue(b1)
batcher.queue(b2, 'update') batcher.queue(b2)
nextTick(function () { nextTick(function () {
assert.strictEqual(updateCount, 1) assert.strictEqual(updateCount, 1)
@ -57,9 +57,9 @@ describe('Batcher', function () {
updateCount = 0 updateCount = 0
var b1 = mockBinding(1), var b1 = mockBinding(1),
b2 = mockBinding(2, function () { b2 = mockBinding(2, function () {
batcher.queue(b1, 'update') batcher.queue(b1)
}) })
batcher.queue(b2, 'update') batcher.queue(b2)
nextTick(function () { nextTick(function () {
assert.strictEqual(updateCount, 2) assert.strictEqual(updateCount, 2)

View File

@ -67,32 +67,45 @@ describe('UNIT: Binding', function () {
assert.ok(pubbed) assert.ok(pubbed)
}) })
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)
}) })
describe('.refresh()', function () { })
var b = new Binding(null, 'test'), describe('.val()', function () {
refreshed = 0,
numInstances = 3, it('should return the raw value for non-computed and function bindings', function () {
instance = { var b1 = new Binding(null, 'test'),
refresh: function () { b2 = new Binding(null, 'test', false, true)
refreshed++ b2.isComputed = true
b1.value = 1
b2.value = 2
assert.strictEqual(b1.val(), 1)
assert.strictEqual(b2.val(), 2)
})
it('should return computed value for computed bindings', function () {
var b = new Binding(null, 'test')
b.isComputed = true
b.value = {
$get: function () {
return 3
} }
} }
for (var i = 0; i < numInstances; i++) { assert.strictEqual(b.val(), 3)
b.instances.push(instance)
}
before(function (done) {
b.refresh()
nextTick(function () {
done()
})
}) })
it('should call refresh() of all instances', function () {
assert.strictEqual(refreshed, numInstances)
})
}) })
describe('.pub()', function () { describe('.pub()', function () {
@ -101,7 +114,7 @@ describe('UNIT: Binding', function () {
refreshed = 0, refreshed = 0,
numSubs = 3, numSubs = 3,
sub = { sub = {
refresh: function () { update: function () {
refreshed++ refreshed++
} }
} }
@ -110,7 +123,7 @@ describe('UNIT: Binding', function () {
} }
b.pub() b.pub()
it('should call refresh() of all subscribers', function () { it('should call update() of all subscribers', function () {
assert.strictEqual(refreshed, numSubs) assert.strictEqual(refreshed, numSubs)
}) })

View File

@ -236,88 +236,40 @@ describe('UNIT: Directive', function () {
}) })
describe('.apply()', function () {
var test,
applyTest = function (val) { test = val }
directives.applyTest = applyTest
it('should invole the _update function', function () {
var d = Directive.parse('applyTest', 'abc', compiler)
d.apply(12345)
assert.strictEqual(test, 12345)
})
it('should apply the filter if there is any', function () {
var d = Directive.parse('applyTest', 'abc | currency £', compiler)
d.apply(12345)
assert.strictEqual(test, '£12,345.00')
})
})
describe('.update()', function () { describe('.update()', function () {
var d = Directive.parse('text', 'abc', compiler), var d = Directive.parse('text', 'abc', compiler),
applied = false updated = false
d.apply = function () { d._update = function () {
applied = true updated = true
} }
it('should apply() for first time update, even with undefined', function () { it('should call _update() for first time update, even with undefined', function () {
d.update(undefined, true) d.update(undefined, true)
assert.strictEqual(applied, true) assert.strictEqual(updated, true)
}) })
it('should apply() when a different value is given', function () { it('should _update() when a different value is given', function () {
applied = false updated = false
d.update(123) d.update(123)
assert.strictEqual(d.value, 123) assert.strictEqual(d.value, 123)
assert.strictEqual(applied, true) assert.strictEqual(updated, true)
}) })
it('should not apply() if the value is the same', function () { it('should not _update() if the value is the same', function () {
applied = false updated = false
d.update(123) d.update(123)
assert.ok(!applied) assert.ok(!updated)
}) })
}) it('should call applyFilter() is there are filters', function () {
var filterApplied = false
describe('.refresh()', function () { d.filters = []
d.applyFilters = function () {
var d = Directive.parse('text', 'abc', compiler), filterApplied = true
applied = false,
el = 1, vm = 2,
value = {
$get: function () {
return el + vm
} }
} d.update(234)
d.el = el assert.ok(filterApplied)
d.vm = vm
d.apply = function () {
applied = true
}
d.refresh(value)
it('should set the value if value arg is given', function () {
assert.strictEqual(d.value, value)
})
it('should get its el&vm context and get correct computedValue', function () {
assert.strictEqual(d.computedValue, el + vm)
})
it('should call apply()', function () {
assert.ok(applied)
})
it('should not call apply() if computedValue is the same', function () {
applied = false
d.refresh()
assert.ok(!applied)
}) })
}) })

View File

@ -42,6 +42,8 @@ describe('Misc Features', function () {
var v = new Vue({ var v = new Vue({
data: { data: {
a: 1, a: 1,
},
computed: {
test: { test: {
$get: function () { $get: function () {
return this.a + b return this.a + b