Skip to content
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

Add passing custom histories to match #2813

Merged
merged 1 commit into from
Jan 1, 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
18 changes: 17 additions & 1 deletion modules/__tests__/serverRendering-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import expect from 'expect'
import expect, { spyOn } from 'expect'
import React, { Component } from 'react'
import { renderToString } from 'react-dom/server'
import match from '../match'
import createMemoryHistory from '../createMemoryHistory'
import RouterContext from '../RouterContext'
import Link from '../Link'

Expand Down Expand Up @@ -75,6 +76,21 @@ describe('server rendering', function () {
})
})

it('accepts a custom history', function (done) {
const history = createMemoryHistory()
const spy = spyOn(history, 'createLocation').andCallThrough()

match({ history, routes, location: '/dashboard' }, function (error, redirectLocation, renderProps) {
const string = renderToString(
<RouterContext {...renderProps} />
)
expect(string).toMatch(/The Dashboard/)
expect(spy).toHaveBeenCalled()
done()
})
})


it('renders active Links as active', function (done) {
match({ routes, location: '/about' }, function (error, redirectLocation, renderProps) {
const string = renderToString(
Expand Down
4 changes: 2 additions & 2 deletions modules/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import { createRouterObject, createRoutingHistory } from './RouterUtils'
* Note: You probably don't want to use this in a browser. Use
* the history.listen API instead.
*/
function match({ routes, location, ...options }, callback) {
function match({ history, routes, location, ...options }, callback) {
invariant(
location,
'match needs a location'
)

let history = createMemoryHistory(options)
history = history ? history : createMemoryHistory(options)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My brain is slow. Certainly there's a better way to do this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't do this in the assignment because you need options. Just history || createMemoryHistory(options) is the best I can think of.

const transitionManager = createTransitionManager(
history,
createRoutes(routes)
Expand Down