Skip to content

Commit

Permalink
Merge pull request #2224 from timdorr/test-link-hash
Browse files Browse the repository at this point in the history
Fix the hash prop on Links
  • Loading branch information
timdorr committed Oct 9, 2015
2 parents 5e0c5e9 + 8459755 commit 8a3ea17
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 8 additions & 2 deletions modules/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,14 @@ class Link extends Component {

event.preventDefault()

if (allowTransition)
this.context.history.pushState(this.props.state, this.props.to, this.props.query)
if (allowTransition) {
let { state, to, query, hash } = this.props

if (hash)
to += hash

this.context.history.pushState(state, to, query)
}
}

render() {
Expand Down
14 changes: 9 additions & 5 deletions modules/__tests__/Link-test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*eslint-env mocha */
/*eslint react/prop-types: 0*/
import expect from 'expect'
import expect, { spyOn } from 'expect'
import React, { Component } from 'react'
import ReactTestUtils from 'react-addons-test-utils'
import { Simulate } from 'react-addons-test-utils'
import { render } from 'react-dom'
import createHistory from 'history/lib/createMemoryHistory'
import execSteps from './execSteps'
import Router from '../Router'
import Route from '../Route'
import Link from '../Link'

const { click } = ReactTestUtils.Simulate
const { click } = Simulate

describe('A <Link>', function () {

Expand Down Expand Up @@ -289,23 +289,27 @@ describe('A <Link>', function () {
// just here to make sure click handlers don't prevent it from happening
}
render() {
return <Link to="/hello" onClick={(e) => this.handleClick(e)}>Link</Link>
return <Link to="/hello" hash="#world" onClick={(e) => this.handleClick(e)}>Link</Link>
}
}

const history = createHistory('/')
const spy = spyOn(history, 'pushState').andCallThrough()

const steps = [
function () {
click(node.querySelector('a'), { button: 0 })
},
function () {
expect(node.innerHTML).toMatch(/Hello/)
expect(spy).toHaveBeenCalledWith(undefined, '/hello#world')
}
]

const execNextStep = execSteps(steps, done)

render((
<Router history={createHistory('/')} onUpdate={execNextStep}>
<Router history={history} onUpdate={execNextStep}>
<Route path="/" component={LinkWrapper} />
<Route path="/hello" component={Hello} />
</Router>
Expand Down

0 comments on commit 8a3ea17

Please sign in to comment.