Skip to content

Commit

Permalink
enhance: Move NetworkManager not found detection to applyManager()
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Jul 18, 2024
1 parent d8666bf commit d84b43c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/spotty-pumpkins-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@data-client/core': patch
'@data-client/react': patch
---

Move NetworkManager missing detection to initialization (applyManager())
38 changes: 38 additions & 0 deletions packages/core/src/manager/__tests__/applyManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Controller from '../../controller/Controller';
import applyManager from '../applyManager';
import NetworkManager from '../NetworkManager';

function onError(e: any) {
e.preventDefault();
}
beforeEach(() => {
if (typeof addEventListener === 'function')
addEventListener('error', onError);
});
afterEach(() => {
if (typeof removeEventListener === 'function')
removeEventListener('error', onError);
});

it('applyManagers should console.warn() when no NetworkManager is provided', () => {
const warnspy = jest
.spyOn(global.console, 'warn')
.mockImplementation(() => {});
try {
applyManager([], new Controller());
expect(warnspy.mock.calls.length).toBe(2);
} finally {
warnspy.mockRestore();
}
});
it('applyManagers should not console.warn() when NetworkManager is provided', () => {
const warnspy = jest
.spyOn(global.console, 'warn')
.mockImplementation(() => {});
try {
applyManager([new NetworkManager()], new Controller());
expect(warnspy.mock.calls.length).toBe(0);
} finally {
warnspy.mockRestore();
}
});
11 changes: 11 additions & 0 deletions packages/core/src/manager/applyManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import NetworkManager from './NetworkManager.js';
import type Controller from '../controller/Controller.js';
import type { Reducer, Dispatch, ReducerState } from '../middlewareTypes.js';
import { Manager } from '../types.js';
Expand All @@ -6,6 +7,16 @@ export default function applyManager(
managers: Manager[],
controller: Controller,
): Middleware[] {
/* istanbul ignore next */
if (
process.env.NODE_ENV !== 'production' &&
!managers.find(mgr => mgr instanceof NetworkManager)
) {
console.warn('NetworkManager not found; this is a required manager.');
console.warn(
'See https://dataclient.io/docs/guides/redux for hooking up redux',
);
}
return managers.map((manager, i) => {
const middleware = manager.getMiddleware();
return ({ dispatch, getState }) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/state/__tests__/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ describe('reducer', () => {
const newState = reducer(iniState, action);
expect(newState.entities).toBe(iniState.entities);
});
it('rdc/fetch should console.warn()', () => {
it('rdc/fetch should not console.warn()', () => {
const warnspy = jest
.spyOn(global.console, 'warn')
.mockImplementation(() => {});
Expand All @@ -618,7 +618,8 @@ describe('reducer', () => {
};
const newState = reducer(iniState, action);
expect(newState).toBe(iniState);
expect(warnspy.mock.calls.length).toBe(2);
// moved warns to applyManager() vv
expect(warnspy.mock.calls.length).toBe(0);
} finally {
warnspy.mockRestore();
}
Expand Down
14 changes: 1 addition & 13 deletions packages/core/src/state/reducer/fetchReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ export function fetchReducer(state: State<unknown>, action: FetchAction) {
...state,
optimistic: [...state.optimistic, setAction],
};
} else {
// If 'fetch' action reaches the reducer there are no middlewares installed to handle it
/* istanbul ignore next */
if (process.env.NODE_ENV !== 'production') {
console.warn(
'Fetch appears unhandled - you are likely missing the NetworkManager middleware',
);
console.warn(
'See https://dataclient.io/docs/guides/redux for hooking up redux',
);
}

return state;
}
return state;
}

0 comments on commit d84b43c

Please sign in to comment.