Skip to content

Commit

Permalink
Simplifies reducer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rjz committed Nov 9, 2017
1 parent 18e8482 commit f0c9991
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/components/__tests__/counter_spec.tsx
Expand Up @@ -6,7 +6,7 @@ import * as ReactShallowRenderer from 'react-test-renderer/shallow'
import { createStore } from 'redux'

import { Counter } from '../counter'
import { reducers } from '../../reducers'
import reducers from '../../reducers'

describe('components/Counter', () => {

Expand Down
4 changes: 2 additions & 2 deletions src/index.tsx
Expand Up @@ -4,12 +4,12 @@ import * as redux from 'redux'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'

import * as state from './reducers'
import reducers, * as state from './reducers'

import { Counter } from './components/counter'

const store: redux.Store<state.All> = redux.createStore(
state.reducers,
reducers,
{} as state.All,
redux.applyMiddleware(thunk),
)
Expand Down
34 changes: 34 additions & 0 deletions src/reducers/__tests__/__snapshots__/index_spec.ts.snap
@@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`reducers/counter increments 1`] = `
Object {
"counter": Object {
"value": 6,
},
"error": "",
"isLoading": false,
"isSaving": false,
}
`;

exports[`reducers/counter restores state 1`] = `
Object {
"counter": Object {
"value": 14,
},
"error": "",
"isLoading": false,
"isSaving": false,
}
`;

exports[`reducers/counter starts at 0 1`] = `
Object {
"counter": Object {
"value": 0,
},
"error": "",
"isLoading": false,
"isSaving": false,
}
`;
52 changes: 23 additions & 29 deletions src/reducers/__tests__/index_spec.ts
@@ -1,37 +1,31 @@
import { createStore } from 'redux'
import reducers, {
initialState,
} from '../index'

import { reducers } from '../index'
import {
incrementCounter,
incrementCounter
} from '../../actions'

const resultOf = actions =>
actions.reduce(reducers, initialState)

describe('reducers/counter', () => {
it('starts at 0', () => {
const store = createStore(reducers)
const { counter } = store.getState()
expect(counter.value).toEqual(0)
})
it('starts at 0', () =>
expect(resultOf([])).toMatchSnapshot())

it('increments', (done) => {
const store = createStore(reducers)
store.subscribe(() => {
const { counter } = store.getState()
expect(counter.value).toEqual(3)
done()
})
store.dispatch(incrementCounter(3))
})
it('increments', () =>
expect(resultOf([
incrementCounter(1),
incrementCounter(2),
incrementCounter(3),
])).toMatchSnapshot())

it('restores state', (done) => {
const store = createStore(reducers)
store.subscribe(() => {
const { counter } = store.getState()
expect(counter.value).toEqual(14)
done()
})
store.dispatch({
type: 'LOAD_COUNT_SUCCESS',
request: {},
response: { value: 14 } })
})
it('restores state', () =>
expect(resultOf([
{
type: 'LOAD_COUNT_SUCCESS',
request: {},
response: { value: 14 },
}
])).toMatchSnapshot())
})
15 changes: 12 additions & 3 deletions src/reducers/index.ts
Expand Up @@ -48,11 +48,11 @@ function error (state: string = '', action: Action): string {
}
}

const initialState: Counter = {
const counterState: Counter = {
value: 0,
}

function counter (state: Counter = initialState, action: Action): Counter {
function counter (state: Counter = counterState, action: Action): Counter {
switch (action.type) {
case 'INCREMENT_COUNTER':
const { delta } = action
Expand All @@ -69,9 +69,18 @@ function counter (state: Counter = initialState, action: Action): Counter {
}
}

export const reducers = combineReducers<All>({
export const initialState = {
counter: counterState,
isSaving: false,
isLoading: false,
error: '',
}

const reducers = combineReducers<All>({
counter,
isSaving,
isLoading,
error,
})

export default reducers

0 comments on commit f0c9991

Please sign in to comment.