Skip to content

Commit

Permalink
unit tests pass for computed property rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 27, 2014
1 parent 7a6169c commit 3924044
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 97 deletions.
5 changes: 2 additions & 3 deletions src/binding.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var batcher = require('./batcher'),
utils = require('./utils'),
id = 0

/**
Expand Down Expand Up @@ -40,7 +39,7 @@ BindingProto.update = function (value) {
*/
BindingProto._update = function () {
var i = this.instances.length,
value = this.eval()
value = this.val()
while (i--) {
this.instances[i].update(value)
}
Expand All @@ -51,7 +50,7 @@ BindingProto._update = function () {
* Return the valuated value regardless
* of whether it is computed or not
*/
BindingProto.eval = function () {
BindingProto.val = function () {
return this.isComputed && !this.isFn
? this.value.$get()
: this.value
Expand Down
2 changes: 1 addition & 1 deletion src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ CompilerProto.bindDirective = function (directive) {
}

// set initial value
directive.update(binding.eval(), true)
directive.update(binding.val(), true)
}

/**
Expand Down
12 changes: 6 additions & 6 deletions test/unit/specs/batcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('Batcher', function () {
updateCount = 0
var b1 = mockBinding(1),
b2 = mockBinding(2)
batcher.queue(b1, 'update')
batcher.queue(b2, 'update')
batcher.queue(b1)
batcher.queue(b2)
assert.strictEqual(updateCount, 0)
assert.notOk(b1.updated)
assert.notOk(b2.updated)
Expand All @@ -40,8 +40,8 @@ describe('Batcher', function () {
updateCount = 0
var b1 = mockBinding(1),
b2 = mockBinding(1)
batcher.queue(b1, 'update')
batcher.queue(b2, 'update')
batcher.queue(b1)
batcher.queue(b2)

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

nextTick(function () {
assert.strictEqual(updateCount, 2)
Expand Down
55 changes: 34 additions & 21 deletions test/unit/specs/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,45 @@ describe('UNIT: Binding', function () {
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 () {
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)
})

var b = new Binding(null, 'test'),
refreshed = 0,
numInstances = 3,
instance = {
refresh: function () {
refreshed++
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++) {
b.instances.push(instance)
}

before(function (done) {
b.refresh()
nextTick(function () {
done()
})
assert.strictEqual(b.val(), 3)
})

it('should call refresh() of all instances', function () {
assert.strictEqual(refreshed, numInstances)
})
})

describe('.pub()', function () {
Expand All @@ -101,7 +114,7 @@ describe('UNIT: Binding', function () {
refreshed = 0,
numSubs = 3,
sub = {
refresh: function () {
update: function () {
refreshed++
}
}
Expand All @@ -110,7 +123,7 @@ describe('UNIT: Binding', function () {
}
b.pub()

it('should call refresh() of all subscribers', function () {
it('should call update() of all subscribers', function () {
assert.strictEqual(refreshed, numSubs)
})

Expand Down
84 changes: 18 additions & 66 deletions test/unit/specs/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {

var d = Directive.parse('text', 'abc', compiler),
applied = false
d.apply = function () {
applied = true
updated = false
d._update = function () {
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)
assert.strictEqual(applied, true)
assert.strictEqual(updated, true)
})

it('should apply() when a different value is given', function () {
applied = false
it('should _update() when a different value is given', function () {
updated = false
d.update(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 () {
applied = false
it('should not _update() if the value is the same', function () {
updated = false
d.update(123)
assert.ok(!applied)
assert.ok(!updated)
})

})

describe('.refresh()', function () {

var d = Directive.parse('text', 'abc', compiler),
applied = false,
el = 1, vm = 2,
value = {
$get: function () {
return el + vm
}
it('should call applyFilter() is there are filters', function () {
var filterApplied = false
d.filters = []
d.applyFilters = function () {
filterApplied = true
}
d.el = el
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)
d.update(234)
assert.ok(filterApplied)
})

})
Expand Down
2 changes: 2 additions & 0 deletions test/unit/specs/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ describe('Misc Features', function () {
var v = new Vue({
data: {
a: 1,
},
computed: {
test: {
$get: function () {
return this.a + b
Expand Down

0 comments on commit 3924044

Please sign in to comment.