@@ -4,6 +4,9 @@ import { ErrorTypes } from '../../src/errors'
44import { mockWarn } from '../vitest-mock-warn'
55import { vi , describe , expect , it } from 'vitest'
66
7+ const NEXT_DEPRECATION_MESSAGE =
8+ 'The `next()` callback in navigation guards is deprecated. Return the value instead of calling `next(value)`.'
9+
710// stub those two
811const to = START_LOCATION_NORMALIZED
912const from = {
@@ -15,28 +18,31 @@ const from = {
1518describe ( 'guardToPromiseFn' , ( ) => {
1619 mockWarn ( )
1720 it ( 'calls the guard with to, from and, next' , async ( ) => {
18- expect . assertions ( 2 )
21+ expect . assertions ( 3 )
1922 const spy = vi . fn ( ( to , from , next ) => next ( ) )
2023 await expect ( guardToPromiseFn ( spy , to , from ) ( ) ) . resolves . toEqual ( undefined )
2124 expect ( spy ) . toHaveBeenCalledWith ( to , from , expect . any ( Function ) )
25+ expect ( NEXT_DEPRECATION_MESSAGE ) . toHaveBeenWarned ( )
2226 } )
2327
2428 it ( 'resolves if next is called with no arguments' , async ( ) => {
25- expect . assertions ( 1 )
29+ expect . assertions ( 2 )
2630 await expect (
2731 guardToPromiseFn ( ( to , from , next ) => next ( ) , to , from ) ( )
2832 ) . resolves . toEqual ( undefined )
33+ expect ( NEXT_DEPRECATION_MESSAGE ) . toHaveBeenWarned ( )
2934 } )
3035
3136 it ( 'resolves if next is called with true' , async ( ) => {
32- expect . assertions ( 1 )
37+ expect . assertions ( 2 )
3338 await expect (
3439 guardToPromiseFn ( ( to , from , next ) => next ( true ) , to , from ) ( )
3540 ) . resolves . toEqual ( undefined )
41+ expect ( NEXT_DEPRECATION_MESSAGE ) . toHaveBeenWarned ( )
3642 } )
3743
3844 it ( 'rejects if next is called with false' , async ( ) => {
39- expect . assertions ( 1 )
45+ expect . assertions ( 2 )
4046 try {
4147 await guardToPromiseFn ( ( to , from , next ) => next ( false ) , to , from ) ( )
4248 } catch ( err ) {
@@ -46,10 +52,11 @@ describe('guardToPromiseFn', () => {
4652 type : ErrorTypes . NAVIGATION_ABORTED ,
4753 } )
4854 }
55+ expect ( NEXT_DEPRECATION_MESSAGE ) . toHaveBeenWarned ( )
4956 } )
5057
5158 it ( 'rejects if next is called with a string location' , async ( ) => {
52- expect . assertions ( 1 )
59+ expect . assertions ( 2 )
5360 try {
5461 await guardToPromiseFn ( ( to , from , next ) => next ( '/new' ) , to , from ) ( )
5562 } catch ( err ) {
@@ -59,10 +66,11 @@ describe('guardToPromiseFn', () => {
5966 type : ErrorTypes . NAVIGATION_GUARD_REDIRECT ,
6067 } )
6168 }
69+ expect ( NEXT_DEPRECATION_MESSAGE ) . toHaveBeenWarned ( )
6270 } )
6371
6472 it ( 'rejects if next is called with an object location' , async ( ) => {
65- expect . assertions ( 1 )
73+ expect . assertions ( 2 )
6674 let redirectTo = { path : '/new' }
6775 try {
6876 await guardToPromiseFn ( ( to , from , next ) => next ( redirectTo ) , to , from ) ( )
@@ -73,14 +81,16 @@ describe('guardToPromiseFn', () => {
7381 type : ErrorTypes . NAVIGATION_GUARD_REDIRECT ,
7482 } )
7583 }
84+ expect ( NEXT_DEPRECATION_MESSAGE ) . toHaveBeenWarned ( )
7685 } )
7786
7887 it ( 'rejects if next is called with an error' , async ( ) => {
79- expect . assertions ( 1 )
88+ expect . assertions ( 2 )
8089 let error = new Error ( 'nope' )
8190 await expect (
8291 guardToPromiseFn ( ( to , from , next ) => next ( error ) , to , from ) ( )
8392 ) . rejects . toBe ( error )
93+ expect ( NEXT_DEPRECATION_MESSAGE ) . toHaveBeenWarned ( )
8494 } )
8595
8696 it ( 'rejects if guard rejects a Promise' , async ( ) => {
@@ -265,7 +275,7 @@ describe('guardToPromiseFn', () => {
265275 } )
266276
267277 it ( 'does not warn if guard returns undefined' , async ( ) => {
268- expect . assertions ( 2 )
278+ expect . assertions ( 3 )
269279 await expect (
270280 guardToPromiseFn (
271281 ( to , from , next ) => {
@@ -278,5 +288,38 @@ describe('guardToPromiseFn', () => {
278288 ) . resolves . toEqual ( undefined )
279289
280290 expect ( 'callback was never called' ) . not . toHaveBeenWarned ( )
291+ expect ( NEXT_DEPRECATION_MESSAGE ) . toHaveBeenWarned ( )
292+ } )
293+
294+ describe ( 'next() callback deprecation' , ( ) => {
295+ it ( 'warns when guard calls next()' , async ( ) => {
296+ await guardToPromiseFn ( ( to , from , next ) => next ( ) , to , from ) ( )
297+ expect ( NEXT_DEPRECATION_MESSAGE ) . toHaveBeenWarned ( )
298+ } )
299+
300+ it ( 'warns only once per guard run when next() is called multiple times' , async ( ) => {
301+ await expect (
302+ guardToPromiseFn (
303+ ( to , from , next ) => {
304+ next ( )
305+ next ( )
306+ } ,
307+ to ,
308+ from
309+ ) ( )
310+ ) . resolves . toEqual ( undefined )
311+ expect ( NEXT_DEPRECATION_MESSAGE ) . toHaveBeenWarnedTimes ( 1 )
312+ expect ( 'called more than once' ) . toHaveBeenWarned ( )
313+ } )
314+
315+ it ( 'does not warn when guard returns value without next parameter' , async ( ) => {
316+ await guardToPromiseFn ( ( to , from ) => true , to , from ) ( )
317+ expect ( NEXT_DEPRECATION_MESSAGE ) . not . toHaveBeenWarned ( )
318+ } )
319+
320+ it ( 'does not warn when guard returns undefined without next parameter' , async ( ) => {
321+ await guardToPromiseFn ( ( to , from ) => { } , to , from ) ( )
322+ expect ( NEXT_DEPRECATION_MESSAGE ) . not . toHaveBeenWarned ( )
323+ } )
281324 } )
282325} )
0 commit comments