Skip to content

Commit 3924044

Browse files
committed
unit tests pass for computed property rewrite
1 parent 7a6169c commit 3924044

File tree

6 files changed

+63
-97
lines changed

6 files changed

+63
-97
lines changed

src/binding.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var batcher = require('./batcher'),
2-
utils = require('./utils'),
32
id = 0
43

54
/**
@@ -40,7 +39,7 @@ BindingProto.update = function (value) {
4039
*/
4140
BindingProto._update = function () {
4241
var i = this.instances.length,
43-
value = this.eval()
42+
value = this.val()
4443
while (i--) {
4544
this.instances[i].update(value)
4645
}
@@ -51,7 +50,7 @@ BindingProto._update = function () {
5150
* Return the valuated value regardless
5251
* of whether it is computed or not
5352
*/
54-
BindingProto.eval = function () {
53+
BindingProto.val = function () {
5554
return this.isComputed && !this.isFn
5655
? this.value.$get()
5756
: this.value

src/compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ CompilerProto.bindDirective = function (directive) {
433433
}
434434

435435
// set initial value
436-
directive.update(binding.eval(), true)
436+
directive.update(binding.val(), true)
437437
}
438438

439439
/**

test/unit/specs/batcher.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe('Batcher', function () {
2020
updateCount = 0
2121
var b1 = mockBinding(1),
2222
b2 = mockBinding(2)
23-
batcher.queue(b1, 'update')
24-
batcher.queue(b2, 'update')
23+
batcher.queue(b1)
24+
batcher.queue(b2)
2525
assert.strictEqual(updateCount, 0)
2626
assert.notOk(b1.updated)
2727
assert.notOk(b2.updated)
@@ -40,8 +40,8 @@ describe('Batcher', function () {
4040
updateCount = 0
4141
var b1 = mockBinding(1),
4242
b2 = mockBinding(1)
43-
batcher.queue(b1, 'update')
44-
batcher.queue(b2, 'update')
43+
batcher.queue(b1)
44+
batcher.queue(b2)
4545

4646
nextTick(function () {
4747
assert.strictEqual(updateCount, 1)
@@ -57,9 +57,9 @@ describe('Batcher', function () {
5757
updateCount = 0
5858
var b1 = mockBinding(1),
5959
b2 = mockBinding(2, function () {
60-
batcher.queue(b1, 'update')
60+
batcher.queue(b1)
6161
})
62-
batcher.queue(b2, 'update')
62+
batcher.queue(b2)
6363

6464
nextTick(function () {
6565
assert.strictEqual(updateCount, 2)

test/unit/specs/binding.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,45 @@ describe('UNIT: Binding', function () {
6767
assert.ok(pubbed)
6868
})
6969

70+
it('should not set the value if it is computed unless a function', function () {
71+
var b1 = new Binding(null, 'test'),
72+
b2 = new Binding(null, 'test', false, true)
73+
b1.isComputed = true
74+
b2.isComputed = true
75+
var ov = { $get: function () {} }
76+
b1.value = ov
77+
b2.value = function () {}
78+
b1.update(1)
79+
b2.update(1)
80+
assert.strictEqual(b1.value, ov)
81+
assert.strictEqual(b2.value, 1)
82+
})
83+
7084
})
7185

72-
describe('.refresh()', function () {
86+
describe('.val()', function () {
87+
88+
it('should return the raw value for non-computed and function bindings', function () {
89+
var b1 = new Binding(null, 'test'),
90+
b2 = new Binding(null, 'test', false, true)
91+
b2.isComputed = true
92+
b1.value = 1
93+
b2.value = 2
94+
assert.strictEqual(b1.val(), 1)
95+
assert.strictEqual(b2.val(), 2)
96+
})
7397

74-
var b = new Binding(null, 'test'),
75-
refreshed = 0,
76-
numInstances = 3,
77-
instance = {
78-
refresh: function () {
79-
refreshed++
98+
it('should return computed value for computed bindings', function () {
99+
var b = new Binding(null, 'test')
100+
b.isComputed = true
101+
b.value = {
102+
$get: function () {
103+
return 3
80104
}
81105
}
82-
for (var i = 0; i < numInstances; i++) {
83-
b.instances.push(instance)
84-
}
85-
86-
before(function (done) {
87-
b.refresh()
88-
nextTick(function () {
89-
done()
90-
})
106+
assert.strictEqual(b.val(), 3)
91107
})
92108

93-
it('should call refresh() of all instances', function () {
94-
assert.strictEqual(refreshed, numInstances)
95-
})
96109
})
97110

98111
describe('.pub()', function () {
@@ -101,7 +114,7 @@ describe('UNIT: Binding', function () {
101114
refreshed = 0,
102115
numSubs = 3,
103116
sub = {
104-
refresh: function () {
117+
update: function () {
105118
refreshed++
106119
}
107120
}
@@ -110,7 +123,7 @@ describe('UNIT: Binding', function () {
110123
}
111124
b.pub()
112125

113-
it('should call refresh() of all subscribers', function () {
126+
it('should call update() of all subscribers', function () {
114127
assert.strictEqual(refreshed, numSubs)
115128
})
116129

test/unit/specs/directive.js

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -236,88 +236,40 @@ describe('UNIT: Directive', function () {
236236

237237
})
238238

239-
describe('.apply()', function () {
240-
241-
var test,
242-
applyTest = function (val) { test = val }
243-
directives.applyTest = applyTest
244-
245-
it('should invole the _update function', function () {
246-
var d = Directive.parse('applyTest', 'abc', compiler)
247-
d.apply(12345)
248-
assert.strictEqual(test, 12345)
249-
})
250-
251-
it('should apply the filter if there is any', function () {
252-
var d = Directive.parse('applyTest', 'abc | currency £', compiler)
253-
d.apply(12345)
254-
assert.strictEqual(test, '£12,345.00')
255-
})
256-
257-
})
258-
259239
describe('.update()', function () {
260240

261241
var d = Directive.parse('text', 'abc', compiler),
262-
applied = false
263-
d.apply = function () {
264-
applied = true
242+
updated = false
243+
d._update = function () {
244+
updated = true
265245
}
266246

267-
it('should apply() for first time update, even with undefined', function () {
247+
it('should call _update() for first time update, even with undefined', function () {
268248
d.update(undefined, true)
269-
assert.strictEqual(applied, true)
249+
assert.strictEqual(updated, true)
270250
})
271251

272-
it('should apply() when a different value is given', function () {
273-
applied = false
252+
it('should _update() when a different value is given', function () {
253+
updated = false
274254
d.update(123)
275255
assert.strictEqual(d.value, 123)
276-
assert.strictEqual(applied, true)
256+
assert.strictEqual(updated, true)
277257
})
278258

279-
it('should not apply() if the value is the same', function () {
280-
applied = false
259+
it('should not _update() if the value is the same', function () {
260+
updated = false
281261
d.update(123)
282-
assert.ok(!applied)
262+
assert.ok(!updated)
283263
})
284264

285-
})
286-
287-
describe('.refresh()', function () {
288-
289-
var d = Directive.parse('text', 'abc', compiler),
290-
applied = false,
291-
el = 1, vm = 2,
292-
value = {
293-
$get: function () {
294-
return el + vm
295-
}
265+
it('should call applyFilter() is there are filters', function () {
266+
var filterApplied = false
267+
d.filters = []
268+
d.applyFilters = function () {
269+
filterApplied = true
296270
}
297-
d.el = el
298-
d.vm = vm
299-
d.apply = function () {
300-
applied = true
301-
}
302-
303-
d.refresh(value)
304-
305-
it('should set the value if value arg is given', function () {
306-
assert.strictEqual(d.value, value)
307-
})
308-
309-
it('should get its el&vm context and get correct computedValue', function () {
310-
assert.strictEqual(d.computedValue, el + vm)
311-
})
312-
313-
it('should call apply()', function () {
314-
assert.ok(applied)
315-
})
316-
317-
it('should not call apply() if computedValue is the same', function () {
318-
applied = false
319-
d.refresh()
320-
assert.ok(!applied)
271+
d.update(234)
272+
assert.ok(filterApplied)
321273
})
322274

323275
})

test/unit/specs/misc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ describe('Misc Features', function () {
4242
var v = new Vue({
4343
data: {
4444
a: 1,
45+
},
46+
computed: {
4547
test: {
4648
$get: function () {
4749
return this.a + b

0 commit comments

Comments
 (0)