-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(history): allow hash history with no origin
Close #163
- Loading branch information
Showing
5 changed files
with
124 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { JSDOM } from 'jsdom' | ||
import createWebHashHistory from '../../src/history/hash' | ||
import createWebHistory from '../../src/history/html5' | ||
import { createDom } from '../utils' | ||
|
||
jest.mock('../../src/history/html5') | ||
|
||
describe('History Hash', () => { | ||
let dom: JSDOM | ||
beforeAll(() => { | ||
dom = createDom() | ||
}) | ||
|
||
beforeEach(() => { | ||
;(createWebHistory as jest.Mock).mockClear() | ||
}) | ||
|
||
afterAll(() => { | ||
dom.window.close() | ||
}) | ||
|
||
afterEach(() => { | ||
// ensure no base element is left after a test as only the first is | ||
// respected | ||
for (let element of Array.from(document.getElementsByTagName('base'))) | ||
element.remove() | ||
}) | ||
|
||
describe('url', () => { | ||
beforeEach(() => { | ||
dom.reconfigure({ url: 'https://example.com' }) | ||
}) | ||
|
||
it('should use a correct', () => { | ||
createWebHashHistory() | ||
expect(createWebHistory).toHaveBeenCalledWith('/#') | ||
}) | ||
|
||
it('should be able to provide a base', () => { | ||
createWebHashHistory('/folder/') | ||
expect(createWebHistory).toHaveBeenCalledWith('/folder/#') | ||
}) | ||
|
||
it('should be able to provide a base with no trailing slash', () => { | ||
createWebHashHistory('/folder') | ||
expect(createWebHistory).toHaveBeenCalledWith('/folder/#') | ||
}) | ||
|
||
it('should read the base tag', () => { | ||
const baseEl = document.createElement('base') | ||
baseEl.href = '/foo/' | ||
document.head.appendChild(baseEl) | ||
createWebHashHistory() | ||
expect(createWebHistory).toHaveBeenCalledWith('/foo/#') | ||
}) | ||
|
||
it('should use the base option over the base tag', () => { | ||
const baseEl = document.createElement('base') | ||
baseEl.href = '/foo/' | ||
document.head.appendChild(baseEl) | ||
createWebHashHistory('/bar/') | ||
expect(createWebHistory).toHaveBeenCalledWith('/bar/#') | ||
}) | ||
}) | ||
|
||
describe('file://', () => { | ||
beforeEach(() => { | ||
dom.reconfigure({ url: 'file:///usr/some-file.html' }) | ||
}) | ||
|
||
it('should use a correct base', () => { | ||
createWebHashHistory() | ||
// both, a trailing / and none work | ||
expect(createWebHistory).toHaveBeenCalledWith( | ||
expect.stringMatching(/^#\/?$/) | ||
) | ||
}) | ||
|
||
it.todo('warns if we provide a base with file://') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { RouterHistory } from './common' | ||
import { RouterHistory, normalizeBase } from './common' | ||
import createWebHistory from './html5' | ||
|
||
export default function createWebHashHistory(base: string = ''): RouterHistory { | ||
export default function createWebHashHistory(base?: string): RouterHistory { | ||
// Make sure this implementation is fine in terms of encoding, specially for IE11 | ||
return createWebHistory(base + '/#') | ||
return createWebHistory(location.host ? normalizeBase(base) + '/#' : '#') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters