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

Throw helpful error when router reducer not mounted under "router" #175

Merged
merged 4 commits into from Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/selectors.js
@@ -1,9 +1,15 @@
import { matchPath } from "react-router"
import isObject from 'lodash/isObject'
Copy link
Owner

Choose a reason for hiding this comment

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

isObject is quite simple, so, we can define isObject here and don't need to maintain external dependency.

const isObject = (value) => (value != null && (typeof value === 'object' || typeof value === 'function'))


const createSelectors = (structure) => {
const { getIn, toJS } = structure
const getLocation = state => toJS(getIn(state, ['router', 'location']))
const getAction = state => toJS(getIn(state, ['router', 'action']))
const { get, toJS } = structure
const getRouter = state => {
mattvague marked this conversation as resolved.
Show resolved Hide resolved
const router = toJS(get(state, 'router'))
Copy link
Owner

Choose a reason for hiding this comment

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

Can we just use getIn(state, 'router') here? So, we don't need to create addition get.

if (!isObject(router)) { throw 'Could not find router reducer in state tree. Are you sure it is mounted under "router"?' }
return router
}
const getLocation = state => toJS(get(getRouter(state), 'location'))
const getAction = state => toJS(get(getRouter(state), 'action'))

// It only makes sense to recalculate the `matchPath` whenever the pathname
// of the location changes. That's why `createMatchSelector` memoizes
Expand Down
9 changes: 9 additions & 0 deletions src/structure/immutable/get.js
@@ -0,0 +1,9 @@
import { Iterable } from 'immutable'
import plainGet from '../plain/get'

const getIn = (state, key) =>
Iterable.isIterable(state)
? state.get(key)
: plainGet(state, key)

export default getIn
2 changes: 2 additions & 0 deletions src/structure/immutable/index.js
@@ -1,10 +1,12 @@
import { Iterable, fromJS } from 'immutable'
import getIn from './getIn'
import get from './get'

const structure = {
fromJS: jsValue => fromJS(jsValue, (key, value) =>
Iterable.isIndexed(value) ? value.toList() : value.toMap()),
getIn,
get,
merge: (state, payload) => state.merge(payload),
toJS: value => Iterable.isIterable(value) ? value.toJS() : value,
}
Expand Down
5 changes: 5 additions & 0 deletions src/structure/plain/get.js
@@ -0,0 +1,5 @@
const get = (state, key) => {
return state[key]
}

export default get
2 changes: 2 additions & 0 deletions src/structure/plain/index.js
@@ -1,8 +1,10 @@
import getIn from './getIn'
import get from './get'

const structure = {
fromJS: value => value,
getIn,
get,
merge: (state, payload) => ({ ...state, ...payload }),
toJS: value => value,
}
Expand Down
2 changes: 2 additions & 0 deletions src/structure/seamless-immutable/index.js
@@ -1,11 +1,13 @@
import SeamlessImmutable from 'seamless-immutable'
import getIn from '../plain/getIn'
import get from '../plain/get'

const { static: Immutable } = SeamlessImmutable

const structure = {
fromJS: value => Immutable.from(value),
getIn,
get,
merge: (state, payload) => Immutable.merge(state, payload),
toJS: value => Immutable.asMutable(value)
}
Expand Down
15 changes: 15 additions & 0 deletions test/selectors.test.js
Expand Up @@ -23,6 +23,21 @@ describe("selectors", () => {
store = createStore(reducer)
})

describe("when router not found", () => {
beforeEach(() => {
const reducer = combineReducers({
notTheRouter: connectRouter(history)
})
store = createStore(reducer)
})

it("throws helpful error", () => {
store.dispatch(push('/'))
const state = store.getState()
expect(() => getLocation(state)).toThrowError('Could not find router reducer in state tree. Are you sure it is mounted under "router"?')
})
})

describe("getLocation", () => {
it("gets the location from the state", () => {
const location = { pathname: "/", hash: '', search: '' }
Expand Down