Skip to content

Commit c480f55

Browse files
committed
test: add e2e test for #2561
1 parent 7f55411 commit c480f55

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

Diff for: examples/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ <h1>Vue Router Examples</h1>
2525
<li><a href="auth-flow">Auth Flow</a></li>
2626
<li><a href="discrete-components">Discrete Components</a></li>
2727
<li><a href="nested-router">Nested Routers</a></li>
28+
<li><a href="navigation-guard-callback">Navigation Guard Callback</a></li>
2829
</ul>
2930
</body>
3031
</html>

Diff for: examples/navigation-guard-callback/app.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import Vue from 'vue'
2+
import VueRouter from 'vue-router'
3+
4+
Vue.use(VueRouter)
5+
6+
const Foo = {
7+
template: '<div class="current">foo</div>',
8+
beforeRouteEnter (to, from, next) {
9+
// in-component beforeRouteEnter hook
10+
next(vm => {
11+
// print log
12+
app.logText = 'beforeRouteEnter callback emited'
13+
})
14+
}
15+
}
16+
17+
const Bar = { template: '<div class="current">bar</div>' }
18+
19+
const router = new VueRouter({
20+
mode: 'history',
21+
base: __dirname,
22+
routes: [
23+
// foo1
24+
{ path: '/foo1', component: Foo },
25+
26+
// foo2, same component as foo1
27+
{ path: '/foo2', component: Foo },
28+
29+
// bar
30+
{ path: '/bar', component: Bar }
31+
]
32+
})
33+
34+
const app = new Vue({
35+
data: {
36+
applyKeepAlive: false,
37+
logText: ''
38+
},
39+
router,
40+
template: `
41+
<div id="app">
42+
<h1>Navigation Guard Callback</h1>
43+
<ul>
44+
<li><router-link to="/" id="root">/</router-link></li>
45+
<li><router-link to="/foo1" id="foo1">/foo1</router-link></li>
46+
<li><router-link to="/foo2" id="foo2">/foo2</router-link></li>
47+
<li><router-link to="/bar" id="bar">/bar</router-link></li>
48+
</ul>
49+
<div v-if="applyKeepAlive" class="keep-alive-wrap">
50+
<keep-alive>
51+
<router-view class="view"></router-view>
52+
</keep-alive>
53+
</div>
54+
<div v-else>
55+
<router-view class="view"></router-view>
56+
</div>
57+
<div class="log">{{logText}}</div>
58+
<div>
59+
<button class="btn-apply-keep-alive" @click="toggle">apply keep-alive</button>
60+
<button class="btn-clear-log" @click="clear">clear log</button>
61+
</div>
62+
</div>
63+
`,
64+
methods: {
65+
toggle () {
66+
// wrap router-view with keep-alive
67+
this.applyKeepAlive = true
68+
},
69+
clear () {
70+
// clear log
71+
this.logText = ''
72+
}
73+
}
74+
}).$mount('#app')

Diff for: examples/navigation-guard-callback/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<link rel="stylesheet" href="/global.css">
3+
<style>
4+
.log { border: 1px solid #333; margin: 20px 0; min-height: 100px; }
5+
a.router-link-exact-active{ color: #f66; }
6+
</style>
7+
<a href="/">&larr; Examples index</a>
8+
<div id="app"></div>
9+
<script src="/__build__/shared.chunk.js"></script>
10+
<script src="/__build__/navigation-guard-callback.js"></script>

Diff for: test/e2e/specs/navigation-guard-callback.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module.exports = {
2+
'navigation guard callback': function (browser) {
3+
browser
4+
.url('http://localhost:8080/navigation-guard-callback/')
5+
.waitForElementVisible('#app', 1000)
6+
.assert.count('li a', 4)
7+
8+
.click('.btn-clear-log')
9+
.assert.containsText('.log', '')
10+
.click('#foo1')
11+
.assert.containsText('.current', 'foo')
12+
.assert.containsText('.log', 'beforeRouteEnter callback emited')
13+
14+
.click('#bar')
15+
.assert.containsText('.current', 'bar')
16+
17+
.click('.btn-clear-log')
18+
.assert.containsText('.log', '')
19+
.click('#foo2')
20+
.assert.containsText('.current', 'foo')
21+
.assert.containsText('.log', 'beforeRouteEnter callback emited')
22+
23+
// back to root path
24+
.click('#root')
25+
26+
// apply keep-alive
27+
.click('.btn-apply-keep-alive')
28+
29+
// do the same
30+
.click('.btn-clear-log')
31+
.assert.containsText('.log', '')
32+
.click('#foo1')
33+
.assert.containsText('.current', 'foo')
34+
.assert.containsText('.log', 'beforeRouteEnter callback emited')
35+
36+
.click('#bar')
37+
.assert.containsText('.current', 'bar')
38+
39+
.click('.btn-clear-log')
40+
.assert.containsText('.log', '')
41+
.click('#foo2')
42+
.assert.containsText('.current', 'foo')
43+
.assert.containsText('.log', 'beforeRouteEnter callback emited')
44+
.end()
45+
}
46+
}

0 commit comments

Comments
 (0)