]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: use new histories
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 9 Oct 2019 13:15:09 +0000 (15:15 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 9 Oct 2019 13:15:09 +0000 (15:15 +0200)
__tests__/errors.spec.ts
__tests__/guards/component-beforeRouteEnter.spec.ts
__tests__/guards/component-beforeRouteLeave.spec.ts
__tests__/guards/component-beforeRouteUpdate.spec.ts
__tests__/guards/global-after.spec.ts
__tests__/guards/global-beforeEach.spec.ts
__tests__/guards/route-beforeEnter.spec.ts
__tests__/router.spec.ts
__tests__/ssr/shared.ts
__tests__/url-encoding.spec.ts

index 0bd4ed635246afb9b3c2da23f2d61447a71f73f7..61b94575dd97a863df9bca13cf891da48ca15b4d 100644 (file)
@@ -1,5 +1,4 @@
-import { AbstractHistory } from '../src/history/abstract'
-import { Router } from '../src/router'
+import { Router, createMemoryHistory } from '../src'
 import { NavigationAborted, NavigationGuardRedirect } from '../src/errors'
 import { components, tick } from './utils'
 import { RouteRecord } from '../src/types'
@@ -24,7 +23,7 @@ const routes: RouteRecord[] = [
 
 const onError = jest.fn()
 function createRouter() {
-  const history = new AbstractHistory()
+  const history = createMemoryHistory()
   const router = new Router({
     history,
     routes,
index f2aaa52fd86ff0b960ae62e50d293a84432ed0b4..3c4ecda4e4afcb4c84cf98f6cb7700236c60374d 100644 (file)
@@ -1,14 +1,14 @@
-import { HTML5History } from '../../src/history/html5'
-import { Router, RouterOptions } from '../../src/router'
+import { RouterOptions } from '../../src/router'
 import fakePromise from 'faked-promise'
 import { NAVIGATION_TYPES, createDom, noGuard } from '../utils'
 import { RouteRecord, NavigationGuard } from '../../src/types'
+import { Router, createHistory } from '../../src'
 
 function createRouter(
   options: Partial<RouterOptions> & { routes: RouteRecord[] }
 ) {
   return new Router({
-    history: new HTML5History(),
+    history: createHistory(),
     ...options,
   })
 }
index aee7cca70d93fb3c669440e0779d6ea1fe152705..204ddf4df7406a2c4824815dea8f9263b8eddf3d 100644 (file)
@@ -1,14 +1,14 @@
-import { HTML5History } from '../../src/history/html5'
-import { Router, RouterOptions } from '../../src/router'
+import { RouterOptions, Router } from '../../src/router'
 import { NAVIGATION_TYPES, createDom, noGuard } from '../utils'
 import { RouteRecord } from '../../src/types'
+import { createHistory } from '../../src'
 
 // TODO: refactor in utils
 function createRouter(
   options: Partial<RouterOptions> & { routes: RouteRecord[] }
 ) {
   return new Router({
-    history: new HTML5History(),
+    history: createHistory(),
     ...options,
   })
 }
index 46172ba85f0798f6111701940e96cba17545c87d..63625ca247ffcc5987b4a230347748de98a3c59e 100644 (file)
@@ -1,7 +1,7 @@
-import { HTML5History } from '../../src/history/html5'
-import { Router } from '../../src/router'
 import fakePromise from 'faked-promise'
 import { NAVIGATION_TYPES, createDom, noGuard } from '../utils'
+import { Router, createHistory } from '../../src'
+import { RouteRecord } from '../../src/types'
 
 function createRouter(
   options: Partial<import('../../src/router').RouterOptions> & {
@@ -9,7 +9,7 @@ function createRouter(
   }
 ) {
   return new Router({
-    history: new HTML5History(),
+    history: createHistory(),
     ...options,
   })
 }
@@ -18,8 +18,7 @@ const Home = { template: `<div>Home</div>` }
 const Foo = { template: `<div>Foo</div>` }
 
 const beforeRouteUpdate = jest.fn()
-/** @type {import('../../src/types').RouteRecord[]} */
-const routes = [
+const routes: RouteRecord[] = [
   { path: '/', component: Home },
   { path: '/foo', component: Foo },
   {
index 2a589401c9d5243aa3abb0bfe2c7e4b43635461b..5cd56e5912e86471e21ced793561bb283b159116 100644 (file)
@@ -1,6 +1,5 @@
-import { HTML5History } from '../../src/history/html5'
-import { Router } from '../../src/router'
 import { NAVIGATION_TYPES, createDom } from '../utils'
+import { createHistory, Router } from '../../src'
 
 function createRouter(
   options: Partial<import('../../src/router').RouterOptions> & {
@@ -8,7 +7,7 @@ function createRouter(
   }
 ) {
   return new Router({
-    history: new HTML5History(),
+    history: createHistory(),
     ...options,
   })
 }
index c0e1b32cab3fb50c3aca73abe128fe33c3453ef2..eb35ae3ff79f469d2b222d1dde1ac47ea185858a 100644 (file)
@@ -1,14 +1,14 @@
-import { HTML5History } from '../../src/history/html5'
-import { Router, RouterOptions } from '../../src/router'
+import { RouterOptions } from '../../src/router'
 import fakePromise from 'faked-promise'
 import { NAVIGATION_TYPES, createDom, tick, noGuard } from '../utils'
 import { RouteRecord, RouteLocation } from '../../src/types'
+import { createHistory, Router } from '../../src'
 
 function createRouter(
   options: Partial<RouterOptions> & { routes: RouteRecord[] }
 ) {
   return new Router({
-    history: new HTML5History(),
+    history: createHistory(),
     ...options,
   })
 }
@@ -17,8 +17,7 @@ const Home = { template: `<div>Home</div>` }
 const Foo = { template: `<div>Foo</div>` }
 const Nested = { template: `<div>Nested<router-view/></div>` }
 
-/** @type {RouteRecord[]} */
-const routes = [
+const routes: RouteRecord[] = [
   { path: '/', component: Home },
   { path: '/foo', component: Foo },
   { path: '/other', component: Foo },
index cee5dbf7340d841927fbbb3879db9f19a68d7280..51e908ced1147e4e761235cb522399b962e3702f 100644 (file)
@@ -1,14 +1,14 @@
-import { HTML5History } from '../../src/history/html5'
-import { Router, RouterOptions } from '../../src/router'
+import { RouterOptions, Router } from '../../src/router'
 import fakePromise from 'faked-promise'
 import { NAVIGATION_TYPES, createDom, noGuard, tick } from '../utils'
 import { RouteRecord } from '../../src/types'
+import { createHistory } from '../../src'
 
 function createRouter(
   options: Partial<RouterOptions> & { routes: RouteRecord[] }
 ) {
   return new Router({
-    history: new HTML5History(),
+    history: createHistory(),
     ...options,
   })
 }
@@ -28,8 +28,7 @@ const nested = {
   nestedNestedParam: jest.fn(),
 }
 
-/** @type {RouteRecord[]} */
-const routes = [
+const routes: RouteRecord[] = [
   { path: '/', component: Home },
   { path: '/home', component: Home, beforeEnter },
   { path: '/foo', component: Foo },
index 619861df7e301d8617ecf84c61b9295d763dd20c..60d326a3aa67034a421bec1cf492b97f024cb38a 100644 (file)
@@ -1,8 +1,7 @@
 import fakePromise from 'faked-promise'
-import { AbstractHistory } from '../src/history/abstract'
-import { Router } from '../src/router'
+import { Router, createMemoryHistory } from '../src'
 import { NavigationCancelled } from '../src/errors'
-import { createDom, components, tick, HistoryMock } from './utils'
+import { createDom, components, tick } from './utils'
 import { RouteRecord, RouteLocation } from '../src/types'
 
 const routes: RouteRecord[] = [
@@ -30,7 +29,7 @@ describe('Router', () => {
   })
 
   it('can be instantiated', () => {
-    const history = new HistoryMock()
+    const history = createMemoryHistory()
     const router = new Router({ history, routes })
     expect(router.currentRoute).toEqual({
       name: undefined,
@@ -45,7 +44,8 @@ describe('Router', () => {
 
   // TODO: should do other checks not based on history implem
   it.skip('takes browser location', () => {
-    const history = new HistoryMock('/search?q=dog#footer')
+    const history = createMemoryHistory()
+    history.replace('/search?q=dog#footer')
     const router = new Router({ history, routes })
     expect(router.currentRoute).toEqual({
       fullPath: '/search?q=dog#footer',
@@ -57,7 +57,7 @@ describe('Router', () => {
   })
 
   it('calls history.push with router.push', async () => {
-    const history = new HistoryMock()
+    const history = createMemoryHistory()
     const router = new Router({ history, routes })
     jest.spyOn(history, 'push')
     await router.push('/foo')
@@ -71,7 +71,7 @@ describe('Router', () => {
   })
 
   it('calls history.replace with router.replace', async () => {
-    const history = new HistoryMock()
+    const history = createMemoryHistory()
     const router = new Router({ history, routes })
     jest.spyOn(history, 'replace')
     await router.replace('/foo')
@@ -85,7 +85,7 @@ describe('Router', () => {
   })
 
   it('can pass replace option to push', async () => {
-    const history = new HistoryMock()
+    const history = createMemoryHistory()
     const router = new Router({ history, routes })
     jest.spyOn(history, 'replace')
     await router.push({ path: '/foo', replace: true })
@@ -104,7 +104,7 @@ describe('Router', () => {
     ) {
       const [p1, r1] = fakePromise()
       const [p2, r2] = fakePromise()
-      const history = new HistoryMock()
+      const history = createMemoryHistory()
       const router = new Router({ history, routes })
       router.beforeEach(async (to, from, next) => {
         if (to.name !== 'Param') return next()
@@ -151,7 +151,7 @@ describe('Router', () => {
     ) {
       const [p1, r1] = fakePromise()
       const [p2, r2] = fakePromise()
-      const history = new AbstractHistory()
+      const history = createMemoryHistory()
       const router = new Router({ history, routes })
       // navigate first to add entries to the history stack
       await router.push('/foo')
@@ -203,7 +203,7 @@ describe('Router', () => {
 
   describe('matcher', () => {
     it('handles one redirect from route record', async () => {
-      const history = new HistoryMock()
+      const history = createMemoryHistory()
       const router = new Router({ history, routes })
       const loc = await router.push('/to-foo')
       expect(loc.name).toBe('Foo')
@@ -213,7 +213,7 @@ describe('Router', () => {
     })
 
     it('drops query and params on redirect if not provided', async () => {
-      const history = new HistoryMock()
+      const history = createMemoryHistory()
       const router = new Router({ history, routes })
       const loc = await router.push('/to-foo?hey=foo#fa')
       expect(loc.name).toBe('Foo')
@@ -225,7 +225,7 @@ describe('Router', () => {
     })
 
     it('allows object in redirect', async () => {
-      const history = new HistoryMock()
+      const history = createMemoryHistory()
       const router = new Router({ history, routes })
       const loc = await router.push('/to-foo-named')
       expect(loc.name).toBe('Foo')
@@ -235,7 +235,7 @@ describe('Router', () => {
     })
 
     it('can pass on query and hash when redirecting', async () => {
-      const history = new HistoryMock()
+      const history = createMemoryHistory()
       const router = new Router({ history, routes })
       const loc = await router.push('/inc-query-hash?n=3#fa')
       expect(loc).toMatchObject({
@@ -252,7 +252,7 @@ describe('Router', () => {
     })
 
     it('handles multiple redirect fields in route record', async () => {
-      const history = new HistoryMock()
+      const history = createMemoryHistory()
       const router = new Router({ history, routes })
       const loc = await router.push('/to-foo2')
       expect(loc.name).toBe('Foo')
index b23b16b544b2cbeefc6270a77a821d5f5c19052f..d0d80b83272bad3009be1afac63fe56eafcf4f28 100644 (file)
@@ -1,18 +1,18 @@
 import Vue, { ComponentOptions } from 'vue'
-import Router from '../../src'
+import { Router, createMemoryHistory, plugin } from '../../src'
 import { components } from '../utils'
 
 import { createRenderer } from 'vue-server-renderer'
 import { RouterOptions } from '../../src/router'
 
-Vue.use(Router)
+Vue.use(plugin)
 
 export const renderer = createRenderer()
 
 export function createRouter(options?: Partial<RouterOptions>) {
   // TODO: a more complex routing that can be used for most tests
   return new Router({
-    mode: 'history',
+    history: createMemoryHistory(),
     routes: [
       {
         path: '/',
@@ -57,7 +57,9 @@ export function renderApp(
     const { app, router } = createApp(routerOptions, vueOptions)
 
     // set server-side router's location
-    router.push(context.url).catch(err => {})
+    router.push(context.url).catch(err => {
+      console.error('ssr push failed', err)
+    })
 
     // wait until router has resolved possible async components and hooks
     // TODO: rename the promise one to isReady
index 4556010a61ba0fa0f836e642127eb8af9828a6b1..ca39a2a8d10dc10aa2cf8dbfd08d5141db7507f7 100644 (file)
@@ -1,6 +1,7 @@
 import { Router } from '../src/router'
-import { createDom, components, HistoryMock } from './utils'
+import { createDom, components } from './utils'
 import { RouteRecord } from '../src/types'
+import { createMemoryHistory } from '../src'
 
 const routes: RouteRecord[] = [
   { path: '/', name: 'home', component: components.Home },
@@ -11,8 +12,9 @@ const routes: RouteRecord[] = [
 
 // this function is meant to easy refactor in the future as Histories are going to be
 // function-based
-function createHistory(...args: ConstructorParameters<typeof HistoryMock>) {
-  return new HistoryMock(...args)
+function createHistory() {
+  const routerHistory = createMemoryHistory()
+  return routerHistory
 }
 
 describe('URL Encoding', () => {
@@ -22,9 +24,9 @@ describe('URL Encoding', () => {
 
   describe('initial navigation', () => {
     it('decodes path', async () => {
-      const history = createHistory('/%25')
+      const history = createHistory()
       const router = new Router({ history, routes })
-      await router.doInitialNavigation()
+      await router.replace('/%25')
       expect(router.currentRoute).toEqual(
         expect.objectContaining({
           name: 'percent',
@@ -36,9 +38,9 @@ describe('URL Encoding', () => {
 
     it('decodes params in path', async () => {
       // /p/€
-      const history = createHistory('/p/%E2%82%AC')
+      const history = createHistory()
       const router = new Router({ history, routes })
-      await router.doInitialNavigation()
+      await router.push('/p/%E2%82%AC')
       expect(router.currentRoute).toEqual(
         expect.objectContaining({
           name: 'params',
@@ -50,9 +52,9 @@ describe('URL Encoding', () => {
     })
 
     it('allows navigating to valid unencoded params (IE and Edge)', async () => {
-      const history = createHistory('/p/€')
+      const history = createHistory()
       const router = new Router({ history, routes })
-      await router.doInitialNavigation()
+      await router.push('/p/€')
       expect(router.currentRoute).toEqual(
         expect.objectContaining({
           name: 'params',
@@ -67,9 +69,9 @@ describe('URL Encoding', () => {
 
     it('allows navigating to invalid unencoded params (IE and Edge)', async () => {
       const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
-      const history = createHistory('/p/%notvalid')
+      const history = createHistory()
       const router = new Router({ history, routes })
-      await router.doInitialNavigation()
+      await router.push('/p/%notvalid')
       expect(spy).toHaveBeenCalledTimes(1)
       spy.mockRestore()
       expect(router.currentRoute).toEqual(
@@ -85,9 +87,9 @@ describe('URL Encoding', () => {
     })
 
     it('decodes params in query', async () => {
-      const history = createHistory('/?q=%25%E2%82%AC')
+      const history = createHistory()
       const router = new Router({ history, routes })
-      await router.doInitialNavigation()
+      await router.push('/?q=%25%E2%82%AC')
       expect(router.currentRoute).toEqual(
         expect.objectContaining({
           name: 'home',
@@ -101,9 +103,9 @@ describe('URL Encoding', () => {
     })
 
     it('decodes params keys in query', async () => {
-      const history = createHistory('/?%E2%82%AC=euro')
+      const history = createHistory()
       const router = new Router({ history, routes })
-      await router.doInitialNavigation()
+      await router.push('/?%E2%82%AC=euro')
       expect(router.currentRoute).toEqual(
         expect.objectContaining({
           name: 'home',
@@ -118,9 +120,9 @@ describe('URL Encoding', () => {
 
     it('allow unencoded params in query (IE Edge)', async () => {
       const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
-      const history = createHistory('/?q=€%notvalid')
+      const history = createHistory()
       const router = new Router({ history, routes })
-      await router.doInitialNavigation()
+      await router.push('/?q=€%notvalid')
       expect(spy).toHaveBeenCalledTimes(1)
       spy.mockRestore()
       expect(router.currentRoute).toEqual(
@@ -139,9 +141,9 @@ describe('URL Encoding', () => {
     // should we do it? it seems to be a bit different as it allows using % without
     // encoding it. To be safe we would have to encode everything
     it.skip('decodes hash', async () => {
-      const history = createHistory('/#%25%E2%82%AC')
+      const history = createHistory()
       const router = new Router({ history, routes })
-      await router.doInitialNavigation()
+      await router.push('/#%25%E2%82%AC')
       expect(router.currentRoute).toEqual(
         expect.objectContaining({
           name: 'home',
@@ -154,9 +156,9 @@ describe('URL Encoding', () => {
 
     it('allow unencoded params in query (IE Edge)', async () => {
       const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
-      const history = createHistory('/?q=€%notvalid')
+      const history = createHistory()
       const router = new Router({ history, routes })
-      await router.doInitialNavigation()
+      await router.push('/?q=€%notvalid')
       expect(spy).toHaveBeenCalledTimes(1)
       spy.mockRestore()
       expect(router.currentRoute).toEqual(
@@ -174,9 +176,8 @@ describe('URL Encoding', () => {
 
   describe('resolving locations', () => {
     it('encodes params when resolving', async () => {
-      const history = createHistory('/')
+      const history = createHistory()
       const router = new Router({ history, routes })
-      await router.doInitialNavigation()
       await router.push({ name: 'params', params: { p: '%€' } })
       expect(router.currentRoute).toEqual(
         expect.objectContaining({