Skip to content

Commit eda9728

Browse files
mgraunicolodavis
authored andcommitted
update _undo to reflect current ctx (#393)
* update _undo to reflect current ctx add tests for undo and redo of actions that change phase. * group undo/redo tests
1 parent e46f195 commit eda9728

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/core/reducer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ export function CreateGameReducer({ game, multiplayer }) {
310310
action.payload
311311
);
312312
state = apiCtx.updateAndDetach(state, true);
313+
state._undo[state._undo.length - 1].ctx = state.ctx;
313314

314315
return state;
315316
}

src/core/reducer.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,16 @@ test('undo / redo', () => {
289289
let game = Game({
290290
moves: {
291291
move: (G, ctx, arg) => ({ ...G, [arg]: true }),
292+
nextPhase: (G, ctx) => {
293+
ctx.events.endPhase({ next: 'phase2' });
294+
},
295+
},
296+
flow: {
297+
startingPhase: 'phase1',
298+
phases: {
299+
phase1: {},
300+
phase2: {},
301+
},
292302
},
293303
});
294304

@@ -336,6 +346,42 @@ test('undo / redo', () => {
336346
state = reducer(state, makeMove('move', 'A'));
337347
expect(state.G).toEqual({ A: true });
338348

349+
state = reducer(state, makeMove('nextPhase'));
350+
expect(state.G).toEqual({ A: true });
351+
expect(state.ctx.phase).toEqual('phase2');
352+
expect(state.ctx).toEqual(state._undo[state._undo.length - 1].ctx);
353+
354+
state = reducer(state, makeMove('move', 'B'));
355+
expect(state.G).toEqual({ A: true, B: true });
356+
expect(state.ctx.phase).toEqual('phase2');
357+
expect(state.ctx).toEqual(state._undo[state._undo.length - 1].ctx);
358+
359+
state = reducer(state, undo());
360+
expect(state.G).toEqual({ A: true });
361+
expect(state.ctx.phase).toEqual('phase2');
362+
expect(state.ctx).toEqual(state._undo[state._undo.length - 1].ctx);
363+
364+
state = reducer(state, redo());
365+
expect(state.G).toEqual({ A: true, B: true });
366+
expect(state.ctx.phase).toEqual('phase2');
367+
expect(state.ctx).toEqual(state._undo[state._undo.length - 1].ctx);
368+
369+
state = reducer(state, undo());
370+
expect(state.G).toEqual({ A: true });
371+
expect(state.ctx.phase).toEqual('phase2');
372+
expect(state.ctx).toEqual(state._undo[state._undo.length - 1].ctx);
373+
374+
state = reducer(state, undo());
375+
expect(state.G).toEqual({ A: true });
376+
expect(state.ctx.phase).toEqual('phase1');
377+
expect(state.ctx).toEqual(state._undo[state._undo.length - 1].ctx);
378+
379+
state = reducer(state, redo());
380+
expect(state.G).toEqual({ A: true });
381+
expect(state.ctx.phase).toEqual('phase2');
382+
expect(state.ctx).toEqual(state._undo[state._undo.length - 1].ctx);
383+
384+
state = reducer(state, undo());
339385
state = reducer(state, gameEvent('endTurn'));
340386
state = reducer(state, undo());
341387
expect(state.G).toEqual({ A: true });

0 commit comments

Comments
 (0)