Skip to content

Commit d84b43c

Browse files
committed
enhance: Move NetworkManager not found detection to applyManager()
1 parent d8666bf commit d84b43c

File tree

5 files changed

+59
-15
lines changed

5 files changed

+59
-15
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@data-client/core': patch
3+
'@data-client/react': patch
4+
---
5+
6+
Move NetworkManager missing detection to initialization (applyManager())
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Controller from '../../controller/Controller';
2+
import applyManager from '../applyManager';
3+
import NetworkManager from '../NetworkManager';
4+
5+
function onError(e: any) {
6+
e.preventDefault();
7+
}
8+
beforeEach(() => {
9+
if (typeof addEventListener === 'function')
10+
addEventListener('error', onError);
11+
});
12+
afterEach(() => {
13+
if (typeof removeEventListener === 'function')
14+
removeEventListener('error', onError);
15+
});
16+
17+
it('applyManagers should console.warn() when no NetworkManager is provided', () => {
18+
const warnspy = jest
19+
.spyOn(global.console, 'warn')
20+
.mockImplementation(() => {});
21+
try {
22+
applyManager([], new Controller());
23+
expect(warnspy.mock.calls.length).toBe(2);
24+
} finally {
25+
warnspy.mockRestore();
26+
}
27+
});
28+
it('applyManagers should not console.warn() when NetworkManager is provided', () => {
29+
const warnspy = jest
30+
.spyOn(global.console, 'warn')
31+
.mockImplementation(() => {});
32+
try {
33+
applyManager([new NetworkManager()], new Controller());
34+
expect(warnspy.mock.calls.length).toBe(0);
35+
} finally {
36+
warnspy.mockRestore();
37+
}
38+
});

packages/core/src/manager/applyManager.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import NetworkManager from './NetworkManager.js';
12
import type Controller from '../controller/Controller.js';
23
import type { Reducer, Dispatch, ReducerState } from '../middlewareTypes.js';
34
import { Manager } from '../types.js';
@@ -6,6 +7,16 @@ export default function applyManager(
67
managers: Manager[],
78
controller: Controller,
89
): Middleware[] {
10+
/* istanbul ignore next */
11+
if (
12+
process.env.NODE_ENV !== 'production' &&
13+
!managers.find(mgr => mgr instanceof NetworkManager)
14+
) {
15+
console.warn('NetworkManager not found; this is a required manager.');
16+
console.warn(
17+
'See https://dataclient.io/docs/guides/redux for hooking up redux',
18+
);
19+
}
920
return managers.map((manager, i) => {
1021
const middleware = manager.getMiddleware();
1122
return ({ dispatch, getState }) => {

packages/core/src/state/__tests__/reducer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ describe('reducer', () => {
595595
const newState = reducer(iniState, action);
596596
expect(newState.entities).toBe(iniState.entities);
597597
});
598-
it('rdc/fetch should console.warn()', () => {
598+
it('rdc/fetch should not console.warn()', () => {
599599
const warnspy = jest
600600
.spyOn(global.console, 'warn')
601601
.mockImplementation(() => {});
@@ -618,7 +618,8 @@ describe('reducer', () => {
618618
};
619619
const newState = reducer(iniState, action);
620620
expect(newState).toBe(iniState);
621-
expect(warnspy.mock.calls.length).toBe(2);
621+
// moved warns to applyManager() vv
622+
expect(warnspy.mock.calls.length).toBe(0);
622623
} finally {
623624
warnspy.mockRestore();
624625
}

packages/core/src/state/reducer/fetchReducer.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ export function fetchReducer(state: State<unknown>, action: FetchAction) {
1212
...state,
1313
optimistic: [...state.optimistic, setAction],
1414
};
15-
} else {
16-
// If 'fetch' action reaches the reducer there are no middlewares installed to handle it
17-
/* istanbul ignore next */
18-
if (process.env.NODE_ENV !== 'production') {
19-
console.warn(
20-
'Fetch appears unhandled - you are likely missing the NetworkManager middleware',
21-
);
22-
console.warn(
23-
'See https://dataclient.io/docs/guides/redux for hooking up redux',
24-
);
25-
}
26-
27-
return state;
2815
}
16+
return state;
2917
}

0 commit comments

Comments
 (0)