2017-02-15 08:52:22 +00:00
|
|
|
/* global describe, it, expect */
|
|
|
|
import Router from '../../dist/lib/router/router'
|
|
|
|
|
|
|
|
describe('Router', () => {
|
2017-02-17 04:12:32 +00:00
|
|
|
const request = { clone: () => null }
|
2017-02-15 08:52:22 +00:00
|
|
|
describe('.prefetch()', () => {
|
|
|
|
it('should prefetch a given page', async () => {
|
|
|
|
const router = new Router('/', {})
|
2017-02-17 04:12:32 +00:00
|
|
|
const promise = Promise.resolve(request)
|
2017-02-15 08:52:22 +00:00
|
|
|
const route = 'routex'
|
2017-02-15 19:29:42 +00:00
|
|
|
router.doFetchRoute = (r) => {
|
2017-02-15 08:52:22 +00:00
|
|
|
expect(r).toBe(route)
|
2017-02-15 19:29:42 +00:00
|
|
|
return promise
|
2017-02-15 08:52:22 +00:00
|
|
|
}
|
|
|
|
await router.prefetch(route)
|
|
|
|
|
2017-02-15 19:29:42 +00:00
|
|
|
expect(router.fetchingRoutes[route]).toBe(promise)
|
2017-02-15 08:52:22 +00:00
|
|
|
})
|
|
|
|
|
2017-02-15 19:29:42 +00:00
|
|
|
it('should stop if it\'s prefetching already', async () => {
|
2017-02-15 08:52:22 +00:00
|
|
|
const router = new Router('/', {})
|
|
|
|
const route = 'routex'
|
2017-02-17 04:12:32 +00:00
|
|
|
router.fetchingRoutes[route] = Promise.resolve(request)
|
2017-02-15 19:29:42 +00:00
|
|
|
router.doFetchRoute = () => { throw new Error('Should not happen') }
|
2017-02-15 08:52:22 +00:00
|
|
|
await router.prefetch(route)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should only run two jobs at a time', async () => {
|
|
|
|
const router = new Router('/', {})
|
|
|
|
let count = 0
|
|
|
|
|
2017-02-15 19:29:42 +00:00
|
|
|
router.doFetchRoute = () => {
|
2017-02-15 08:52:22 +00:00
|
|
|
count++
|
|
|
|
return new Promise((resolve) => {})
|
|
|
|
}
|
|
|
|
|
|
|
|
router.prefetch('route1')
|
|
|
|
router.prefetch('route2')
|
|
|
|
router.prefetch('route3')
|
|
|
|
router.prefetch('route4')
|
|
|
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
|
|
|
|
|
|
expect(count).toBe(2)
|
2017-02-15 19:29:42 +00:00
|
|
|
expect(Object.keys(router.fetchingRoutes)).toEqual(['route1', 'route2'])
|
2017-02-15 08:52:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should run all the jobs', async () => {
|
|
|
|
const router = new Router('/', {})
|
|
|
|
const routes = ['route1', 'route2', 'route3', 'route4']
|
|
|
|
|
2017-02-17 04:12:32 +00:00
|
|
|
router.doFetchRoute = () => Promise.resolve(request)
|
2017-02-15 08:52:22 +00:00
|
|
|
|
|
|
|
await router.prefetch(routes[0])
|
|
|
|
await router.prefetch(routes[1])
|
|
|
|
await router.prefetch(routes[2])
|
|
|
|
await router.prefetch(routes[3])
|
|
|
|
|
2017-02-15 19:29:42 +00:00
|
|
|
expect(Object.keys(router.fetchingRoutes)).toEqual(routes)
|
2017-02-15 08:52:22 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|