From 20c8bed3cfc80fa8bfb7618c1dde81a527db79a9 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 3 Apr 2020 13:52:49 +0200 Subject: [PATCH] refactor: reorganize unit tests --- __tests__/router.spec.ts | 42 +++++++++++++++++----------------- __tests__/url-encoding.spec.ts | 16 ++----------- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/__tests__/router.spec.ts b/__tests__/router.spec.ts index f32cbc4e..ab5bc145 100644 --- a/__tests__/router.spec.ts +++ b/__tests__/router.spec.ts @@ -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) ) { diff --git a/__tests__/url-encoding.spec.ts b/__tests__/url-encoding.spec.ts index 0fe20e44..085a6989 100644 --- a/__tests__/url-encoding.spec.ts +++ b/__tests__/url-encoding.spec.ts @@ -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) { -- 2.39.5