diff --git a/src/instrument.js b/src/instrument.js index 64444207..f110acd0 100644 --- a/src/instrument.js +++ b/src/instrument.js @@ -238,7 +238,7 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options if (options.maxAge && stagedActionIds.length === options.maxAge) { // If maxAge has been reached, remove oldest action. delete actionsById[stagedActionIds[1]]; - skippedActionIds = skippedActionIds.filter(id => id !== stagedActionIds[0]); + skippedActionIds = skippedActionIds.filter(id => id !== stagedActionIds[1]); stagedActionIds = [0].concat(stagedActionIds.slice(2)); committedState = computedStates[1].state; computedStates = computedStates.slice(1); diff --git a/test/instrument.spec.js b/test/instrument.spec.js index 00c2342b..986111cd 100644 --- a/test/instrument.spec.js +++ b/test/instrument.spec.js @@ -300,27 +300,30 @@ describe('instrument', () => { let configuredLiftedStore; beforeEach(() => { - configuredStore = createStore(counter, instrument(undefined, { maxAge: 2 })); + configuredStore = createStore(counter, instrument(undefined, { maxAge: 3 })); configuredLiftedStore = configuredStore.liftedStore; }); - it('should remove earliest action when maxAge is reached', () => { + it('should auto-commit earliest non-@@INIT action when maxAge is reached', () => { + configuredStore.dispatch({ type: 'INCREMENT' }); configuredStore.dispatch({ type: 'INCREMENT' }); let liftedStoreState = configuredLiftedStore.getState(); - expect(configuredStore.getState()).toBe(1); - expect(Object.keys(liftedStoreState.actionsById).length).toBe(2); + expect(configuredStore.getState()).toBe(2); + expect(Object.keys(liftedStoreState.actionsById).length).toBe(3); expect(liftedStoreState.committedState).toBe(undefined); + expect(liftedStoreState.stagedActionIds).toInclude(1); + // Triggers auto-commit. configuredStore.dispatch({ type: 'INCREMENT' }); liftedStoreState = configuredLiftedStore.getState(); - expect(configuredStore.getState()).toBe(2); - expect(Object.keys(liftedStoreState.actionsById).length).toBe(2); - expect(liftedStoreState.stagedActionIds).toExclude(0); + expect(configuredStore.getState()).toBe(3); + expect(Object.keys(liftedStoreState.actionsById).length).toBe(3); + expect(liftedStoreState.stagedActionIds).toExclude(1); expect(liftedStoreState.computedStates[0].state).toBe(1); expect(liftedStoreState.committedState).toBe(1); - expect(liftedStoreState.currentStateIndex).toBe(1); + expect(liftedStoreState.currentStateIndex).toBe(2); }); it('should handle skipped actions', () => {