From d05208b6c9457931bda8205ba6d9f1d5e39a54c7 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 19 Mar 2020 17:41:06 +0100 Subject: [PATCH] fix: initial location with base --- __tests__/history/locationUtils.spec.ts | 22 ++++++++++++++++++++++ e2e/specs/encoding.js | 12 ++++-------- src/history/common.ts | 6 ++---- 3 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 __tests__/history/locationUtils.spec.ts diff --git a/__tests__/history/locationUtils.spec.ts b/__tests__/history/locationUtils.spec.ts new file mode 100644 index 00000000..35bf0b1b --- /dev/null +++ b/__tests__/history/locationUtils.spec.ts @@ -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') + }) + }) +}) diff --git a/e2e/specs/encoding.js b/e2e/specs/encoding.js index 305f5015..c11bef6a 100644 --- a/e2e/specs/encoding.js +++ b/e2e/specs/encoding.js @@ -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') @@ -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() }, } diff --git a/src/history/common.ts b/src/history/common.ts index f8eab7f0..41efecb1 100644 --- a/src/history/common.ts +++ b/src/history/common.ts @@ -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( -- 2.39.5