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

Commit

Permalink
implement integration testing for url change and state change
Browse files Browse the repository at this point in the history
fix a BOATLOAD of problems found in the integration testing.
Approaching a stable release
  • Loading branch information
cellog committed Apr 13, 2017
1 parent f564067 commit fea5e2d
Show file tree
Hide file tree
Showing 15 changed files with 676 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as types from './types'

function makeUrlAction(name) {
return (details, state = undefined) => ({
return (details, state = {}) => ({
type: types.ACTION,
payload: {
verb: name,
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const setServer = (val = true) => {
}

export const pending = opts => opts.pending
export const nonBlockingPending = opts => !!opts.pending

export const begin = (opts) => {
const o = opts
Expand All @@ -35,7 +36,7 @@ export const begin = (opts) => {
export const commit = (opts) => {
const o = opts
o.pending = false
o.resolve(false)
o.resolve(true)
return false
}

Expand Down
1 change: 0 additions & 1 deletion src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const defaultState = {
search: '',
hash: ''
},
pending: false,
matchedRoutes: [],
routes: {
ids: [],
Expand Down
23 changes: 12 additions & 11 deletions src/sagas/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as actions from '../actions'
import * as routes from './matchRoutes'
import * as create from './createRoutes'
import * as stateChanges from './stateChanges'
import { begin, commit, pending } from '..'
import { begin, commit, nonBlockingPending } from '..'

export function *browserListener(history) {
while (true) { // eslint-disable-line
Expand All @@ -22,30 +22,31 @@ export function *locationListener(channel, options) {
const path = createPath(locationChange.location)
if (location !== path) {
location = path
yield call(pending, options)
if (yield call(nonBlockingPending, options)) {
continue // eslint-disable-line
}
yield call(begin, options)
yield call(routes.matchRoutes, yield select(), path, options.enhancedRoutes)
yield put(actions.route(locationChange.location))
yield call(commit, options)
}
yield put(actions.route(locationChange.location))
}
}

export function *doState(state, options) {
yield call(begin, options)
yield call(stateChanges.handleStateChange, state, options.enhancedRoutes)
yield call(commit, options)
}

export function *stateMonitor(options) {
let state = yield select()
while (true) { // eslint-disable-line
yield take('*')
yield call(pending, options)
const newState = yield select()
if (yield call(nonBlockingPending, options)) {
state = newState
continue // eslint-disable-line
}
if (state !== newState) {
state = newState
yield fork(doState, state, options)
yield call(begin, options)
yield call(stateChanges.handleStateChange, state, options.enhancedRoutes)
yield call(commit, options)
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/sagas/matchRoutes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { call, put, select } from 'redux-saga/effects'
import { createPath } from 'history'

import * as selectors from '../selectors'
import * as actions from '../actions'
import * as urlChanges from './urlChanges'

export function template(s, params) {
return s.exitParams instanceof Function ?
s.exitParams(params) : s.exitParams
{ ...s.exitParams(params) } : { ...s.exitParams }
}

export const exitRoute = (state, enhanced) => function *(s) {
const params = selectors.oldParams(state, s.name)
const params = s.params
let parentParams = params
let a = s
while (a.parent) {
Expand All @@ -23,12 +22,13 @@ export const exitRoute = (state, enhanced) => function *(s) {
a = parent
}
parentParams = { ...parentParams, ...template(s, parentParams) }
yield call(urlChanges.updateState, parentParams, state)
yield call(urlChanges.updateState, s, parentParams, state)
}

export const diff = (main, second) => main.filter(name => second.indexOf(name) === -1)
export const exitRoutes = exit => location => name => call(exit, name, location)
export const filter = (enhancedRoutes, path) => name => enhancedRoutes[name]['@parser'].match(path)
export const mapRoute = (er, enhancedRoutes) => route => exitRoutes(er)(enhancedRoutes[route])

export function *matchRoutes(state, path, enhancedRoutes) {
const lastMatches = state.routing.matchedRoutes
Expand All @@ -46,7 +46,8 @@ export function *matchRoutes(state, path, enhancedRoutes) {
}
if (exiting.length) {
const er = yield call(exitRoute, state, enhancedRoutes)
yield exiting.map(exitRoutes(er)(createPath(state.routing.location)))
const r = yield call(mapRoute, er, enhancedRoutes)
yield exiting.map(route => call(r, route))
}
yield call(urlChanges.stateFromLocation, enhancedRoutes, yield select(), path)
}
2 changes: 1 addition & 1 deletion src/sagas/stateChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function *handleStateChange(state, enhancedRoutes) {
const url = yield call(getUrlUpdate, state, s)
if (url) {
const params = yield call([s['@parser'], s['@parser'].match], url)
const newState = yield call([s, s.stateFromParams], params, state)
const newState = yield call(s.stateFromParams, params, state)
yield put(actions.setParamsAndState(s.name, params, newState))
yield put(actions.push(url))
}
Expand Down
4 changes: 2 additions & 2 deletions test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('react-redux-saga-router actions', () => {
payload: {
verb: 'push',
route: '/hi',
state: undefined
state: {}
}
})
expect(actions.push('/hi', { some: 'state' })).eqls({
Expand All @@ -27,7 +27,7 @@ describe('react-redux-saga-router actions', () => {
payload: {
verb: 'replace',
route: '/hi',
state: undefined
state: {}
}
})
expect(actions.replace('/hi', { some: 'state' })).eqls({
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('react-redux-saga-router', () => {
expect(pending).eqls(true)
expect(index.pending(options)).equals(options.pending)
options.pending.then((value) => {
expect(value).eqls(false)
expect(value).eqls(true)
done()
})
index.commit(options)
Expand Down
Loading

0 comments on commit fea5e2d

Please sign in to comment.