Skip to content

Commit 2938bff

Browse files
committed
test: refactor next usage
1 parent b5a47e0 commit 2938bff

12 files changed

+93
-90
lines changed

packages/router/__tests__/errors.spec.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ describe('Errors & Navigation failures', () => {
8686

8787
it('next("/location") triggers afterEach', async () => {
8888
await testNavigation(
89-
((to, from, next) => {
90-
if (to.path === '/location') next()
91-
else next('/location')
89+
((to, from) => {
90+
if (to.path === '/location') return
91+
else return '/location'
9292
}) as NavigationGuard,
9393
undefined
9494
)
@@ -109,10 +109,13 @@ describe('Errors & Navigation failures', () => {
109109
it('triggers afterEach if a new navigation happens', async () => {
110110
const { router } = createRouter()
111111
const [promise, resolve] = fakePromise()
112-
router.beforeEach((to, from, next) => {
112+
router.beforeEach(async (to, from) => {
113113
// let it hang otherwise
114-
if (to.path === '/') next()
115-
else promise.then(() => next())
114+
if (to.path === '/') return
115+
else {
116+
await promise
117+
return
118+
}
116119
})
117120

118121
let from = router.currentRoute.value
@@ -236,9 +239,9 @@ describe('Errors & Navigation failures', () => {
236239

237240
it('next("/location") triggers afterEach with history.back', async () => {
238241
await testHistoryNavigation(
239-
((to, from, next) => {
240-
if (to.path === '/location') next()
241-
else next('/location')
242+
((to, from) => {
243+
if (to.path === '/location') return
244+
else return '/location'
242245
}) as NavigationGuard,
243246
undefined
244247
)

packages/router/__tests__/guards/beforeEach.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ describe('router.beforeEach', () => {
8787
const spy = vi.fn()
8888
const router = createRouter({ routes })
8989
await router.push('/foo')
90-
spy.mockImplementation((to, from, next) => {
90+
spy.mockImplementation((to, from) => {
9191
// only allow going to /other
92-
if (to.fullPath !== '/other') next('/other')
93-
else next()
92+
if (to.fullPath !== '/other') return '/other'
93+
else return
9494
})
9595
router.beforeEach(spy)
9696
expect(spy).not.toHaveBeenCalled()
@@ -160,11 +160,11 @@ describe('router.beforeEach', () => {
160160
const spy = vi.fn()
161161
const router = createRouter({ routes })
162162
await router.push('/')
163-
spy.mockImplementation((to, from, next) => {
163+
spy.mockImplementation((to, from) => {
164164
// only allow going to /other
165165
const i = Number(to.params.i)
166-
if (i >= 3) next()
167-
else next(redirectFn(String(i + 1)))
166+
if (i >= 3) return
167+
else return redirectFn(String(i + 1))
168168
})
169169
router.beforeEach(spy)
170170
expect(spy).not.toHaveBeenCalled()
@@ -210,9 +210,9 @@ describe('router.beforeEach', () => {
210210
it('waits before navigating', async () => {
211211
const [promise, resolve] = fakePromise()
212212
const router = createRouter({ routes })
213-
router.beforeEach(async (to, from, next) => {
213+
router.beforeEach(async (to, from) => {
214214
await promise
215-
next()
215+
return
216216
})
217217
const p = router.push('/foo')
218218
expect(router.currentRoute.value.fullPath).toBe('/')
@@ -227,17 +227,17 @@ describe('router.beforeEach', () => {
227227
const router = createRouter({ routes })
228228
const guard1 = vi.fn()
229229
let order = 0
230-
guard1.mockImplementationOnce(async (to, from, next) => {
230+
guard1.mockImplementationOnce(async (to, from) => {
231231
expect(order++).toBe(0)
232232
await p1
233-
next()
233+
return
234234
})
235235
router.beforeEach(guard1)
236236
const guard2 = vi.fn()
237-
guard2.mockImplementationOnce(async (to, from, next) => {
237+
guard2.mockImplementationOnce(async (to, from) => {
238238
expect(order++).toBe(1)
239239
await p2
240-
next()
240+
return
241241
})
242242
router.beforeEach(guard2)
243243
let navigation = router.push('/foo')

packages/router/__tests__/guards/beforeEnter.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ describe('beforeEnter', () => {
157157
it('waits before navigating', async () => {
158158
const [promise, resolve] = fakePromise()
159159
const router = createRouter({ routes })
160-
beforeEnter.mockImplementationOnce(async (to, from, next) => {
160+
beforeEnter.mockImplementationOnce(async (to, from) => {
161161
await promise
162-
next()
162+
return
163163
})
164164
const p = router.push('/foo')
165165
expect(router.currentRoute.value.fullPath).toBe('/')
@@ -172,13 +172,13 @@ describe('beforeEnter', () => {
172172
const [p1, r1] = fakePromise()
173173
const [p2, r2] = fakePromise()
174174
const router = createRouter({ routes })
175-
beforeEnters[0].mockImplementationOnce(async (to, from, next) => {
175+
beforeEnters[0].mockImplementationOnce(async (to, from) => {
176176
await p1
177-
next()
177+
return
178178
})
179-
beforeEnters[1].mockImplementationOnce(async (to, from, next) => {
179+
beforeEnters[1].mockImplementationOnce(async (to, from) => {
180180
await p2
181-
next()
181+
return
182182
})
183183
const p = router.push('/multiple')
184184
expect(router.currentRoute.value.fullPath).toBe('/')

packages/router/__tests__/guards/beforeRouteEnter.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ describe('beforeRouteEnter', () => {
111111

112112
it('calls beforeRouteEnter guards on navigation', async () => {
113113
const router = createRouter({ routes })
114-
beforeRouteEnter.mockImplementationOnce((to, from, next) => {
115-
if (to.params.n !== 'valid') return next(false)
116-
next()
114+
beforeRouteEnter.mockImplementationOnce((to, from) => {
115+
if (to.params.n !== 'valid') return false
116+
return
117117
})
118118
await router.push('/guard/valid')
119119
expect(beforeRouteEnter).toHaveBeenCalledTimes(1)
@@ -183,8 +183,8 @@ describe('beforeRouteEnter', () => {
183183

184184
it('aborts navigation if one of the named views aborts', async () => {
185185
const router = createRouter({ routes })
186-
named.default.mockImplementationOnce((to, from, next) => {
187-
next(false)
186+
named.default.mockImplementationOnce((to, from) => {
187+
return false
188188
})
189189
named.other.mockImplementationOnce(noGuard)
190190
await router.push('/named').catch(err => {}) // catch abort
@@ -204,9 +204,9 @@ describe('beforeRouteEnter', () => {
204204
it('waits before navigating', async () => {
205205
const [promise, resolve] = fakePromise()
206206
const router = createRouter({ routes })
207-
beforeRouteEnter.mockImplementationOnce(async (to, from, next) => {
207+
beforeRouteEnter.mockImplementationOnce(async (to, from) => {
208208
await promise
209-
next()
209+
return
210210
})
211211
const p = router.push('/foo')
212212
expect(router.currentRoute.value.fullPath).toBe('/')

packages/router/__tests__/guards/beforeRouteLeave.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ describe('beforeRouteLeave', () => {
9494

9595
it('calls beforeRouteLeave guard on navigation', async () => {
9696
const router = createRouter({ routes })
97-
beforeRouteLeave.mockImplementationOnce((to, from, next) => {
98-
if (to.path === 'foo') next(false)
99-
else next()
97+
beforeRouteLeave.mockImplementationOnce((to, from) => {
98+
if (to.path === 'foo') return false
99+
else return
100100
})
101101
await router.push('/guard')
102102
expect(beforeRouteLeave).not.toHaveBeenCalled()
@@ -110,8 +110,8 @@ describe('beforeRouteLeave', () => {
110110

111111
it('does not call beforeRouteLeave guard if the view is not mounted', async () => {
112112
const router = createRouter({ routes })
113-
beforeRouteLeave.mockImplementationOnce((to, from, next) => {
114-
next()
113+
beforeRouteLeave.mockImplementationOnce((to, from) => {
114+
return
115115
})
116116
await router.push('/guard')
117117
expect(beforeRouteLeave).not.toHaveBeenCalled()
@@ -158,17 +158,17 @@ describe('beforeRouteLeave', () => {
158158
await router.push({ name: 'nested-nested-foo' })
159159
resetMocks()
160160
let count = 0
161-
nested.nestedNestedFoo.mockImplementation((to, from, next) => {
161+
nested.nestedNestedFoo.mockImplementation((to, from) => {
162162
expect(count++).toBe(0)
163-
next()
163+
return
164164
})
165-
nested.nestedNested.mockImplementation((to, from, next) => {
165+
nested.nestedNested.mockImplementation((to, from) => {
166166
expect(count++).toBe(1)
167-
next()
167+
return
168168
})
169-
nested.parent.mockImplementation((to, from, next) => {
169+
nested.parent.mockImplementation((to, from) => {
170170
expect(count++).toBe(2)
171-
next()
171+
return
172172
})
173173

174174
// simulate a mounted route component
@@ -184,8 +184,8 @@ describe('beforeRouteLeave', () => {
184184

185185
it('can cancel navigation', async () => {
186186
const router = createRouter({ routes })
187-
beforeRouteLeave.mockImplementationOnce(async (to, from, next) => {
188-
next(false)
187+
beforeRouteLeave.mockImplementationOnce(async (to, from) => {
188+
return false
189189
})
190190
await router.push('/guard')
191191
const p = router.push('/')

packages/router/__tests__/guards/beforeRouteUpdate.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ describe('beforeRouteUpdate', () => {
6868
it('waits before navigating', async () => {
6969
const [promise, resolve] = fakePromise()
7070
const router = createRouter({ routes })
71-
beforeRouteUpdate.mockImplementationOnce(async (to, from, next) => {
71+
beforeRouteUpdate.mockImplementationOnce(async (to, from) => {
7272
await promise
73-
next()
73+
return
7474
})
7575
await router.push('/guard/one')
7676
const p = router.push('/guard/foo')

packages/router/__tests__/guards/extractComponentsGuards.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ const ErrorLazyLoad: RouteRecordRaw = {
4040

4141
beforeEach(() => {
4242
beforeRouteEnter.mockReset()
43-
beforeRouteEnter.mockImplementation((to, from, next) => {
44-
next()
43+
beforeRouteEnter.mockImplementation((to, from) => {
44+
return
4545
})
4646
})
4747

packages/router/__tests__/initialNavigation.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ const routes: RouteRecordRaw[] = [
1919
{
2020
path: '/home-before',
2121
component,
22-
beforeEnter: (to, from, next) => {
23-
next('/')
22+
beforeEnter: (to, from) => {
23+
return '/'
2424
},
2525
},
2626
{ path: '/bar', component },

packages/router/__tests__/lazyLoading.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ describe('Lazy Loading', () => {
159159

160160
it('avoid fetching async component if navigation is cancelled through beforeEnter', async () => {
161161
const { component, resolve } = createLazyComponent()
162-
const spy = vi.fn((to, from, next) => next(false))
162+
const spy = vi.fn((to, from) => false)
163163
const { router } = newRouter({
164164
routes: [
165165
{
@@ -187,7 +187,7 @@ describe('Lazy Loading', () => {
187187
],
188188
})
189189

190-
const spy = vi.fn((to, from, next) => next(false))
190+
const spy = vi.fn((to, from) => false)
191191

192192
router.beforeEach(spy)
193193

@@ -199,7 +199,7 @@ describe('Lazy Loading', () => {
199199

200200
it('invokes beforeRouteEnter after lazy loading the component', async () => {
201201
const { promise, resolve } = createLazyComponent()
202-
const spy = vi.fn((to, from, next) => next())
202+
const spy = vi.fn((to, from) => {})
203203
const component = vi.fn(() =>
204204
promise.then(() => ({ beforeRouteEnter: spy }))
205205
)
@@ -215,7 +215,7 @@ describe('Lazy Loading', () => {
215215

216216
it('beforeRouteLeave works on a lazy loaded component', async () => {
217217
const { promise, resolve } = createLazyComponent()
218-
const spy = vi.fn((to, from, next) => next())
218+
const spy = vi.fn((to, from) => {})
219219
const component = vi.fn(() =>
220220
promise.then(() => ({ beforeRouteLeave: spy }))
221221
)
@@ -240,7 +240,7 @@ describe('Lazy Loading', () => {
240240

241241
it('beforeRouteUpdate works on a lazy loaded component', async () => {
242242
const { promise, resolve } = createLazyComponent()
243-
const spy = vi.fn((to, from, next) => next())
243+
const spy = vi.fn((to, from) => {})
244244
const component = vi.fn(() =>
245245
promise.then(() => ({ beforeRouteUpdate: spy }))
246246
)

packages/router/__tests__/multipleApps.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ describe('Multiple apps', () => {
3434
it('does not listen to url changes before being ready', async () => {
3535
const { router, history } = newRouter()
3636

37-
const spy = vi.fn((to, from, next) => {
38-
next()
37+
const spy = vi.fn((to, from) => {
38+
return
3939
})
4040
router.beforeEach(spy)
4141

0 commit comments

Comments
 (0)