const { JSDOM } = require('jsdom')
const fakePromise = require('faked-promise')
+const tick = () => new Promise(resolve => process.nextTick(resolve))
+
/**
* @param {Partial<import('../src/router').RouterOptions> & { routes: import('../src/types').RouteRecord[]}} options
*/
await p
expect(router.currentRoute.fullPath).toBe('/foo')
})
+
+ it('waits in the right order', async () => {
+ const [p1, r1] = fakePromise()
+ const [p2, r2] = fakePromise()
+ const router = createRouter({ routes })
+ const guard1 = jest.fn(async (to, from, next) => {
+ await p1
+ next()
+ })
+ router.beforeEach(guard1)
+ const guard2 = jest.fn(async (to, from, next) => {
+ await p2
+ next()
+ })
+ router.beforeEach(guard2)
+ let navigation = router.push('/foo')
+ expect(router.currentRoute.fullPath).toBe('/')
+ expect(guard1).toHaveBeenCalled()
+ expect(guard2).not.toHaveBeenCalled()
+ r1()
+ // wait until the guard is called
+ await tick()
+ await tick()
+ expect(guard2).toHaveBeenCalled()
+ r2()
+ expect(router.currentRoute.fullPath).toBe('/')
+ await navigation
+ expect(guard2).toHaveBeenCalled()
+ expect(router.currentRoute.fullPath).toBe('/foo')
+ })
})
const { HTML5History } = require('../src/history/html5')
const { JSDOM } = require('jsdom')
-describe('History HTMl5', () => {
+describe.skip('History HTMl5', () => {
beforeAll(() => {
// TODO: move to utils for tests that need DOM
const dom = new JSDOM(
this.currentRoute = toLocation
}
- async navigate(
+ private async navigate(
to: RouteLocationNormalized,
from: RouteLocationNormalized
): Promise<TODO> {
// TODO: Will probably need to be some kind of queue in the future that allows to remove
// elements and other stuff
- const guards: Promise<any>[] = []
+ const guards: Array<() => Promise<any>> = []
for (const guard of this.beforeGuards) {
guards.push(
- new Promise((resolve, reject) => {
- const next: NavigationGuardCallback = (valid?: boolean) => {
- if (valid === false) reject(new Error('Aborted'))
- else resolve()
- }
+ () =>
+ new Promise((resolve, reject) => {
+ const next: NavigationGuardCallback = (valid?: boolean) => {
+ // TODO: better error
+ if (valid === false) reject(new Error('Aborted'))
+ else resolve()
+ }
- guard(to, from, next)
- })
+ guard(to, from, next)
+ })
)
}
console.log('Guarding against', guards.length, 'guards')
for (const guard of guards) {
- await guard
+ await guard()
}
}