Skip to content

Commit 8316de0

Browse files
committed
Tweak tests to use new API and add another warning
1 parent f1ecc9d commit 8316de0

File tree

4 files changed

+59
-37
lines changed

4 files changed

+59
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@
6363
"dependencies": {
6464
"lodash": "^3.10.1",
6565
"react-redux": "^4.0.0",
66-
"redux": "^3.0.0"
66+
"redux": "^3.2.1"
6767
}
6868
}

src/instrument.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,30 @@ function unliftStore(liftedStore, liftReducer) {
341341
*/
342342
export default function instrument(monitorReducer = () => null) {
343343
return createStore => (reducer, initialState, enhancer) => {
344+
344345
function liftReducer(r) {
345346
if (typeof r !== 'function') {
346-
throw new Error('Expected the nextReducer to be a function.');
347+
if (r && typeof r['default'] === 'function') {
348+
throw new Error(
349+
'Expected the reducer to be a function. ' +
350+
'Instead got an object with a "default" field. ' +
351+
'Did you pass a module instead of the default export? ' +
352+
'Try passing require(...).default instead.'
353+
);
354+
}
355+
throw new Error('Expected the reducer to be a function.');
347356
}
348357
return liftReducerWith(r, initialState, monitorReducer);
349358
}
350359

351-
const liftedStore = createStore(liftReducer(reducer), undefined, enhancer);
360+
const liftedStore = createStore(liftReducer(reducer), enhancer);
352361
if (liftedStore.liftedStore) {
353-
throw new Error('DevTools instrument shouldn\'t be included more than once. ' +
354-
'Check your store configuration.');
362+
throw new Error(
363+
'DevTools instrumentation should not be applied more than once. ' +
364+
'Check your store configuration.'
365+
);
355366
}
367+
356368
return unliftStore(liftedStore, liftReducer);
357369
};
358370
}

test/instrument.spec.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('instrument', () => {
3232
let liftedStore;
3333

3434
beforeEach(() => {
35-
store = instrument()(createStore)(counter);
35+
store = createStore(counter, instrument());
3636
liftedStore = store.liftedStore;
3737
});
3838

@@ -156,7 +156,7 @@ describe('instrument', () => {
156156

157157
it('should catch and record errors', () => {
158158
let spy = spyOn(console, 'error');
159-
let storeWithBug = instrument()(createStore)(counterWithBug);
159+
let storeWithBug = createStore(counterWithBug, instrument());
160160

161161
storeWithBug.dispatch({ type: 'INCREMENT' });
162162
storeWithBug.dispatch({ type: 'DECREMENT' });
@@ -180,12 +180,13 @@ describe('instrument', () => {
180180
expect(() => {
181181
store.dispatch({ type: undefined });
182182
}).toThrow(
183-
/Actions may not have an undefined/
183+
'Actions may not have an undefined "type" property. ' +
184+
'Have you misspelled a constant?'
184185
);
185186
});
186187

187188
it('should return the last non-undefined state from getState', () => {
188-
let storeWithBug = instrument()(createStore)(counterWithBug);
189+
let storeWithBug = createStore(counterWithBug, instrument());
189190
storeWithBug.dispatch({ type: 'INCREMENT' });
190191
storeWithBug.dispatch({ type: 'INCREMENT' });
191192
expect(storeWithBug.getState()).toBe(2);
@@ -196,7 +197,7 @@ describe('instrument', () => {
196197

197198
it('should not recompute states on every action', () => {
198199
let reducerCalls = 0;
199-
let monitoredStore = instrument()(createStore)(() => reducerCalls++);
200+
let monitoredStore = createStore(() => reducerCalls++, instrument());
200201
expect(reducerCalls).toBe(1);
201202
monitoredStore.dispatch({ type: 'INCREMENT' });
202203
monitoredStore.dispatch({ type: 'INCREMENT' });
@@ -206,7 +207,7 @@ describe('instrument', () => {
206207

207208
it('should not recompute old states when toggling an action', () => {
208209
let reducerCalls = 0;
209-
let monitoredStore = instrument()(createStore)(() => reducerCalls++);
210+
let monitoredStore = createStore(() => reducerCalls++, instrument());
210211
let monitoredLiftedStore = monitoredStore.liftedStore;
211212

212213
expect(reducerCalls).toBe(1);
@@ -249,7 +250,7 @@ describe('instrument', () => {
249250

250251
it('should not recompute states when jumping to state', () => {
251252
let reducerCalls = 0;
252-
let monitoredStore = instrument()(createStore)(() => reducerCalls++);
253+
let monitoredStore = createStore(() => reducerCalls++, instrument());
253254
let monitoredLiftedStore = monitoredStore.liftedStore;
254255

255256
expect(reducerCalls).toBe(1);
@@ -272,10 +273,9 @@ describe('instrument', () => {
272273
expect(monitoredLiftedStore.getState().computedStates).toBe(savedComputedStates);
273274
});
274275

275-
276276
it('should not recompute states on monitor actions', () => {
277277
let reducerCalls = 0;
278-
let monitoredStore = instrument()(createStore)(() => reducerCalls++);
278+
let monitoredStore = createStore(() => reducerCalls++, instrument());
279279
let monitoredLiftedStore = monitoredStore.liftedStore;
280280

281281
expect(reducerCalls).toBe(1);
@@ -301,7 +301,7 @@ describe('instrument', () => {
301301
let exportedState;
302302

303303
beforeEach(() => {
304-
monitoredStore = instrument()(createStore)(counter);
304+
monitoredStore = createStore(counter, instrument());
305305
monitoredLiftedStore = monitoredStore.liftedStore;
306306
// Set up state to export
307307
monitoredStore.dispatch({ type: 'INCREMENT' });
@@ -312,15 +312,15 @@ describe('instrument', () => {
312312
});
313313

314314
it('should replay all the steps when a state is imported', () => {
315-
let importMonitoredStore = instrument()(createStore)(counter);
315+
let importMonitoredStore = createStore(counter, instrument());
316316
let importMonitoredLiftedStore = importMonitoredStore.liftedStore;
317317

318318
importMonitoredLiftedStore.dispatch(ActionCreators.importState(exportedState));
319319
expect(importMonitoredLiftedStore.getState()).toEqual(exportedState);
320320
});
321321

322322
it('should replace the existing action log with the one imported', () => {
323-
let importMonitoredStore = instrument()(createStore)(counter);
323+
let importMonitoredStore = createStore(counter, instrument());
324324
let importMonitoredLiftedStore = importMonitoredStore.liftedStore;
325325

326326
importMonitoredStore.dispatch({ type: 'DECREMENT' });
@@ -333,12 +333,27 @@ describe('instrument', () => {
333333

334334
it('throws if reducer is not a function', () => {
335335
expect(() =>
336-
instrument()(createStore)()
337-
).toThrow('Expected the nextReducer to be a function.');
336+
createStore(undefined, instrument())
337+
).toThrow('Expected the reducer to be a function.');
338+
});
339+
340+
it('warns if the reducer is not a function but has a default field that is', () => {
341+
expect(() =>
342+
createStore(({ default: () => {} }), instrument())
343+
).toThrow(
344+
'Expected the reducer to be a function. ' +
345+
'Instead got an object with a "default" field. ' +
346+
'Did you pass a module instead of the default export? ' +
347+
'Try passing require(...).default instead.'
348+
);
338349
});
350+
339351
it('throws if there are more than one instrument enhancer included', () => {
340352
expect(() => {
341-
store = createStore(counter, undefined, compose(instrument(), instrument()));
342-
}).toThrow('DevTools instrument shouldn\'t be included more than once. Check your store configuration.');
353+
createStore(counter, compose(instrument(), instrument()))
354+
}).toThrow(
355+
'DevTools instrumentation should not be applied more than once. ' +
356+
'Check your store configuration.'
357+
);
343358
});
344359
});

test/persistState.spec.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,64 +39,59 @@ describe('persistState', () => {
3939
};
4040

4141
it('should persist state', () => {
42-
const finalCreateStore = compose(instrument(), persistState('id'))(createStore);
43-
const store = finalCreateStore(reducer);
42+
const store = createStore(reducer, compose(instrument(), persistState('id')));
4443
expect(store.getState()).toBe(0);
4544

4645
store.dispatch({ type: 'INCREMENT' });
4746
store.dispatch({ type: 'INCREMENT' });
4847
expect(store.getState()).toBe(2);
4948

50-
const store2 = finalCreateStore(reducer);
49+
const store2 = createStore(reducer, compose(instrument(), persistState('id')));
5150
expect(store2.getState()).toBe(2);
5251
});
5352

5453
it('should not persist state if no session id', () => {
55-
const finalCreateStore = compose(instrument(), persistState())(createStore);
56-
const store = finalCreateStore(reducer);
54+
const store = createStore(reducer, compose(instrument(), persistState()));
5755
expect(store.getState()).toBe(0);
5856

5957
store.dispatch({ type: 'INCREMENT' });
6058
store.dispatch({ type: 'INCREMENT' });
6159
expect(store.getState()).toBe(2);
6260

63-
const store2 = finalCreateStore(reducer);
61+
const store2 = createStore(reducer, compose(instrument(), persistState()));
6462
expect(store2.getState()).toBe(0);
6563
});
6664

6765
it('should run with a custom state deserializer', () => {
6866
const oneLess = state => state === undefined ? -1 : state - 1;
69-
const finalCreateStore = compose(instrument(), persistState('id', oneLess))(createStore);
70-
const store = finalCreateStore(reducer);
67+
const store = createStore(reducer, compose(instrument(), persistState('id', oneLess)));
7168
expect(store.getState()).toBe(0);
7269

7370
store.dispatch({ type: 'INCREMENT' });
7471
store.dispatch({ type: 'INCREMENT' });
7572
expect(store.getState()).toBe(2);
7673

77-
const store2 = finalCreateStore(reducer);
74+
const store2 = createStore(reducer, compose(instrument(), persistState('id', oneLess)));
7875
expect(store2.getState()).toBe(1);
7976
});
8077

8178
it('should run with a custom action deserializer', () => {
8279
const incToDec = action => action.type === 'INCREMENT' ? { type: 'DECREMENT' } : action;
83-
const finalCreateStore = compose(instrument(), persistState('id', undefined, incToDec))(createStore);
84-
const store = finalCreateStore(reducer);
80+
const store = createStore(reducer, compose(instrument(), persistState('id', undefined, incToDec)));
8581
expect(store.getState()).toBe(0);
8682

8783
store.dispatch({ type: 'INCREMENT' });
8884
store.dispatch({ type: 'INCREMENT' });
8985
expect(store.getState()).toBe(2);
9086

91-
const store2 = finalCreateStore(reducer);
87+
const store2 = createStore(reducer, compose(instrument(), persistState('id', undefined, incToDec)));
9288
expect(store2.getState()).toBe(-2);
9389
});
9490

9591
it('should warn if read from localStorage fails', () => {
9692
const spy = expect.spyOn(console, 'warn');
97-
const finalCreateStore = compose(instrument(), persistState('id'))(createStore);
9893
delete global.localStorage.getItem;
99-
finalCreateStore(reducer);
94+
const store = createStore(reducer, compose(instrument(), persistState('id')));
10095

10196
expect(spy.calls).toContain(
10297
/Could not read debug session from localStorage/,
@@ -105,11 +100,11 @@ describe('persistState', () => {
105100

106101
spy.restore();
107102
});
103+
108104
it('should warn if write to localStorage fails', () => {
109105
const spy = expect.spyOn(console, 'warn');
110-
const finalCreateStore = compose(instrument(), persistState('id'))(createStore);
111106
delete global.localStorage.setItem;
112-
const store = finalCreateStore(reducer);
107+
const store = createStore(reducer, compose(instrument(), persistState('id')));
113108

114109
store.dispatch({ type: 'INCREMENT' });
115110
expect(spy.calls).toContain(

0 commit comments

Comments
 (0)