diff --git a/.changeset/spotty-pumpkins-explain.md b/.changeset/spotty-pumpkins-explain.md new file mode 100644 index 000000000000..aa8a86b7eab9 --- /dev/null +++ b/.changeset/spotty-pumpkins-explain.md @@ -0,0 +1,6 @@ +--- +'@data-client/core': patch +'@data-client/react': patch +--- + +Move NetworkManager missing detection to initialization (applyManager()) diff --git a/packages/core/src/manager/__tests__/applyManager.ts b/packages/core/src/manager/__tests__/applyManager.ts new file mode 100644 index 000000000000..333dd00657ea --- /dev/null +++ b/packages/core/src/manager/__tests__/applyManager.ts @@ -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(); + } +}); diff --git a/packages/core/src/manager/applyManager.ts b/packages/core/src/manager/applyManager.ts index 8e3b22e5886b..4b0b697a6d8f 100644 --- a/packages/core/src/manager/applyManager.ts +++ b/packages/core/src/manager/applyManager.ts @@ -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'; @@ -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 }) => { diff --git a/packages/core/src/state/__tests__/reducer.ts b/packages/core/src/state/__tests__/reducer.ts index ae00ed24a562..1ad2165c0922 100644 --- a/packages/core/src/state/__tests__/reducer.ts +++ b/packages/core/src/state/__tests__/reducer.ts @@ -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(() => {}); @@ -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(); } diff --git a/packages/core/src/state/reducer/fetchReducer.ts b/packages/core/src/state/reducer/fetchReducer.ts index 46d5199b406c..e1274d250c41 100644 --- a/packages/core/src/state/reducer/fetchReducer.ts +++ b/packages/core/src/state/reducer/fetchReducer.ts @@ -12,18 +12,6 @@ export function fetchReducer(state: State, 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; }