Skip to content

Commit 8a3ea17

Browse files
committed
Merge pull request #2224 from timdorr/test-link-hash
Fix the hash prop on Links
2 parents 5e0c5e9 + 8459755 commit 8a3ea17

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

modules/Link.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,14 @@ class Link extends Component {
8181

8282
event.preventDefault()
8383

84-
if (allowTransition)
85-
this.context.history.pushState(this.props.state, this.props.to, this.props.query)
84+
if (allowTransition) {
85+
let { state, to, query, hash } = this.props
86+
87+
if (hash)
88+
to += hash
89+
90+
this.context.history.pushState(state, to, query)
91+
}
8692
}
8793

8894
render() {

modules/__tests__/Link-test.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/*eslint-env mocha */
22
/*eslint react/prop-types: 0*/
3-
import expect from 'expect'
3+
import expect, { spyOn } from 'expect'
44
import React, { Component } from 'react'
5-
import ReactTestUtils from 'react-addons-test-utils'
5+
import { Simulate } from 'react-addons-test-utils'
66
import { render } from 'react-dom'
77
import createHistory from 'history/lib/createMemoryHistory'
88
import execSteps from './execSteps'
99
import Router from '../Router'
1010
import Route from '../Route'
1111
import Link from '../Link'
1212

13-
const { click } = ReactTestUtils.Simulate
13+
const { click } = Simulate
1414

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

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

296+
const history = createHistory('/')
297+
const spy = spyOn(history, 'pushState').andCallThrough()
298+
296299
const steps = [
297300
function () {
298301
click(node.querySelector('a'), { button: 0 })
299302
},
300303
function () {
301304
expect(node.innerHTML).toMatch(/Hello/)
305+
expect(spy).toHaveBeenCalledWith(undefined, '/hello#world')
302306
}
303307
]
304308

305309
const execNextStep = execSteps(steps, done)
306310

307311
render((
308-
<Router history={createHistory('/')} onUpdate={execNextStep}>
312+
<Router history={history} onUpdate={execNextStep}>
309313
<Route path="/" component={LinkWrapper} />
310314
<Route path="/hello" component={Hello} />
311315
</Router>

0 commit comments

Comments
 (0)