boardgame.io
Version:
library for turn-based games
145 lines (124 loc) • 3.09 kB
JavaScript
/*
* Copyright 2018 The boardgame.io Authors
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
import PluginPlayer from './plugin-player';
import { Client } from '../client/client';
describe('default values', () => {
test('playerState is not passed', () => {
const plugin = PluginPlayer();
const game = {
plugins: [plugin],
};
const client = Client({ game });
expect(client.getState().plugins[plugin.name].data).toEqual({
players: { '0': {}, '1': {} },
});
});
test('playerState is passed', () => {
const plugin = PluginPlayer({ setup: () => ({ A: 1 }) });
const game = {
plugins: [plugin],
};
const client = Client({ game });
expect(client.getState().plugins[plugin.name].data).toEqual({
players: { '0': { A: 1 }, '1': { A: 1 } },
});
});
});
describe('2 player game', () => {
let client;
beforeAll(() => {
const game = {
moves: {
A: (_, ctx) => {
ctx.player.set({ field: 'A1' });
ctx.player.opponent.set({ field: 'A2' });
},
B: (G, ctx) => {
G.playerValue = ctx.player.get().field;
G.opponentValue = ctx.player.opponent.get().field;
},
},
plugins: [PluginPlayer()],
};
client = Client({ game });
});
test('player 0 turn', () => {
client.moves.A();
expect(client.getState().plugins[PluginPlayer().name].data).toEqual({
players: {
'0': { field: 'A1' },
'1': { field: 'A2' },
},
});
});
test('player 1 turn', () => {
client.events.endTurn();
client.moves.A();
expect(client.getState().plugins[PluginPlayer().name].data).toEqual({
players: {
'0': { field: 'A2' },
'1': { field: 'A1' },
},
});
});
test('player 1 makes move B', () => {
client.moves.B();
expect(client.getState().G).toEqual({
playerValue: 'A1',
opponentValue: 'A2',
});
});
});
describe('3 player game', () => {
let client;
beforeAll(() => {
const game = {
moves: {
A: (_, ctx) => {
ctx.player.set({ field: 'A' });
},
},
plugins: [PluginPlayer()],
};
client = Client({ game, numPlayers: 3 });
});
test('G.opponent is not created', () => {
client.moves.A();
expect(client.getState().plugins[PluginPlayer().name].data).toEqual({
players: {
'0': { field: 'A' },
'1': {},
'2': {},
},
});
});
});
describe('game with phases', () => {
let client;
beforeAll(() => {
const game = {
plugins: [PluginPlayer({ setup: id => ({ id }) })],
phases: {
phase: {},
},
};
client = Client({ game });
});
test('includes playerSetup state', () => {
expect(client.getState().plugins[PluginPlayer().name].data).toEqual({
players: {
0: {
id: '0',
},
1: {
id: '1',
},
},
});
});
});