1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/test/unit/router.test.js
Arunoda Susiripala 14c86bef1d Introduce a simple prefetching solution (#957)
* Implement a very simple prefetching solution.

* Remove next-prefetcher.

* Require 'whatwg-fetch' only in the client.

* Use xhr in the code.

* Use a simple fetching solution.

* Fix 404 and xhr status issue.

* Move the prefetching implementation to next/router.

* Add deprecated warnning for next/prefetch

* Run only 2 parellel prefetching request at a time.

* Change xhr to jsonPageRes.

* Improve the prefetching logic.

* Add unit tests covering the Router.prefetch()

* Update examples to use the new syntax.

* Update docs.

* Use execOnce() to manage warn printing.

* Remove prefetcher building from the flyfile.js
Because, we no longer use it.
2017-02-15 14:22:22 +05:30

88 lines
2.6 KiB
JavaScript

/* global describe, it, expect */
import Router from '../../dist/lib/router/router'
describe('Router', () => {
describe('.prefetch()', () => {
it('should prefetch a given page', async () => {
const router = new Router('/', {})
const res = { aa: 'res' }
const route = 'routex'
router.fetchUrl = (r) => {
expect(r).toBe(route)
return Promise.resolve(res)
}
await router.prefetch(route)
expect(router.prefetchedRoutes[route]).toBe(res)
})
it('should stop if it\'s prefetched already', async () => {
const router = new Router('/', {})
const route = 'routex'
router.prefetchedRoutes[route] = { aa: 'res' }
router.fetchUrl = () => { throw new Error('Should not happen') }
await router.prefetch(route)
})
it('should stop if it\'s currently prefetching', async () => {
const router = new Router('/', {})
const route = 'routex'
router.prefetchingRoutes[route] = true
router.fetchUrl = () => { throw new Error('Should not happen') }
await router.prefetch(route)
})
it('should ignore the response if it asked to do', async () => {
const router = new Router('/', {})
const res = { aa: 'res' }
const route = 'routex'
let called = false
router.fetchUrl = () => {
called = true
router.prefetchingRoutes[route] = 'IGNORE'
return Promise.resolve(res)
}
await router.prefetch(route)
expect(router.prefetchedRoutes[route]).toBeUndefined()
expect(called).toBe(true)
})
it('should only run two jobs at a time', async () => {
const router = new Router('/', {})
let count = 0
router.fetchUrl = () => {
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)
expect(Object.keys(router.prefetchingRoutes)).toEqual(['route1', 'route2'])
})
it('should run all the jobs', async () => {
const router = new Router('/', {})
const routes = ['route1', 'route2', 'route3', 'route4']
router.fetchUrl = () => Promise.resolve({ aa: 'res' })
await router.prefetch(routes[0])
await router.prefetch(routes[1])
await router.prefetch(routes[2])
await router.prefetch(routes[3])
expect(Object.keys(router.prefetchedRoutes)).toEqual(routes)
expect(Object.keys(router.prefetchingRoutes)).toEqual([])
})
})
})