Procházet zdrojové kódy

improve code coverage in unit tests

Evan You před 12 roky
rodič
revize
6cbb9fa49b

+ 2 - 2
src/deps-parser.js

@@ -8,13 +8,13 @@ var Emitter  = require('./emitter'),
  */
 function catchDeps (binding) {
     if (binding.isFn) return
-    utils.log('\n ' + binding.key)
+    utils.log('\n- ' + binding.key)
     var got = utils.hash()
     observer.on('get', function (dep) {
         var has = got[dep.key]
         if (has && has.compiler === dep.compiler) return
         got[dep.key] = dep
-        utils.log('  └─ ' + dep.key)
+        utils.log('  - ' + dep.key)
         binding.deps.push(dep)
         dep.subs.push(binding)
     })

+ 1 - 1
src/directives/repeat.js

@@ -98,7 +98,7 @@ module.exports = {
         self.hasTrans   = el.hasAttribute(config.attrs.transition)
 
         // create a comment node as a reference node for DOM insertions
-        self.ref = document.createComment(config.prefix + '-repeat-' + self.arg)
+        self.ref = document.createComment(config.prefix + '-repeat-' + self.key)
         ctn.insertBefore(self.ref, el)
         ctn.removeChild(el)
 

+ 1 - 0
test/unit/runner.html

@@ -48,6 +48,7 @@
 		<script src="specs/viewmodel.js"></script>
         <script src="specs/transition.js"></script>
         <script src="specs/batcher.js"></script>
+        <script src="specs/misc.js"></script>
 		<script>
 			if (navigator.userAgent.indexOf('PhantomJS') < 0) {
             	mocha.run(Cover.report)

+ 22 - 0
test/unit/specs/api.js

@@ -740,6 +740,28 @@ describe('UNIT: API', function () {
 
                 })
 
+                describe('Hook inheritance', function () {
+                    
+                    it('should merge hooks with parent Class', function () {
+                        var parentCreated = false,
+                            childCreated = false
+                        var Parent = Vue.extend({
+                            created: function () {
+                                parentCreated = true
+                            }
+                        })
+                        var Child = Parent.extend({
+                            created: function () {
+                                childCreated = true
+                            }
+                        })
+                        new Child()
+                        assert.ok(parentCreated)
+                        assert.ok(childCreated)
+                    })
+
+                })
+
             })
 
         })

+ 56 - 8
test/unit/specs/directives.js

@@ -1,11 +1,3 @@
-/*
- *  Only tests directives in `src/directives/index.js`
- *  and the non-delegated case for `v-on`
- *
- *  The combination of `v-repeat` and `v-on` are covered in
- *  the E2E test case for repeated items.
- */
-
 describe('UNIT: Directives', function () {
     
     describe('attr', function () {
@@ -637,6 +629,62 @@ describe('UNIT: Directives', function () {
 
     })
 
+    // More detailed testing for v-repeat can be found in functional tests.
+    // this is mainly for code coverage
+    describe('repeat', function () {
+
+        var nextTick = require('vue/src/utils').nextTick,
+            VM = require('vue/src/viewmodel')
+
+        it('should work', function (done) {
+            var handlerCalled = false
+            var v = new Vue({
+                template: '<span v-repeat="items" v-on="click:check">{{title}}</span>',
+                data: {
+                    items: [
+                        {title: 1},
+                        {title: 2}
+                    ]
+                },
+                methods: {
+                    check: function (e) {
+                        assert.ok(e.targetVM instanceof VM)
+                        assert.strictEqual(this, v)
+                        handlerCalled = true
+                    }
+                }
+            })
+            nextTick(function () {
+                assert.equal(v.$el.innerHTML, '<span>1</span><span>2</span><!--v-repeat-items-->')
+                v.items.push({title:3})
+                v.items.pop()
+                v.items.unshift({title:0})
+                v.items.shift()
+                v.items.splice(0, 1, {title:-1})
+                v.items.sort(function (a, b) {
+                    return a.title > b.title
+                })
+                v.items.reverse()
+                nextTick(function () {
+                    assert.equal(v.$el.innerHTML, '<span>2</span><span>-1</span><!--v-repeat-items-->')
+                    testHandler()
+                })
+            })
+
+            function testHandler () {
+                document.getElementById('test').appendChild(v.$el)
+                var span = v.$el.querySelector('span'),
+                    e = mockMouseEvent('click')
+                span.dispatchEvent(e)
+                nextTick(function () {
+                    assert.ok(handlerCalled)
+                    done()
+                })
+            }
+        })
+
+    })
+
 })
 
 function mockDirective (dirName, tag, type) {

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

@@ -0,0 +1,66 @@
+describe('Misc Features', function () {
+
+    var nextTick = require('vue/src/utils').nextTick
+    
+    describe('inline expression', function () {
+        it('should evaluate the correct value', function (done) {
+            var v = new Vue({
+                template: '{{a + "123" + b}} and {{c}}'
+            })
+            v.a = 'A'
+            v.b = 'B'
+            v.c = 'C'
+            nextTick(function () {
+                assert.strictEqual(v.$el.textContent, 'A123B and C')
+                done()
+            })
+        })
+    })
+
+    describe('computed properties', function () {
+        it('should be accessible like a normal attribtue', function () {
+            var b = 2
+            var v = new Vue({
+                data: {
+                    a: 1,
+                    test: {
+                        $get: function () {
+                            return this.a + b
+                        },
+                        $set: function (v) {
+                            b = v - this.a
+                        }
+                    }
+                }
+            })
+
+            assert.strictEqual(v.test, 3)
+            v.a = 2
+            assert.strictEqual(v.test, 4)
+            b = 3
+            assert.strictEqual(v.test, 5)
+            v.test = 10
+            assert.strictEqual(b, 8)
+        })
+    })
+
+    describe('setting an object to empty', function () {
+        it('should emit undefined for paths in the old object', function () {
+            var v = new Vue({
+                data: {
+                    a: {
+                        b: { c: 1 }
+                    }
+                }
+            })
+            var emitted = false
+            v.$watch('a.b.c', function (v) {
+                assert.strictEqual(v, undefined)
+                emitted = true
+            })
+            v.a = {}
+            assert.ok(emitted)
+        })
+    })
+
+})

+ 74 - 1
test/unit/specs/utils.js

@@ -1,6 +1,13 @@
 describe('UNIT: Utils', function () {
 
-    var utils = require('vue/src/utils')
+    var utils = require('vue/src/utils'),
+        config = require('vue/src/config')
+
+    try {
+        require('non-existent')
+    } catch (e) {
+        console.log('testing require fail')
+    }
     
     describe('hash', function () {
 
@@ -241,4 +248,70 @@ describe('UNIT: Utils', function () {
 
     })
 
+    describe('log', function () {
+        
+        it('should only log in debug mode', function () {
+            // overwrite log temporarily
+            var oldLog = console.log,
+                logged
+            console.log = function (msg) {
+                logged = msg
+            }
+
+            utils.log('123')
+            assert.notOk(logged)
+
+            config.debug = true
+            utils.log('123')
+            assert.strictEqual(logged, '123')
+
+            // teardown
+            config.debug = false
+            console.log = oldLog
+        })
+
+    })
+
+    describe('warn', function () {
+        
+        it('should only warn when not in silent mode', function () {
+            config.silent = true
+            var oldWarn = console.warn,
+                warned
+            console.warn = function (msg) {
+                warned = msg
+            }
+
+            utils.warn('123')
+            assert.notOk(warned)
+
+            config.silent = false
+            utils.warn('123')
+            assert.strictEqual(warned, '123')
+
+            console.warn = oldWarn
+        })
+
+        it('should also trace in debug mode', function () {
+            config.silent = false
+            config.debug = true
+            var oldTrace = console.trace,
+                oldWarn = console.warn,
+                traced
+            console.warn = function () {}
+            console.trace = function () {
+                traced = true
+            }
+
+            utils.warn('testing trace')
+            assert.ok(traced)
+
+            config.silent = true
+            config.debug = false
+            console.trace = oldTrace
+            console.warn = oldWarn
+        })
+
+    })
+
 })

+ 23 - 0
test/unit/specs/viewmodel.js

@@ -469,4 +469,27 @@ describe('UNIT: ViewModel', function () {
 
     })
 
+    describe('$data', function () {
+
+        it('should be the same data', function () {
+            var data = {},
+                vm = new Vue({data:data})
+            assert.strictEqual(vm.$data, data)
+        })
+
+        it('should be able to be swapped', function () {
+            var data1 = { a: 1 },
+                data2 = { a: 2 },
+                vm = new Vue({data: data1}),
+                emittedChange = false
+            vm.$watch('a', function (v) {
+                assert.equal(v, 2)
+                emittedChange = true
+            })
+            vm.$data = data2
+            assert.equal(vm.a, 2)
+            assert.ok(emittedChange)
+        })
+    })
+
 })