Skip to content

Commit 6cbb9fa

Browse files
author
Evan You
committed
improve code coverage in unit tests
1 parent 7720da1 commit 6cbb9fa

File tree

8 files changed

+245
-12
lines changed

8 files changed

+245
-12
lines changed

src/deps-parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ var Emitter = require('./emitter'),
88
*/
99
function catchDeps (binding) {
1010
if (binding.isFn) return
11-
utils.log('\n ' + binding.key)
11+
utils.log('\n- ' + binding.key)
1212
var got = utils.hash()
1313
observer.on('get', function (dep) {
1414
var has = got[dep.key]
1515
if (has && has.compiler === dep.compiler) return
1616
got[dep.key] = dep
17-
utils.log(' └─ ' + dep.key)
17+
utils.log(' - ' + dep.key)
1818
binding.deps.push(dep)
1919
dep.subs.push(binding)
2020
})

src/directives/repeat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module.exports = {
9898
self.hasTrans = el.hasAttribute(config.attrs.transition)
9999

100100
// create a comment node as a reference node for DOM insertions
101-
self.ref = document.createComment(config.prefix + '-repeat-' + self.arg)
101+
self.ref = document.createComment(config.prefix + '-repeat-' + self.key)
102102
ctn.insertBefore(self.ref, el)
103103
ctn.removeChild(el)
104104

test/unit/runner.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<script src="specs/viewmodel.js"></script>
4949
<script src="specs/transition.js"></script>
5050
<script src="specs/batcher.js"></script>
51+
<script src="specs/misc.js"></script>
5152
<script>
5253
if (navigator.userAgent.indexOf('PhantomJS') < 0) {
5354
mocha.run(Cover.report)

test/unit/specs/api.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,28 @@ describe('UNIT: API', function () {
740740

741741
})
742742

743+
describe('Hook inheritance', function () {
744+
745+
it('should merge hooks with parent Class', function () {
746+
var parentCreated = false,
747+
childCreated = false
748+
var Parent = Vue.extend({
749+
created: function () {
750+
parentCreated = true
751+
}
752+
})
753+
var Child = Parent.extend({
754+
created: function () {
755+
childCreated = true
756+
}
757+
})
758+
new Child()
759+
assert.ok(parentCreated)
760+
assert.ok(childCreated)
761+
})
762+
763+
})
764+
743765
})
744766

745767
})

test/unit/specs/directives.js

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
/*
2-
* Only tests directives in `src/directives/index.js`
3-
* and the non-delegated case for `v-on`
4-
*
5-
* The combination of `v-repeat` and `v-on` are covered in
6-
* the E2E test case for repeated items.
7-
*/
8-
91
describe('UNIT: Directives', function () {
102

113
describe('attr', function () {
@@ -637,6 +629,62 @@ describe('UNIT: Directives', function () {
637629

638630
})
639631

632+
// More detailed testing for v-repeat can be found in functional tests.
633+
// this is mainly for code coverage
634+
describe('repeat', function () {
635+
636+
var nextTick = require('vue/src/utils').nextTick,
637+
VM = require('vue/src/viewmodel')
638+
639+
it('should work', function (done) {
640+
var handlerCalled = false
641+
var v = new Vue({
642+
template: '<span v-repeat="items" v-on="click:check">{{title}}</span>',
643+
data: {
644+
items: [
645+
{title: 1},
646+
{title: 2}
647+
]
648+
},
649+
methods: {
650+
check: function (e) {
651+
assert.ok(e.targetVM instanceof VM)
652+
assert.strictEqual(this, v)
653+
handlerCalled = true
654+
}
655+
}
656+
})
657+
nextTick(function () {
658+
assert.equal(v.$el.innerHTML, '<span>1</span><span>2</span><!--v-repeat-items-->')
659+
v.items.push({title:3})
660+
v.items.pop()
661+
v.items.unshift({title:0})
662+
v.items.shift()
663+
v.items.splice(0, 1, {title:-1})
664+
v.items.sort(function (a, b) {
665+
return a.title > b.title
666+
})
667+
v.items.reverse()
668+
nextTick(function () {
669+
assert.equal(v.$el.innerHTML, '<span>2</span><span>-1</span><!--v-repeat-items-->')
670+
testHandler()
671+
})
672+
})
673+
674+
function testHandler () {
675+
document.getElementById('test').appendChild(v.$el)
676+
var span = v.$el.querySelector('span'),
677+
e = mockMouseEvent('click')
678+
span.dispatchEvent(e)
679+
nextTick(function () {
680+
assert.ok(handlerCalled)
681+
done()
682+
})
683+
}
684+
})
685+
686+
})
687+
640688
})
641689

642690
function mockDirective (dirName, tag, type) {

test/unit/specs/misc.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
describe('Misc Features', function () {
2+
3+
var nextTick = require('vue/src/utils').nextTick
4+
5+
describe('inline expression', function () {
6+
it('should evaluate the correct value', function (done) {
7+
var v = new Vue({
8+
template: '{{a + "123" + b}} and {{c}}'
9+
})
10+
v.a = 'A'
11+
v.b = 'B'
12+
v.c = 'C'
13+
nextTick(function () {
14+
assert.strictEqual(v.$el.textContent, 'A123B and C')
15+
done()
16+
})
17+
})
18+
})
19+
20+
describe('computed properties', function () {
21+
it('should be accessible like a normal attribtue', function () {
22+
var b = 2
23+
var v = new Vue({
24+
data: {
25+
a: 1,
26+
test: {
27+
$get: function () {
28+
return this.a + b
29+
},
30+
$set: function (v) {
31+
b = v - this.a
32+
}
33+
}
34+
}
35+
})
36+
37+
assert.strictEqual(v.test, 3)
38+
v.a = 2
39+
assert.strictEqual(v.test, 4)
40+
b = 3
41+
assert.strictEqual(v.test, 5)
42+
v.test = 10
43+
assert.strictEqual(b, 8)
44+
})
45+
})
46+
47+
describe('setting an object to empty', function () {
48+
it('should emit undefined for paths in the old object', function () {
49+
var v = new Vue({
50+
data: {
51+
a: {
52+
b: { c: 1 }
53+
}
54+
}
55+
})
56+
var emitted = false
57+
v.$watch('a.b.c', function (v) {
58+
assert.strictEqual(v, undefined)
59+
emitted = true
60+
})
61+
v.a = {}
62+
assert.ok(emitted)
63+
})
64+
})
65+
66+
})

test/unit/specs/utils.js

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
describe('UNIT: Utils', function () {
22

3-
var utils = require('vue/src/utils')
3+
var utils = require('vue/src/utils'),
4+
config = require('vue/src/config')
5+
6+
try {
7+
require('non-existent')
8+
} catch (e) {
9+
console.log('testing require fail')
10+
}
411

512
describe('hash', function () {
613

@@ -241,4 +248,70 @@ describe('UNIT: Utils', function () {
241248

242249
})
243250

251+
describe('log', function () {
252+
253+
it('should only log in debug mode', function () {
254+
// overwrite log temporarily
255+
var oldLog = console.log,
256+
logged
257+
console.log = function (msg) {
258+
logged = msg
259+
}
260+
261+
utils.log('123')
262+
assert.notOk(logged)
263+
264+
config.debug = true
265+
utils.log('123')
266+
assert.strictEqual(logged, '123')
267+
268+
// teardown
269+
config.debug = false
270+
console.log = oldLog
271+
})
272+
273+
})
274+
275+
describe('warn', function () {
276+
277+
it('should only warn when not in silent mode', function () {
278+
config.silent = true
279+
var oldWarn = console.warn,
280+
warned
281+
console.warn = function (msg) {
282+
warned = msg
283+
}
284+
285+
utils.warn('123')
286+
assert.notOk(warned)
287+
288+
config.silent = false
289+
utils.warn('123')
290+
assert.strictEqual(warned, '123')
291+
292+
console.warn = oldWarn
293+
})
294+
295+
it('should also trace in debug mode', function () {
296+
config.silent = false
297+
config.debug = true
298+
var oldTrace = console.trace,
299+
oldWarn = console.warn,
300+
traced
301+
console.warn = function () {}
302+
console.trace = function () {
303+
traced = true
304+
}
305+
306+
utils.warn('testing trace')
307+
assert.ok(traced)
308+
309+
config.silent = true
310+
config.debug = false
311+
console.trace = oldTrace
312+
console.warn = oldWarn
313+
})
314+
315+
})
316+
244317
})

test/unit/specs/viewmodel.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,4 +469,27 @@ describe('UNIT: ViewModel', function () {
469469

470470
})
471471

472+
describe('$data', function () {
473+
474+
it('should be the same data', function () {
475+
var data = {},
476+
vm = new Vue({data:data})
477+
assert.strictEqual(vm.$data, data)
478+
})
479+
480+
it('should be able to be swapped', function () {
481+
var data1 = { a: 1 },
482+
data2 = { a: 2 },
483+
vm = new Vue({data: data1}),
484+
emittedChange = false
485+
vm.$watch('a', function (v) {
486+
assert.equal(v, 2)
487+
emittedChange = true
488+
})
489+
vm.$data = data2
490+
assert.equal(vm.a, 2)
491+
assert.ok(emittedChange)
492+
})
493+
})
494+
472495
})

0 commit comments

Comments
 (0)