Skip to content

Commit

Permalink
fix bug that was causing ctx.events to be undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Oct 19, 2018
1 parent ea3754b commit 58e135b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/core/reducer.js
Expand Up @@ -188,7 +188,7 @@ export function CreateGameReducer({ game, numPlayers, multiplayer }) {
game,
action.payload.playerID
);
apiCtx.attachToContext(state.ctx);
state.ctx = apiCtx.attachToContext(state.ctx);

let newState = game.flow.processGameEvent(state, action);

Expand Down
49 changes: 33 additions & 16 deletions src/core/reducer.test.js
Expand Up @@ -216,6 +216,39 @@ test('deltalog', () => {
expect(state.deltalog).toEqual([{ action: actionC }]);
});

describe('Events API', () => {
const fn = (G, ctx) => (ctx.events ? {} : { error: true });

const game = Game({
setup: () => ({}),
flow: {
phases: [{ name: 'A' }, { name: 'B' }],
onTurnBegin: fn,
onTurnEnd: fn,
onPhaseBegin: fn,
onPhaseEnd: fn,
onMove: fn,
},
});

const reducer = CreateGameReducer({ game });
let state = reducer(undefined, { type: 'init' });

test('is attached at the beginning', () => {
expect(state.G).not.toEqual({ error: true });
});

test('is attached at the end of turns', () => {
state = reducer(state, gameEvent('endTurn'));
expect(state.G).not.toEqual({ error: true });
});

test('is attached at the end of phases', () => {
state = reducer(state, gameEvent('endPhase'));
expect(state.G).not.toEqual({ error: true });
});
});

describe('Random inside setup()', () => {
const game1 = Game({
seed: 'seed1',
Expand Down Expand Up @@ -257,22 +290,6 @@ describe('Random inside setup()', () => {
});
});

test('events API inside first onTurnBegin', () => {
const game = Game({
flow: {
setActionPlayers: true,
onTurnBegin: (G, ctx) => {
ctx.events.setActionPlayers(['0', '1']);
},
},
});

const reducer = CreateGameReducer({ game });
const state = reducer(undefined, { type: 'init' });

expect(state.ctx.actionPlayers).toEqual(['0', '1']);
});

test('undo / redo', () => {
let game = Game({
moves: {
Expand Down

0 comments on commit 58e135b

Please sign in to comment.