From ca8ae5071725515f7aa2c7d4ce24150adbc31ed0 Mon Sep 17 00:00:00 2001 From: Sylvain Brocard Date: Mon, 25 Mar 2019 17:20:09 +0100 Subject: [PATCH] IE compatibility and add unit tests --- src/core/router/history/base.js | 6 ++-- test/unit/base.js | 62 +++++++++++++++++++++++++++++++++ test/unit/util.js | 30 ++++++++++++++++ 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 test/unit/base.js create mode 100644 test/unit/util.js diff --git a/src/core/router/history/base.js b/src/core/router/history/base.js index c58a1f9d9..7ea763d8d 100644 --- a/src/core/router/history/base.js +++ b/src/core/router/history/base.js @@ -74,11 +74,11 @@ export class History { if (local) { const idIndex = currentRoute.indexOf('?') path = - (idIndex > 0 ? currentRoute.substr(0, idIndex) : currentRoute) + path + (idIndex > 0 ? currentRoute.substring(0, idIndex) : currentRoute) + path } - if (this.config.relativePath && !path.startsWith('/')) { - const currentDir = currentRoute.substr(0, currentRoute.lastIndexOf('/') + 1) + if (this.config.relativePath && path.indexOf('/') !== 0) { + const currentDir = currentRoute.substring(0, currentRoute.lastIndexOf('/') + 1) return cleanPath(resolvePath(currentDir + path)) } return cleanPath('/' + path) diff --git a/test/unit/base.js b/test/unit/base.js new file mode 100644 index 000000000..036700b99 --- /dev/null +++ b/test/unit/base.js @@ -0,0 +1,62 @@ +/* eslint-env node, chai, mocha */ +require = require('esm')(module/*, options*/) +const {expect} = require('chai') +const {History} = require('../../src/core/router/history/base') + +class MockHistory extends History { + parse(path) { + return {path} + } +} + +describe('router/history/base', function () { + describe('relativePath true', function () { + var history + + beforeEach(function () { + history = new MockHistory({relativePath: true}) + }) + + it('toURL', function () { + // WHEN + const url = history.toURL('guide.md', {}, '/zh-ch/') + + // THEN + expect(url).equal('/zh-ch/guide') + }) + + it('toURL with double dot', function () { + // WHEN + const url = history.toURL('../README.md', {}, '/zh-ch/') + + // THEN + expect(url).equal('/README') + }) + + it('toURL child path', function () { + // WHEN + const url = history.toURL('config/example.md', {}, '/zh-ch/') + + // THEN + expect(url).equal('/zh-ch/config/example') + }) + + it('toURL absolute path', function () { + // WHEN + const url = history.toURL('/README', {}, '/zh-ch/') + + // THEN + expect(url).equal('/README') + }) + }) + + it('toURL without relative path', function () { + const history = new MockHistory({relativePath: false}) + + // WHEN + const url = history.toURL('README', {}, '/zh-ch/') + + // THEN + expect(url).equal('/README') + }) +}) diff --git a/test/unit/util.js b/test/unit/util.js new file mode 100644 index 000000000..1e65dafe0 --- /dev/null +++ b/test/unit/util.js @@ -0,0 +1,30 @@ +/* eslint-env node, chai, mocha */ +require = require('esm')(module/*, options*/) +const {expect} = require('chai') +const {resolvePath} = require('../../src/core/router/util') + +describe('router/util', function () { + it('resolvePath', async function () { + // WHEN + const result = resolvePath('hello.md') + + // THEN + expect(result).equal('/hello.md') + }) + + it('resolvePath with dot', async function () { + // WHEN + const result = resolvePath('./hello.md') + + // THEN + expect(result).equal('/hello.md') + }) + + it('resolvePath with two dots', async function () { + // WHEN + const result = resolvePath('test/../hello.md') + + // THEN + expect(result).equal('/hello.md') + }) +})