Skip to content

Commit 7afedcb

Browse files
committed
Add tests for beforeRouteUpdate next callback
1 parent 097dca8 commit 7afedcb

File tree

2 files changed

+93
-5
lines changed

2 files changed

+93
-5
lines changed

examples/navigation-guards/app.js

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const Baz = {
3333
},
3434
template: `
3535
<div>
36-
<p>baz ({{ saved ? 'saved' : 'not saved' }})<p>
36+
<p>baz ({{ saved ? 'saved' : 'not saved' }})</p>
3737
<button @click="saved = true">save</button>
3838
</div>
3939
`,
@@ -46,7 +46,7 @@ const Baz = {
4646
}
4747
}
4848

49-
// Baz implements an in-component beforeRouteEnter hook
49+
// Qux implements an in-component beforeRouteEnter hook
5050
const Qux = {
5151
data () {
5252
return {
@@ -87,6 +87,57 @@ const Quux = {
8787
}
8888
}
8989

90+
// Nested implements an in-component beforeRouteUpdate hook that uses
91+
// next(vm => {})
92+
const Nested = {
93+
data () {
94+
return {
95+
calls: 0,
96+
updates: 0
97+
}
98+
},
99+
template: `
100+
<div>
101+
<p>
102+
nested calls:{{ calls }} updates:{{ updates }}
103+
</p>
104+
<router-view></router-view>
105+
</div>
106+
`,
107+
beforeRouteUpdate (to, from, next) {
108+
// calls is incremented every time anything navigates, even if the
109+
// navigation is canceled by a child component.
110+
this.calls++
111+
112+
next(vm => {
113+
// updates is only incremented once all children confirm and complete
114+
// navigation. If any children load data async, then this will not be
115+
// called until all children have called next() themselves. If any child
116+
// cancels navigation, then this will never be called.
117+
vm.updates++
118+
})
119+
}
120+
}
121+
122+
// Buux implements a cancelable beforeRouteUpdate hook
123+
const Buux = {
124+
data () {
125+
return {
126+
prevId: 0
127+
}
128+
},
129+
template: `<div>buux id:{{ $route.params.id }} prevId:{{ prevId }}</div>`,
130+
beforeRouteUpdate (to, from, next) {
131+
if (window.confirm(`Navigate to ${to.path}?`)) {
132+
next(vm => {
133+
vm.prevId = from.params.id
134+
})
135+
} else {
136+
next(false)
137+
}
138+
}
139+
}
140+
90141
const router = new VueRouter({
91142
mode: 'history',
92143
base: __dirname,
@@ -114,7 +165,14 @@ const router = new VueRouter({
114165
} },
115166

116167
// in-component beforeRouteUpdate hook
117-
{ path: '/quux/:id', component: Quux }
168+
{ path: '/quux/:id', component: Quux },
169+
{ path: '/nested', component: Nested,
170+
children: [
171+
{ path: '', component: Home },
172+
{ path: 'qux', component: Qux },
173+
{ path: 'buux/:id', component: Buux }
174+
]
175+
}
118176
]
119177
})
120178

@@ -140,6 +198,10 @@ new Vue({
140198
<li><router-link to="/qux-async">/qux-async</router-link></li>
141199
<li><router-link to="/quux/1">/quux/1</router-link></li>
142200
<li><router-link to="/quux/2">/quux/2</router-link></li>
201+
<li><router-link to="/nested">/nested</router-link></li>
202+
<li><router-link to="/nested/qux">/nested/qux</router-link></li>
203+
<li><router-link to="/nested/buux/1">/nested/buux/1</router-link></li>
204+
<li><router-link to="/nested/buux/2">/nested/buux/2</router-link></li>
143205
</ul>
144206
<router-view class="view"></router-view>
145207
</div>

test/e2e/specs/navigation-guards.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
browser
99
.url('http://localhost:8080/navigation-guards/')
1010
.waitForElementVisible('#app', 1000)
11-
.assert.count('li a', 8)
11+
.assert.count('li a', 12)
1212
.assert.containsText('.view', 'home')
1313

1414
.click('li:nth-child(2) a')
@@ -130,6 +130,32 @@ module.exports = {
130130
.click('li:nth-child(7) a')
131131
.assert.urlEquals('http://localhost:8080/navigation-guards/quux/1')
132132
.assert.containsText('.view', 'id:1 prevId:2')
133-
.end()
133+
.click('li:nth-child(9) a')
134+
.assert.urlEquals('http://localhost:8080/navigation-guards/nested')
135+
.assert.containsText('.view', 'nested calls:0 updates:0')
136+
.click('li:nth-child(10) a')
137+
.assert.containsText('.view', 'nested calls:1 updates:0')
138+
.waitFor(300)
139+
.assert.urlEquals('http://localhost:8080/navigation-guards/nested/qux')
140+
.assert.containsText('.view', 'nested calls:1 updates:1')
141+
.assert.containsText('.view', 'Qux')
142+
.click('li:nth-child(11) a')
143+
.assert.urlEquals('http://localhost:8080/navigation-guards/nested/buux/1')
144+
.assert.containsText('.view', 'nested calls:2 updates:2')
145+
.assert.containsText('.view', 'buux id:1 prevId:0')
146+
.click('li:nth-child(12) a')
147+
.dismissAlert()
148+
.assert.urlEquals('http://localhost:8080/navigation-guards/nested/buux/1')
149+
.assert.containsText('.view', 'nested calls:3 updates:2')
150+
.assert.containsText('.view', 'buux id:1 prevId:0')
151+
.click('li:nth-child(12) a')
152+
.acceptAlert()
153+
.assert.urlEquals('http://localhost:8080/navigation-guards/nested/buux/2')
154+
.assert.containsText('.view', 'nested calls:4 updates:3')
155+
.assert.containsText('.view', 'buux id:2 prevId:1')
156+
.click('li:nth-child(9) a')
157+
.assert.urlEquals('http://localhost:8080/navigation-guards/nested')
158+
.assert.containsText('.view', 'nested calls:5 updates:4')
159+
.end()
134160
}
135161
}

0 commit comments

Comments
 (0)