]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: reorganize unit tests
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 3 Apr 2020 11:52:49 +0000 (13:52 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 3 Apr 2020 11:52:49 +0000 (13:52 +0200)
__tests__/router.spec.ts
__tests__/url-encoding.spec.ts

index f32cbc4e9f474032afde84259605f10cc6f247fb..ab5bc14585950d8cda682304c2d9f5ecff50e0ad 100644 (file)
@@ -71,7 +71,7 @@ describe('Router', () => {
     createDom()
   })
 
-  it('can be instantiated', () => {
+  it('starts at START_LOCATION', () => {
     const history = createMemoryHistory()
     const router = createRouter({ history, routes })
     expect(router.currentRoute.value).toEqual(START_LOCATION_NORMALIZED)
@@ -93,6 +93,23 @@ describe('Router', () => {
     )
   })
 
+  it('calls history.replace with router.replace', async () => {
+    const history = createMemoryHistory()
+    const { router } = await newRouter({ history })
+    jest.spyOn(history, 'replace')
+    await router.replace('/foo')
+    expect(history.replace).toHaveBeenCalledTimes(1)
+    expect(history.replace).toHaveBeenCalledWith(
+      expect.objectContaining({
+        fullPath: '/foo',
+        path: '/foo',
+        query: {},
+        hash: '',
+      }),
+      undefined
+    )
+  })
+
   it('allows to customize parseQuery', async () => {
     const parseQuery = jest.fn()
     const { router } = await newRouter({ parseQuery })
@@ -117,23 +134,6 @@ describe('Router', () => {
     expect(router.currentRoute.value).not.toBe(START_LOCATION_NORMALIZED)
   })
 
-  it('calls history.replace with router.replace', async () => {
-    const history = createMemoryHistory()
-    const { router } = await newRouter({ history })
-    jest.spyOn(history, 'replace')
-    await router.replace('/foo')
-    expect(history.replace).toHaveBeenCalledTimes(1)
-    expect(history.replace).toHaveBeenCalledWith(
-      expect.objectContaining({
-        fullPath: '/foo',
-        path: '/foo',
-        query: {},
-        hash: '',
-      }),
-      undefined
-    )
-  })
-
   it('can pass replace option to push', async () => {
     const { router, history } = await newRouter()
     jest.spyOn(history, 'replace')
@@ -187,12 +187,12 @@ describe('Router', () => {
     const spy = jest.fn((to, from, next) => next())
     router.beforeEach(spy)
     await router.push('/idontexist')
-    expect(spy).toHaveBeenCalled()
+    expect(spy).toHaveBeenCalledTimes(1)
     expect(router.currentRoute.value).toMatchObject({ matched: [] })
     spy.mockClear()
     await router.push('/me-neither')
     expect(router.currentRoute.value).toMatchObject({ matched: [] })
-    expect(spy).toHaveBeenCalled()
+    expect(spy).toHaveBeenCalledTimes(1)
   })
 
   it('navigates to same route record but different query', async () => {
@@ -246,7 +246,7 @@ describe('Router', () => {
     })
   })
 
-  describe('navigation', () => {
+  describe('navigation cancelled', () => {
     async function checkNavigationCancelledOnPush(
       target?: RouteLocationRaw | false | ((vm: any) => void)
     ) {
index 0fe20e44b5c2708bd863263de751647b73e96b2a..085a6989d43391aefdc4736b03eec3d3d6d0a854 100644 (file)
@@ -1,5 +1,5 @@
 import { createRouter as newRouter } from '../src/router'
-import { createDom, components } from './utils'
+import { components } from './utils'
 import { RouteRecordRaw } from '../src/types'
 import { createMemoryHistory } from '../src'
 import * as encoding from '../src/utils/encoding'
@@ -14,25 +14,13 @@ const routes: RouteRecordRaw[] = [
   { path: '/p/:p+', component: components.Bar, name: 'repeat' },
 ]
 
-// this function is meant to easy refactor in the future as Histories are going to be
-// function-based
-function createWebHistory() {
-  const routerHistory = createMemoryHistory()
-  return routerHistory
-}
-
 function createRouter() {
-  const history = createWebHistory()
+  const history = createMemoryHistory()
   const router = newRouter({ history, routes })
   return router
 }
 
-// TODO: test by spying on encode functions since things are already tested by encoding.spec.ts
 describe('URL Encoding', () => {
-  beforeAll(() => {
-    createDom()
-  })
-
   beforeEach(() => {
     // mock all encoding functions
     for (const key in encoding) {