Skip to content

Commit 2a78b90

Browse files
committed
tests for new partial
1 parent 6b7f9d9 commit 2a78b90

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed

src/compiler/transclude.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,18 @@ function transcludeTemplate (el, options) {
6363
_.warn('Invalid template option: ' + template)
6464
} else {
6565
var replacer = frag.firstChild
66+
var tag = replacer.tagName && replacer.tagName.toLowerCase()
6667
if (options.replace) {
6768
if (
69+
// multi-children template
6870
frag.childNodes.length > 1 ||
71+
// non-element template
6972
replacer.nodeType !== 1 ||
70-
// when root node is <content> or has v-repeat, the
71-
// instance could end up having multiple top-level
72-
// nodes, thus becoming a block instance.
73-
replacer.tagName.toLowerCase() === 'content' ||
73+
// when root node is <content>, <partial> or has
74+
// v-repeat, the instance could end up having
75+
// multiple top-level nodes, thus becoming a block
76+
// instance.
77+
tag === 'content' || tag === 'partial' ||
7478
replacer.hasAttribute(config.prefix + 'repeat')
7579
) {
7680
return frag

src/directives/repeat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ module.exports = {
358358
// is this a component?
359359
_asComponent: this.asComponent,
360360
// linker cachable if no inline-template
361-
_linkerCachable: !this.inlineTemplate,
361+
_linkerCachable: !this.inlineTemplate && Ctor !== _.Vue,
362362
// transclusion host
363363
_host: this._host,
364364
// pre-compiled linker for simple repeats
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
var Vue = require('../../../../src/vue')
2+
var _ = require('../../../../src/util')
3+
var compiler = require('../../../../src/compiler')
4+
5+
describe('Partial', function () {
6+
7+
var el
8+
beforeEach(function () {
9+
el = document.createElement('div')
10+
})
11+
12+
it('static', function (done) {
13+
var vm = new Vue({
14+
el: el,
15+
template: '<partial name="yo"></partial>',
16+
data: {
17+
msg: 'hello'
18+
},
19+
partials: {
20+
yo: '{{msg}}'
21+
}
22+
})
23+
expect(el.textContent).toBe('hello')
24+
vm.msg = 'what'
25+
_.nextTick(function () {
26+
expect(el.textContent).toBe('what')
27+
done()
28+
})
29+
})
30+
31+
it('dynamic', function (done) {
32+
var vm = new Vue({
33+
el: el,
34+
template: '<partial name="test-{{id}}"></partial>',
35+
data: {
36+
id: 'a',
37+
},
38+
partials: {
39+
'test-a': 'a {{id}}',
40+
'test-b': 'b {{id}}'
41+
}
42+
})
43+
expect(el.textContent).toBe('a a')
44+
vm.id = 'b'
45+
_.nextTick(function () {
46+
expect(el.textContent).toBe('b b')
47+
done()
48+
})
49+
})
50+
51+
it('caching', function () {
52+
var calls = 0
53+
var compile = compiler.compile
54+
compiler.compile = function () {
55+
calls++
56+
return compile.apply(this, arguments)
57+
}
58+
var vm = new Vue({
59+
el: el,
60+
template:
61+
'<partial name="cache-test"></partial> ' +
62+
'<partial name="cache-test"></partial>',
63+
data: {
64+
msg: 'hi'
65+
},
66+
partials: {
67+
'cache-test': 'cache-test {{msg}}'
68+
}
69+
})
70+
expect(el.textContent).toBe('cache-test hi cache-test hi')
71+
// one call for instance, and one for partial
72+
expect(calls).toBe(2)
73+
// cleanup
74+
compiler.compile = compile
75+
})
76+
77+
it('teardown', function () {
78+
var vm = new Vue({
79+
el: el,
80+
template: '<partial name="test-{{id}}"></partial>',
81+
data: {
82+
id: 'a',
83+
},
84+
partials: {
85+
'test-a': 'a {{id}}'
86+
}
87+
})
88+
expect(vm._directives.length).toBe(2)
89+
expect(vm._directives[1].name).toBe('partial')
90+
expect(vm._watchers.length).toBe(2)
91+
vm._directives[1]._teardown()
92+
// the text-directive should've been removed.
93+
expect(vm._directives.length).toBe(1)
94+
expect(vm._directives[0].name).toBe('partial')
95+
expect(vm._watchers.length).toBe(0)
96+
})
97+
98+
})

0 commit comments

Comments
 (0)