Skip to content

provided HashRouter with own createHref implementation #4024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions modules/HashRouter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import React, { PropTypes } from 'react'
import HashHistory from 'react-history/HashHistory'
import { addLeadingSlash, stripLeadingSlash } from 'history/PathUtils'
import StaticRouter from './StaticRouter'

const createHref = hashType => path => {
let newPath

switch (hashType) {
case 'hashbang':
newPath = path.charAt(0) === '!' ? path : '!/' + stripLeadingSlash(path)
break
case 'noslash':
newPath = stripLeadingSlash(path)
break
case 'slash':
default:
newPath = addLeadingSlash(path)
break
}

return `#${newPath}`
}

/**
* A router that uses the URL hash.
*/
Expand All @@ -19,6 +39,7 @@ const HashRouter = ({ basename, getUserConfirmation, hashType, ...props }) => (
onPush={history.push}
onReplace={history.replace}
blockTransitions={history.block}
createHref={createHref(hashType)}
{...props}
/>
)}
Expand Down
54 changes: 54 additions & 0 deletions modules/__tests__/HashRouter-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import expect from 'expect'
import { render } from 'react-dom'
import React from 'react'
import HashRouter from '../HashRouter'
import Link from '../Link'

describe('HashRouter', () => {
const linkCreator = (hashType, to) => {
const div = document.createElement('div')
render((
<HashRouter hashType={hashType}>
<Link to={to} />
</HashRouter>
), div)
const a = div.querySelector('a')
return a
}

describe('hashType slash', () => {
it('adds a hash link', () => {
const link = linkCreator('slash', '/foo')
expect(link.getAttribute('href')).toEqual('#/foo')
})

it('adds a hash link with a leading slash if it is missing', () => {
const link = linkCreator('slash', 'foo')
expect(link.getAttribute('href')).toEqual('#/foo')
})
})

describe('hashType hashbang', () => {
it('adds a hashbang to the link', () => {
const link = linkCreator('hashbang', '/foo')
expect(link.getAttribute('href')).toEqual('#!/foo')
})

it('adds a hashbang to the link with a leading slash if it is missing', () => {
const link = linkCreator('hashbang', 'foo')
expect(link.getAttribute('href')).toEqual('#!/foo')
})
})

describe('hashType noslash', () => {
it('adds a hash link', () => {
const link = linkCreator('noslash', 'foo')
expect(link.getAttribute('href')).toEqual('#foo')
})

it('adds a hash link and removes the leading slash', () => {
const link = linkCreator('noslash', '/foo')
expect(link.getAttribute('href')).toEqual('#foo')
})
})
})