Skip to content

Commit b4da7aa

Browse files
committed
Bring over importing state from #118
1 parent c23a997 commit b4da7aa

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/instrument.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export const ActionTypes = {
55
COMMIT: 'COMMIT',
66
SWEEP: 'SWEEP',
77
TOGGLE_ACTION: 'TOGGLE_ACTION',
8-
JUMP_TO_STATE: 'JUMP_TO_STATE'
8+
JUMP_TO_STATE: 'JUMP_TO_STATE',
9+
IMPORT_STATE: 'IMPORT_STATE'
910
};
1011

1112
/**
@@ -38,6 +39,10 @@ export const ActionCreators = {
3839

3940
jumpToState(index) {
4041
return { type: ActionTypes.JUMP_TO_STATE, index };
42+
},
43+
44+
importState(nextLiftedState) {
45+
return { type: ActionTypes.IMPORT_STATE, nextLiftedState };
4146
}
4247
};
4348

@@ -186,6 +191,16 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer) {
186191
);
187192
computedStates = [...computedStates, nextEntry];
188193
break;
194+
case ActionTypes.IMPORT_STATE:
195+
({
196+
stagedActions,
197+
skippedActions,
198+
computedStates,
199+
currentStateIndex,
200+
timestamps,
201+
monitorState
202+
} = liftedAction.nextLiftedState);
203+
break;
189204
case '@@redux/INIT':
190205
// Always recompute states on hot reload and init.
191206
shouldRecomputeStates = true;
@@ -266,7 +281,7 @@ function unliftStore(liftedStore, liftReducer) {
266281
}
267282

268283
/**
269-
* Redux History store enhancer.
284+
* Redux instrumentation store enhancer.
270285
*/
271286
export default function instrument(monitorReducer = () => null) {
272287
return createStore => (reducer, initialState) => {

test/instrument.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,40 @@ describe('instrument', () => {
215215
monitoredInstrumentedStore.dispatch(ActionCreators.jumpToState(3));
216216
expect(reducerCalls).toBe(4);
217217
});
218+
219+
describe('Import State', () => {
220+
let monitoredStore;
221+
let monitoredLiftedStore;
222+
let exportedState;
223+
224+
beforeEach(() => {
225+
monitoredStore = instrument()(createStore)(counter);
226+
monitoredLiftedStore = monitoredStore.liftedStore;
227+
// Set up state to export
228+
monitoredStore.dispatch({ type: 'INCREMENT' });
229+
monitoredStore.dispatch({ type: 'INCREMENT' });
230+
monitoredStore.dispatch({ type: 'INCREMENT' });
231+
232+
exportedState = monitoredLiftedStore.getState();
233+
});
234+
235+
it('should replay all the steps when a state is imported', () => {
236+
let importMonitoredStore = instrument()(createStore)(counter);
237+
let importMonitoredLiftedStore = importMonitoredStore.liftedStore;
238+
// Import exported state
239+
importMonitoredLiftedStore.dispatch(ActionCreators.importState(exportedState));
240+
expect(importMonitoredLiftedStore.getState()).toEqual(exportedState);
241+
});
242+
243+
it('should replace the existing action log with the one imported', () => {
244+
let importMonitoredStore = instrument()(createStore)(counter);
245+
let importMonitoredLiftedStore = importMonitoredStore.liftedStore;
246+
247+
importMonitoredStore.dispatch({ type: 'DECREMENT' });
248+
importMonitoredStore.dispatch({ type: 'DECREMENT' });
249+
250+
importMonitoredLiftedStore.dispatch(ActionCreators.importState(exportedState));
251+
expect(importMonitoredLiftedStore.getState()).toEqual(exportedState);
252+
});
253+
});
218254
});

0 commit comments

Comments
 (0)