Skip to content

Commit e29de0a

Browse files
committed
test for keep-alive
1 parent d6dd1ef commit e29de0a

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed

build/karma.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function (config) {
1313
{
1414
test: /\.js$/,
1515
exclude: /test|node_modules|vue\/src/,
16-
loader: 'babel?optional[]=runtime'
16+
loader: 'babel?optional[]=runtime&loose=all'
1717
}
1818
],
1919
postLoaders: [

src/override.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default function (Vue) {
99
if (route) {
1010
route.router._children.push(this)
1111
if (!this.$route) {
12+
/* istanbul ignore if */
1213
if (this._defineMeta) {
1314
// 0.12
1415
this._defineMeta('$route', route)

src/pipeline.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ export function activate (view, transition, depth, cb, reuse) {
193193
view.transition(component)
194194
} else {
195195
// no transition on first render, manual transition
196+
/* istanbul ignore if */
196197
if (view.setCurrent) {
197198
// 0.12 compat
198199
view.setCurrent(component)

test/unit/specs/core.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,69 @@ describe('Core', function () {
133133
], done)
134134
})
135135

136+
it('matching nested views with keep-alive', function (done) {
137+
router = new Router({ abstract: true })
138+
var spyA = jasmine.createSpy()
139+
var spySubA = jasmine.createSpy()
140+
router.map({
141+
'/a': {
142+
component: {
143+
template: 'VIEW A <router-view></router-view>',
144+
created: spyA
145+
},
146+
subRoutes: {
147+
'/': {
148+
component: {
149+
template: 'SUB A DEFAULT'
150+
}
151+
},
152+
'/sub-a': {
153+
component: {
154+
template: 'SUB A'
155+
}
156+
},
157+
'/sub-a-2': {
158+
component: {
159+
template: 'SUB A2',
160+
created: spySubA
161+
}
162+
}
163+
}
164+
},
165+
'/b': {
166+
component: {
167+
template: 'VIEW B <router-view></router-view>'
168+
},
169+
subRoutes: {
170+
'/sub-b': {
171+
component: {
172+
template: 'SUB B'
173+
}
174+
}
175+
}
176+
}
177+
})
178+
var App = Vue.extend({
179+
template: '<div><router-view keep-alive></router-view></div>'
180+
})
181+
router.start(App, el)
182+
assertRoutes([
183+
['/a', 'VIEW A SUB A DEFAULT'],
184+
['/a/sub-a', 'VIEW A SUB A'],
185+
['/a/sub-a-2', 'VIEW A SUB A2'],
186+
['/b/sub-b', 'VIEW B SUB B'],
187+
// revisit a kept-alive view
188+
['/a/sub-a-2', 'VIEW A SUB A2'],
189+
['/b', 'VIEW B '],
190+
// no match
191+
['/b/sub-a', '']
192+
], function () {
193+
expect(spyA.calls.count()).toBe(1)
194+
expect(spySubA.calls.count()).toBe(1)
195+
done()
196+
})
197+
})
198+
136199
it('route context', function (done) {
137200
Vue.config.silent = true
138201
router = new Router({ abstract: true })
@@ -581,25 +644,41 @@ describe('Core', function () {
581644
}
582645
})
583646

647+
// a synchronous hook
648+
var spy3 = jasmine.createSpy('before hook 3')
649+
router.beforeEach(function () {
650+
spy3()
651+
})
652+
584653
router.start(App, el)
585654
expect(spy1).toHaveBeenCalled()
586655
expect(spy2).not.toHaveBeenCalled()
656+
expect(spy3).not.toHaveBeenCalled()
587657
expect(router.app.$el.textContent).toBe('')
658+
588659
setTimeout(function () {
589660
expect(spy2).toHaveBeenCalled()
661+
expect(spy3).toHaveBeenCalled()
590662
expect(router.app.$el.textContent).toBe('default')
591663
router.go('/no')
592664
}, wait * 2)
665+
593666
function next () {
594667
expect(spy1.calls.count()).toBe(2)
595668
expect(spy2.calls.count()).toBe(2)
669+
expect(spy3.calls.count()).toBe(1) // aborted at 2
596670
expect(router.app.$el.textContent).toBe('default')
597671
router.go('/redirect/12345')
598672
}
673+
599674
function next2 () {
600675
expect(spy1.calls.count()).toBe(4) // go + redirect
601676
expect(spy2.calls.count()).toBe(3) // only go at this moment
677+
expect(spy3.calls.count()).toBe(1) // still 1
602678
setTimeout(function () {
679+
expect(spy1.calls.count()).toBe(4)
680+
expect(spy2.calls.count()).toBe(4)
681+
expect(spy3.calls.count()).toBe(2) // after redirect
603682
expect(router.app.$el.textContent).toBe('to 12345')
604683
done()
605684
}, wait * 2)

test/unit/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
{
1010
test: /\.js$/,
1111
exclude: /test|node_modules|vue\/src/,
12-
loader: 'babel?optional[]=runtime&loose=true'
12+
loader: 'babel?optional[]=runtime&loose=all'
1313
}
1414
]
1515
}

0 commit comments

Comments
 (0)