Skip to content

Commit b04c209

Browse files
committed
test for 3f943c6, also extract inDoc patch for PhantomJS tests into external file
1 parent 3f943c6 commit b04c209

File tree

5 files changed

+64
-28
lines changed

5 files changed

+64
-28
lines changed

gruntfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ module.exports = function (grunt) {
4141
frameworks: ['jasmine', 'commonjs'],
4242
files: [
4343
'src/**/*.js',
44+
'test/unit/lib/indoc_patch.js',
4445
'test/unit/specs/**/*.js'
4546
],
4647
preprocessors: {
4748
'src/**/*.js': ['commonjs'],
49+
'test/unit/lib/indoc_patch.js': ['commonjs'],
4850
'test/unit/specs/**/*.js': ['commonjs']
4951
},
5052
singleRun: true
@@ -61,6 +63,7 @@ module.exports = function (grunt) {
6163
reporters: ['progress', 'coverage'],
6264
preprocessors: {
6365
'src/**/*.js': ['commonjs', 'coverage'],
66+
'test/unit/lib/indoc_patch.js': ['commonjs'],
6467
'test/unit/specs/**/*.js': ['commonjs']
6568
},
6669
coverageReporter: {

test/unit/lib/indoc_patch.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// PhantomJS always return false when using Element.contains
2+
// on a comment node - so we have to patch the inDoc util
3+
// function when running in PhantomJS.
4+
5+
var _ = require('../../../src/util')
6+
var inDoc = _.inDoc
7+
8+
_.inDoc = function (el) {
9+
if (el && el.nodeType === 8) {
10+
return manualInDoc(el)
11+
}
12+
return inDoc(el)
13+
}
14+
15+
function manualInDoc (el) {
16+
while (el) {
17+
if (el === document.documentElement) {
18+
return true
19+
}
20+
el = el.parentNode
21+
}
22+
return false
23+
}

test/unit/specs/directives/component_spec.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// patch inDoc
2+
require('../../lib/indoc_patch')
13
var _ = require('../../../../src/util')
24
var Vue = require('../../../../src/vue')
35

@@ -7,9 +9,14 @@ if (_.inBrowser) {
79
var el
810
beforeEach(function () {
911
el = document.createElement('div')
12+
document.body.appendChild(el)
1013
spyOn(_, 'warn')
1114
})
1215

16+
afterEach(function () {
17+
document.body.removeChild(el)
18+
})
19+
1320
it('static', function () {
1421
var vm = new Vue({
1522
el: el,
@@ -248,15 +255,6 @@ if (_.inBrowser) {
248255
var spy1 = jasmine.createSpy('enter')
249256
var spy2 = jasmine.createSpy('leave')
250257
var next
251-
// === IMPORTANT ===
252-
// PhantomJS always returns false when calling
253-
// Element.contains() on a comment node. This causes
254-
// transitions to be skipped. Monkey patching here
255-
// isn't ideal but does the job...
256-
var inDoc = _.inDoc
257-
_.inDoc = function () {
258-
return true
259-
}
260258
var vm = new Vue({
261259
el: el,
262260
data: {
@@ -289,8 +287,6 @@ if (_.inBrowser) {
289287
next()
290288
expect(spy2).toHaveBeenCalled()
291289
expect(el.textContent).toBe('BBB')
292-
// clean up
293-
_.inDoc = inDoc
294290
done()
295291
})
296292
})
@@ -299,10 +295,6 @@ if (_.inBrowser) {
299295
var spy1 = jasmine.createSpy('enter')
300296
var spy2 = jasmine.createSpy('leave')
301297
var next
302-
var inDoc = _.inDoc
303-
_.inDoc = function () {
304-
return true
305-
}
306298
var vm = new Vue({
307299
el: el,
308300
data: {
@@ -335,8 +327,6 @@ if (_.inBrowser) {
335327
next()
336328
expect(spy2).toHaveBeenCalled()
337329
expect(el.textContent).toBe('BBB')
338-
// clean up
339-
_.inDoc = inDoc
340330
done()
341331
})
342332
})

test/unit/specs/directives/repeat_spec.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// patch inDoc
2+
require('../../lib/indoc_patch')
13
var _ = require('../../../../src/util')
24
var Vue = require('../../../../src/vue')
35

@@ -456,15 +458,7 @@ if (_.inBrowser) {
456458
})
457459

458460
it('with transition', function (done) {
459-
// === IMPORTANT ===
460-
// PhantomJS always returns false when calling
461-
// Element.contains() on a comment node. This causes
462-
// transitions to be skipped. Monkey patching here
463-
// isn't ideal but does the job...
464-
var inDoc = _.inDoc
465-
_.inDoc = function () {
466-
return true
467-
}
461+
document.body.appendChild(el)
468462
var vm = new Vue({
469463
el: el,
470464
template: '<div v-repeat="items" v-transition="test">{{a}}</div>',
@@ -482,8 +476,7 @@ if (_.inBrowser) {
482476
vm.items.splice(1, 1, {a:4})
483477
setTimeout(function () {
484478
expect(el.innerHTML).toBe('<div>1</div><div>4</div><div>3</div><!--v-repeat-->')
485-
// clean up
486-
_.inDoc = inDoc
479+
document.body.removeChild(el)
487480
done()
488481
}, 30)
489482
})

test/unit/specs/misc_spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// patch inDoc
2+
require('../lib/indoc_patch')
13
// test cases for edge cases & bug fixes
24
var Vue = require('../../../src/vue')
35

@@ -22,4 +24,29 @@ describe('Misc', function () {
2224
expect(vm.$el.textContent).toBe('yo hi')
2325
})
2426

27+
it('attached/detached hooks for transcluded components', function () {
28+
var spy1 = jasmine.createSpy('attached')
29+
var spy2 = jasmine.createSpy('detached')
30+
var el = document.createElement('div')
31+
el.innerHTML = '<div v-component="outter" v-ref="outter"><div v-component="inner"></div></div>'
32+
document.body.appendChild(el)
33+
34+
var vm = new Vue({
35+
el: el,
36+
components: {
37+
outter: {
38+
template: '<content></content>'
39+
},
40+
inner: {
41+
template: 'hi',
42+
attached: spy1,
43+
detached: spy2
44+
}
45+
}
46+
})
47+
expect(spy1).toHaveBeenCalled()
48+
vm.$.outter.$remove()
49+
expect(spy2).toHaveBeenCalled()
50+
})
51+
2552
})

0 commit comments

Comments
 (0)