Skip to content

Commit

Permalink
[enhance] Warn when fetch actions reach reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Apr 24, 2019
1 parent cac9a69 commit dde07f8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/state/__tests__/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ describe('reducer', () => {
const newState = reducer(iniState, action);
expect(newState).toEqual(iniState);
});
it('other types should do nothing', () => {
it('rest-hooks/fetch should console.warn()', () => {
global.console.warn = jest.fn();
const action: FetchAction = {
type: 'rest-hooks/fetch',
payload: () => new Promise<any>(() => null),
Expand All @@ -291,5 +292,18 @@ describe('reducer', () => {
};
const newState = reducer(iniState, action);
expect(newState).toBe(iniState);
expect((global.console.warn as jest.Mock).mock.calls.length).toBe(2);
});
it('other types should do nothing', () => {
const action: any = {
type: 'whatever',
};
const iniState = {
entities: {},
results: { abc: '5' },
meta: {},
};
const newState = reducer(iniState, action);
expect(newState).toBe(iniState);
});
});
5 changes: 5 additions & 0 deletions src/state/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ export default function reducer(state: State<unknown> | undefined, action: Actio
entities: e,
};
default:
// If 'fetch' action reaches the reducer there are no middlewares installed to handle it
if (process.env.NODE_ENV !== 'production' && action.type === 'rest-hooks/fetch') {
console.warn('Reducer recieved fetch action - you are likely missing the NetworkManager middleware')
console.warn('See https://resthooks.io/docs/guides/redux#indextsx for hooking up redux')
}
// A reducer must always return a valid state.
// Alternatively you can throw an error if an invalid action is dispatched.
return state;
Expand Down

0 comments on commit dde07f8

Please sign in to comment.