Преглед изворни кода

unit tests pass for computed property rewrite

Evan You пре 12 година
родитељ
комит
3924044338
6 измењених фајлова са 63 додато и 97 уклоњено
  1. 2 3
      src/binding.js
  2. 1 1
      src/compiler.js
  3. 6 6
      test/unit/specs/batcher.js
  4. 34 21
      test/unit/specs/binding.js
  5. 18 66
      test/unit/specs/directive.js
  6. 2 0
      test/unit/specs/misc.js

+ 2 - 3
src/binding.js

@@ -1,5 +1,4 @@
 var batcher = require('./batcher'),
-    utils = require('./utils'),
     id = 0
 
 /**
@@ -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)
     }
@@ -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

+ 1 - 1
src/compiler.js

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

+ 6 - 6
test/unit/specs/batcher.js

@@ -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)
@@ -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)
@@ -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)

+ 34 - 21
test/unit/specs/binding.js

@@ -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 () {
@@ -101,7 +114,7 @@ describe('UNIT: Binding', function () {
             refreshed = 0,
             numSubs = 3,
             sub = {
-                refresh: function () {
+                update: function () {
                     refreshed++
                 }
             }
@@ -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)
         })
 

+ 18 - 66
test/unit/specs/directive.js

@@ -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)
         })
 
     })

+ 2 - 0
test/unit/specs/misc.js

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