Skip to content

Commit

Permalink
fix: initial location with base
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Mar 19, 2020
1 parent 447053b commit d05208b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
22 changes: 22 additions & 0 deletions __tests__/history/locationUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { stripBase } from '../../src/history/common'
import { createDom } from '../utils'

describe('History Location Utils', () => {
beforeAll(() => {
createDom()
})

describe('stripBase', () => {
it('returns the pathname if no base', () => {
expect(stripBase('', '')).toBe('')
expect(stripBase('/', '')).toBe('/')
expect(stripBase('/thing', '')).toBe('/thing')
})

it('returns the pathname without the base', () => {
expect(stripBase('/base', '/base')).toBe('/')
expect(stripBase('/base/', '/base')).toBe('/')
expect(stripBase('/base/foo', '/base')).toBe('/foo')
})
})
})
12 changes: 4 additions & 8 deletions e2e/specs/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module.exports = {
basic(browser) {
browser
.url(baseURL)
// TODO: move this test to a different spec
.assert.urlEquals(baseURL + '/')
.waitForElementVisible('#app', 1000)

.click('li:nth-child(3) a')
Expand All @@ -20,18 +22,12 @@ module.exports = {
.assert.containsText('#params', JSON.stringify({ id: '€uro' }, null, 2))

// check initial visit
.url(baseURL + '/encoding/documents/%E2%82%ACuro')
.url(baseURL + '/documents/%E2%82%ACuro')
.waitForElementVisible('#app', 1000)
.assert.containsText('#fullPath', '/documents/%E2%82%ACuro')
.assert.containsText('#path', '/documents/%E2%82%ACuro')
// .assert.containsText('#params', JSON.stringify({ id: '€uro' }, null, 2))
// .assert.containsText('#params', JSON.stringify({ id: '€uro' }, null, 2))
.assert.containsText('#params', JSON.stringify({ id: '€uro' }, null, 2))

browser
.getText('#params', function(res) {
this.assert.equal(res.value, JSON.stringify({ id: '€uro' }, null, 2))
console.log(res.state)
})
.end()
},
}
6 changes: 2 additions & 4 deletions src/history/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,8 @@ export function stringifyURL(
* @param base base to strip off
*/
export function stripBase(pathname: string, base: string): string {
return (
(base && pathname.indexOf(base) === 0 && pathname.replace(base, '')) ||
pathname
)
if (!base || pathname.indexOf(base) !== 0) return pathname
return pathname.replace(base, '') || '/'
}

export function normalizeHistoryLocation(
Expand Down

0 comments on commit d05208b

Please sign in to comment.