Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
added TYPES constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwooley committed Sep 18, 2016
1 parent e822c36 commit 1e91d03
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ const asyncActions = {
const testReducerCreator = easyReducerCreator(defaultState, syncActions, asyncActions)
// testReducer1 now has all the action creators you would use for your methods, with the types TR1/methodName
export const testReducer1 = testReducerCreator('TR1')

// generated types as constants for reference with RXJS epics or whatever
// EG: {testDataPlusNum: 'TR1/testDataPlusNum', testDataPlusTwoNum: 'TR1/testDataPlusTwoNum'}
console.log(testReducer1.TYPES)

// Reducers are reusable with different ID's
export const testReducer2 = testReducerCreator('TR2')

Expand Down
10 changes: 8 additions & 2 deletions lib/reducerCreator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/reducerCreator.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion src/__tests__/reducerCreator.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import reducerCreator from '../reducerCreator'
import { createStore, applyMiddleware, combineReducers } from 'redux'
import thunk from 'redux-thunk'
Expand All @@ -14,6 +13,14 @@ describe('reducerCreator', () => {
it('should throw an error if there is no default state', () => {
expect(() => (reducerCreator as any)()('ID')).toThrowError('Reducers must have a default state')
})
it('should throw an error if you you try to name your action `TYPES`', () => {
expect(() => (reducerCreator as any)({}, {TYPES: ''})('ID'))
.toThrowError(`You cannot have an action called 'TYPES'`)
})
it('should throw an error if you you try to name your action `reducer`', () => {
expect(() => (reducerCreator as any)({}, {reducer: ''})('ID'))
.toThrowError(`You cannot have an action called 'reducer'`)
})
describe('sync actions', () => {
let testReducer
let defaultState = {
Expand All @@ -37,6 +44,13 @@ describe('reducerCreator', () => {
expect(testReducer.testDataPlusManyNumbers(1, 2, 3, 4))
.toEqual({type: 'ID/testDataPlusManyNumbers', payload: [1, 2, 3, 4]})
})
it('should have type constansts', () => {
expect(testReducer.TYPES).toEqual({
'testDataPlus1': 'ID/testDataPlus1',
'testDataPlusNumber': 'ID/testDataPlusNumber',
'testDataPlusManyNumbers': 'ID/testDataPlusManyNumbers'
})
})
})
describe('reducing', () => {
let testReducer
Expand Down
18 changes: 14 additions & 4 deletions src/reducerCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ export interface IReducer {
reducer: Function
}

const restrictedActionNames = {
TYPES: true,
reducer: true
}

export default function makeReducer
<ISubState, T, V>
(defaultState: ISubState, Actions?: T, AsyncActions?: V): (ID: string) => V & T & IReducer {
Expand All @@ -16,14 +21,18 @@ export default function makeReducer

const newSyncActions: T & IReducer = merge(({} as IReducer), Actions)
const newAsyncActions: V = merge({}, AsyncActions)

const TYPES = {}
// Actions are now functions that auto return types
Object.keys(Actions).forEach((key) => {
(newSyncActions[key]) = (...payload: any[]) => {
return { type: `${ID}/${key}`, payload }
if (restrictedActionNames[key]) {
throw new Error(`You cannot have an action called '${key}'`)
}
const type = `${ID}/${key}`
TYPES[key] = type
newSyncActions[key] = (...payload: any[]) => {
return { type, payload }
}
})

if (AsyncActions) {
// async actions have dispatch and the payload injected into them.
Object.keys(AsyncActions).forEach((key) => {
Expand All @@ -37,6 +46,7 @@ export default function makeReducer
})
}
const baseReducer = {
TYPES,
reducer: (state: ISubState, action: {type: string, payload?: any}) => {
state = state || defaultState
/* tslint:disable */
Expand Down

0 comments on commit 1e91d03

Please sign in to comment.