From: Eduardo San Martin Morote Date: Fri, 28 Jun 2019 09:26:20 +0000 (+0200) Subject: test: move history tests to own folder X-Git-Tag: v4.0.0-alpha.0~336 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=092bbc5153a4bceab6e4f1b6a2a40f5ffa91e88b;p=thirdparty%2Fvuejs%2Frouter.git test: move history tests to own folder --- diff --git a/__tests__/abstract.spec.js b/__tests__/history/abstract.spec.js similarity index 96% rename from __tests__/abstract.spec.js rename to __tests__/history/abstract.spec.js index 6e38c12f..b32fb881 100644 --- a/__tests__/abstract.spec.js +++ b/__tests__/history/abstract.spec.js @@ -1,11 +1,11 @@ // @ts-check -require('./helper') +require('../helper') const expect = require('expect') -const { AbstractHistory } = require('../src/history/abstract') -const { START } = require('../src/history/base') +const { AbstractHistory } = require('../../src/history/abstract') +const { START } = require('../../src/history/base') -/** @type {import('../src/history/base').HistoryLocation} */ +/** @type {import('../../src/history/base').HistoryLocation} */ const loc = { path: '/foo', } diff --git a/__tests__/html5.spec.js b/__tests__/history/html5.spec.js similarity index 57% rename from __tests__/html5.spec.js rename to __tests__/history/html5.spec.js index 7a714011..ccc0b4d6 100644 --- a/__tests__/html5.spec.js +++ b/__tests__/history/html5.spec.js @@ -1,9 +1,11 @@ // @ts-check -require('./helper') +require('../helper') const expect = require('expect') -const { HTML5History } = require('../src/history/html5') -const { createDom } = require('./utils') +const { HTML5History } = require('../../src/history/html5') +const { createDom } = require('../utils') +// TODO: is it really worth testing this implementation on jest or is it +// better to directly use e2e tests instead describe.skip('History HTMl5', () => { beforeAll(() => { createDom() diff --git a/__tests__/router.spec.js b/__tests__/router.spec.js index 02832445..a748511b 100644 --- a/__tests__/router.spec.js +++ b/__tests__/router.spec.js @@ -8,7 +8,13 @@ const { Router } = require('../src/router') const { NavigationCancelled } = require('../src/errors') const { createDom, components, tick } = require('./utils') -function mockHistory() { +/** + * Created a mocked version of the history + * @param {string} [start] starting locationh + */ +function mockHistory(start) { + // @ts-ignore + if (start) window.location = start // TODO: actually do a mock return new HTML5History() } @@ -16,6 +22,7 @@ function mockHistory() { /** @type {import('../src/types').RouteRecord[]} */ const routes = [ { path: '/', component: components.Home }, + { path: '/search', component: components.Home }, { path: '/foo', component: components.Foo, name: 'Foo' }, { path: '/to-foo', redirect: '/foo' }, { path: '/to-foo-named', redirect: { name: 'Foo' } }, @@ -49,6 +56,19 @@ describe('Router', () => { }) }) + // TODO: should do other checks not based on history implem + it.skip('takes browser location', () => { + const history = mockHistory('/search?q=dog#footer') + const router = new Router({ history, routes }) + expect(router.currentRoute).toEqual({ + fullPath: '/search?q=dog#footer', + hash: '#footer', + params: {}, + path: '/search', + query: { q: 'dog' }, + }) + }) + it('calls history.push with router.push', async () => { const history = mockHistory() const router = new Router({ history, routes }) @@ -77,6 +97,20 @@ describe('Router', () => { }) }) + it('can pass replace option to push', async () => { + const history = mockHistory() + const router = new Router({ history, routes }) + jest.spyOn(history, 'replace') + await router.push({ path: '/foo', replace: true }) + expect(history.replace).toHaveBeenCalledTimes(1) + expect(history.replace).toHaveBeenCalledWith({ + fullPath: '/foo', + path: '/foo', + query: {}, + hash: '', + }) + }) + describe('navigation', () => { async function checkNavigationCancelledOnPush(target) { const [p1, r1] = fakePromise() @@ -170,7 +204,7 @@ describe('Router', () => { }) it('cancels navigation abort if a newer one is finished on user navigation (from history)', async () => { - await checkNavigationCancelledOnPush(undefined, '/p/b') + await checkNavigationCancelledOnPush(undefined) }) })