Skip to content

Commit

Permalink
IE compatibility and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain Brocard committed Mar 25, 2019
1 parent 31654f1 commit ca8ae50
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/core/router/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
62 changes: 62 additions & 0 deletions test/unit/base.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
30 changes: 30 additions & 0 deletions test/unit/util.js
Original file line number Diff line number Diff line change
@@ -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')
})
})

0 comments on commit ca8ae50

Please sign in to comment.