Skip to content

Commit 2b5920f

Browse files
pgoodrichnicolodavis
authored andcommitted
Add Immer to other events (#327)
* 323 Added Immer for other events * CR update - curry produce * undo flow.test.js * refactor test
1 parent 873e1f5 commit 2b5920f

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

src/core/flow.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { automaticGameEvent } from './action-creators';
1616
import { ContextEnhancer } from './reducer';
1717
import * as logging from './logger';
18+
import produce from 'immer';
1819

1920
/**
2021
* Helper to create a reducer that manages ctx (with the
@@ -310,9 +311,11 @@ export function FlowWithPhases({
310311
if (conf.onPhaseBegin === undefined) {
311312
conf.onPhaseBegin = G => G;
312313
}
314+
conf.onPhaseBegin = produce(conf.onPhaseBegin);
313315
if (conf.onPhaseEnd === undefined) {
314316
conf.onPhaseEnd = G => G;
315317
}
318+
conf.onPhaseEnd = produce(conf.onPhaseEnd);
316319
if (conf.movesPerTurn === undefined) {
317320
conf.movesPerTurn = movesPerTurn;
318321
}
@@ -325,12 +328,15 @@ export function FlowWithPhases({
325328
if (conf.onTurnBegin === undefined) {
326329
conf.onTurnBegin = onTurnBegin;
327330
}
331+
conf.onTurnBegin = produce(conf.onTurnBegin);
328332
if (conf.onTurnEnd === undefined) {
329333
conf.onTurnEnd = onTurnEnd;
330334
}
335+
conf.onTurnEnd = produce(conf.onTurnEnd);
331336
if (conf.onMove === undefined) {
332337
conf.onMove = onMove;
333338
}
339+
conf.onMove = produce(conf.onMove);
334340
if (conf.turnOrder === undefined) {
335341
conf.turnOrder = turnOrder;
336342
}

src/core/flow.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,3 +866,58 @@ describe('endPhase returns to previous phase', () => {
866866
expect(state.ctx.phase).toBe('B');
867867
});
868868
});
869+
870+
describe('immer', () => {
871+
let flow;
872+
let state;
873+
874+
beforeAll(() => {
875+
flow = FlowWithPhases({
876+
startingPhase: 'A',
877+
phases: {
878+
A: {
879+
onPhaseBegin: G => {
880+
G.onPhaseBegin = true;
881+
},
882+
onPhaseEnd: G => {
883+
G.onPhaseEnd = true;
884+
},
885+
},
886+
},
887+
onTurnBegin: G => {
888+
G.onTurnBegin = true;
889+
},
890+
onTurnEnd: G => {
891+
G.onTurnEnd = true;
892+
},
893+
onMove: G => {
894+
G.onMove = true;
895+
},
896+
});
897+
898+
state = { G: {}, ctx: flow.ctx(2) };
899+
state = flow.init(state);
900+
});
901+
902+
test('begin', () => {
903+
expect(state.G.onPhaseBegin).toBe(true);
904+
expect(state.G.onTurnBegin).toBe(true);
905+
expect(state.G.onPhaseEnd).not.toBe(true);
906+
expect(state.G.onTurnEnd).not.toBe(true);
907+
});
908+
909+
test('end turn', () => {
910+
state = flow.processGameEvent(state, gameEvent('endTurn'));
911+
expect(state.G.onTurnEnd).toBe(true);
912+
});
913+
914+
test('end phase', () => {
915+
state = flow.processGameEvent(state, gameEvent('endPhase'));
916+
expect(state.G.onPhaseEnd).toBe(true);
917+
});
918+
919+
test('move', () => {
920+
state = flow.processMove(state, makeMove('A').payload);
921+
expect(state.G.onMove).toBe(true);
922+
});
923+
});

0 commit comments

Comments
 (0)