Skip to content

Commit

Permalink
feat(api): state is now newState
Browse files Browse the repository at this point in the history
when returning fx and state from the reducer the state should be in the newState field
  • Loading branch information
Matthieu Beteille committed Oct 20, 2017
1 parent 427f51a commit 949086b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export interface StateWithFx {
[key: string]: any
state: any
newState: any
_fx: { [key: string]: any }
}

export function hasFX(s: any): s is StateWithFx {
return s && typeof s._fx === 'object' && '_fx' in s && 'state' in s
return s && typeof s._fx === 'object' && '_fx' in s && 'newState' in s
}
4 changes: 2 additions & 2 deletions src/redux-data-fx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ const reduxDataFX = <S>(createStore: StoreEnhancerStoreCreator<S>) => (
const result = reducer(state, action)

if (hasFX(result)) {
let { _fx, state } = result
let { _fx, newState } = result
forEach(_fx, (params, id) => {
q.push([id, params])
})
return state
return newState
} else {
return result
}
Expand Down
20 changes: 6 additions & 14 deletions test/redux-data-fx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function reducer(state: State = initialState, action: Action) {
switch (action.type) {
case 'wait':
return {
state,
newState: state,
_fx: {
timeout: {
value: 50,
Expand All @@ -64,7 +64,7 @@ function reducer(state: State = initialState, action: Action) {
ok: 'cool'
}
},
state
newState: state
}

case 'storageSet':
Expand All @@ -77,7 +77,7 @@ function reducer(state: State = initialState, action: Action) {
}
}
},
state
newState: state
}

case 'storageRemove':
Expand All @@ -87,7 +87,7 @@ function reducer(state: State = initialState, action: Action) {
remove: ['key1', 'key2']
}
},
state
newState: { value: 1000 }
}

default:
Expand Down Expand Up @@ -128,21 +128,13 @@ describe('Simple FX tests', () => {
expect(true).toBeTruthy()
})

// it("should trigger timeout side fx", done => {
// store.dispatch({ type: "timeout" });
// expect(store.getState().value).toBe(1);
// setTimeout(() => {
// expect(store.getState().value).toBe(2);
// done();
// }, 100);
// });

it('should use localStorage side fx', () => {
it('should run localStorage side fx, and update the state', () => {
store.dispatch({ type: 'storageSet' })
expect(localStorage.getItem('key1')).toBe('value1')
expect(localStorage.getItem('key2')).toBe('value2')
store.dispatch({ type: 'storageRemove' })
expect(localStorage.getItem('key1')).toBe(undefined)
expect(localStorage.getItem('key2')).toBe(undefined)
expect(store.getState().value).toBe(1000)
})
})

0 comments on commit 949086b

Please sign in to comment.