Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
small update to make testing apps that use the router easier
Browse files Browse the repository at this point in the history
  • Loading branch information
cellog committed Apr 13, 2017
1 parent fea5e2d commit cbb2735
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,44 @@ Everything is properly isolated, and testable. You can easily unit test your ro
stateFromParams and paramsFromState and updateState properties. Components are
simply components, no magic.
To set up routes for testing in a unit test, the `synchronousMakeRoutes` functions is
available. Pass in an array of routes, and use the return in the reducer
```javascript
import { synchronousMakeRoutes, routerReducer } from 'react-redux-saga-router'

describe('some component that uses routes', () => {
let fakeState
beforeEach(() => {
const action = synchronousMakeRoutes([
{
name: 'route1',
path: '/route1'
},
{
name: 'route1',
path: '/route2/:thing',
stateToParams: state => state,
paramsToState: params => params,
update: {
thing: thing => ({ type: 'changething', payload: thing })
}
}
])
fakeState = {
routing: routerReducer(undefined, action)
}
})
it('test something', () => {
// use components that have <Link> or <Toggle>
})
})
```
You will need to set this up for any `<Link>` components that use route to generate
the path, and any components that contain `<Router>` or `<Route>` tags when rendering
them.
## License
MIT License
Expand Down
1 change: 1 addition & 0 deletions enhancers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/enhancers.js')
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-redux-saga-router",
"version": "0.9.0",
"version": "0.9.1",
"description": "elegant powerful routing based on the simplicity of storing url as state",
"main": "lib/index.js",
"directories": {
Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import historyChannel from './historyChannel'
import routerReducer from './reducer'
import * as actions from './actions'
import * as sagas from './sagas'
import * as enhancers from './enhancers'
import { connectLink } from './Link'
import { connectRoutes } from './Routes'
import { connectToggle } from './Toggle'
Expand Down Expand Up @@ -57,6 +58,14 @@ export const setEnhancedRoutes = (r) => {
options.enhancedRoutes = r
}

// for unit-testing purposes
export function synchronousMakeRoutes(routes) {
const action = actions.batchRoutes(routes)
setEnhancedRoutes(Object.keys(action.payload.routes).reduce((en, route) =>
enhancers.save(action.payload.routes[route], en), options.enhancedRoutes))
return action
}

export function *router(connect, routeDefinitions, history, channel, isServer) {
options.server = !!isServer
yield [
Expand Down
49 changes: 49 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,53 @@ describe('react-redux-saga-router', () => {
expect(middleware.run.args[0]).eqls([index.router, connect, routes, a, b, true])
index.default(middleware, connect, routes) // for coverage
})
it('synchronousMakeRoutes', () => {
const routes = [{
name: 'campers',
path: '/campers/:year(/:id)',
paramsFromState: state => ({
id: state.campers.selectedCamper ? state.campers.selectedCamper : undefined,
year: state.currentYear + '' // eslint-disable-line
}),
stateFromParams: params => ({
id: params.id ? params.id : false,
year: +params.year
}),
updateState: {
id: id => ({ type: 'select', payload: id }),
year: year => ({ type: 'year', payload: year })
}
}, {
name: 'ensembles',
path: '/ensembles(/:id)',
paramsFromState: state => ({
id: state.ensembleTypes.selectedEnsembleType ?
state.ensembleTypes.selectedEnsembleType : undefined,
}),
stateFromParams: params => ({
id: params.id ? params.id : false,
}),
updateState: {
id: id => ({ type: 'ensemble', payload: id }),
}
}, {
name: 'foo',
path: '/my/:fancy/path(/:wow/*supercomplicated(/:thing))',
}]
expect(index.synchronousMakeRoutes(routes)).eqls(actions.batchRoutes(routes))
expect(index.options.enhancedRoutes).eqls({
campers: {
...enhancers.enhanceRoute(routes[0]),
parent: undefined,
},
ensembles: {
...enhancers.enhanceRoute(routes[1]),
parent: undefined,
},
foo: {
...enhancers.enhanceRoute(routes[2]),
parent: undefined,
}
})
})
})

0 comments on commit cbb2735

Please sign in to comment.