import { guardToPromiseFn } from '../../src/navigationGuards'
import { START_LOCATION_NORMALIZED } from '../../src/types'
import { ErrorTypes } from '../../src/errors'
+import { mockWarn } from 'jest-mock-warn'
// stub those two
const to = START_LOCATION_NORMALIZED
}
describe('guardToPromiseFn', () => {
+ mockWarn()
it('calls the guard with to, from and, next', async () => {
const spy = jest.fn((to, from, next) => next())
await expect(guardToPromiseFn(spy, to, from)()).resolves.toEqual(undefined)
}
})
})
+
+ it('warns if guard resolves without calling next', async () => {
+ expect.assertions(2)
+ await expect(
+ guardToPromiseFn((to, from, next) => false, to, from)()
+ ).rejects.toEqual(expect.any(Error))
+
+ // try {
+ // await guardToPromiseFn((to, from, next) => false, to, from)()
+ // } catch (error) {
+ // expect(error).toEqual(expect.any(Error))
+ // }
+
+ expect('callback was never called').toHaveBeenWarned()
+ })
})
)
if (guard.length < 3) guardCall.then(next)
+ if (__DEV__ && guard.length > 2)
+ guardCall.then(() => {
+ // @ts-ignore: _called is added at canOnlyBeCalledOnce
+ if (!next._called)
+ warn(
+ `The "next" callback was never called inside of ${
+ guard.name ? '"' + guard.name + '"' : ''
+ }:\n${guard.toString()}\n. If you are returning a value instead of calling "next", make sure to remove the "next" parameter from your function.`
+ )
+ reject(new Error('Invalid navigation guard'))
+ })
guardCall.catch(err => reject(err))
})
}
warn(
`The "next" callback was called more than once in one navigation guard when going from "${from.fullPath}" to "${to.fullPath}". It should be called exactly one time in each navigation guard. This will fail in production.`
)
+ // @ts-ignore: we put it in the original one because it's easier to check
+ next._called = true
if (called === 1) next.apply(null, arguments as any)
}
}
(location: RouteLocationRaw): void
(valid: boolean): void
(cb: NavigationGuardNextCallback): void
+ /**
+ * Allows to detect if `next` isn't called in a resolved guard. Used
+ * internally in DEV mode to emit a warning. Commented out to simplify
+ * typings.
+ * @internal
+ */
+ // _called: boolean
}
export type NavigationGuardNextCallback = (vm: ComponentPublicInstance) => any