Skip to content

Commit

Permalink
Dispatches an event to indicate effect firing and fixes bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlplusb committed Oct 24, 2018
1 parent b4abe08 commit 4900bd1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
30 changes: 28 additions & 2 deletions src/__tests__/easy-peasy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ beforeEach(() => {
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ = undefined
})

const trackActionsMiddleware = () => {
const middleware = () => next => action => {
middleware.actions.push(action)
return next(action)
}
middleware.actions = []
return middleware
}

test('empty object in state', () => {
// arrange
const model = {
Expand Down Expand Up @@ -297,7 +306,7 @@ test('supports initial state', () => {
foo: {
bar: {
stuff: [3, 4],
other: 'qux',
invalid: 'qux',
},
},
}
Expand All @@ -310,10 +319,27 @@ test('supports initial state', () => {
foo: {
bar: {
stuff: [3, 4],
other: 'qux',
},
color: 'red',
},
baz: 'bob',
})
})

test('dispatches an action to represent the start of an effect', () => {
// arrange
const model = {
foo: {
doSomething: effect(() => undefined),
},
}
const trackActions = trackActionsMiddleware()
const store = createStore(model, { middleware: [trackActions] })
const payload = 'hello'

// act
store.dispatch.foo.doSomething(payload)

// assert
expect(trackActions.actions).toEqual([{ type: 'foo.doSomething', payload }])
})
16 changes: 12 additions & 4 deletions src/easy-peasy.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,20 @@ export const createStore = (model, options = {}) => {
const value = current[key]
const path = [...parentPath, key]
if (typeof value === 'function') {
const actionName = path.join('.')

if (value[effectSymbol]) {
// Effect Action
const action = payload =>
value(references.dispatch, payload, {
const action = payload => {
references.dispatch({
type: actionName,
payload,
})
return value(references.dispatch, payload, {
getState: references.getState,
})
}
action.actionName = actionName
set(path, actionEffects, action)

// Effect Action Creator
Expand All @@ -74,7 +82,7 @@ export const createStore = (model, options = {}) => {
getState: references.getState,
}),
)
action.actionName = path.join('.')
action.actionName = actionName
set(path, actionReducers, action)

// Reducer Action Creator
Expand All @@ -85,7 +93,7 @@ export const createStore = (model, options = {}) => {
}),
)
}
} else if (isObject(value) && Object.keys(value).length > 1) {
} else if (isObject(value) && Object.keys(value).length > 0) {
extract(value, path)
} else {
// State
Expand Down

0 comments on commit 4900bd1

Please sign in to comment.