Skip to content

Commit

Permalink
handle default number of players bigger than 2 for 1st game of the li…
Browse files Browse the repository at this point in the history
…st (#392)
  • Loading branch information
francoijs authored and darthfiddler committed May 4, 2019
1 parent 3982150 commit 8ed812e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
28 changes: 20 additions & 8 deletions src/lobby/create-room-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ class LobbyCreateRoomForm extends React.Component {
numPlayers: 2,
};

constructor(props) {
super(props);
/* fix min and max number of players */
for (let game of props.games) {
let game_details = game.game;
if (!game_details.minPlayers) {
game_details.minPlayers = 1;
}
if (!game_details.maxPlayers) {
game_details.maxPlayers = 4;
}
console.assert(game_details.maxPlayers >= game_details.minPlayers);
}
this.state = {
selectedGame: 0,
numPlayers: props.games[0].game.minPlayers,
};
}

_createGameNameOption = (game, idx) => {
return (
<option key={'name-option-' + idx} value={idx}>
Expand All @@ -37,13 +56,6 @@ class LobbyCreateRoomForm extends React.Component {
};

_createNumPlayersRange = game => {
if (!game.minPlayers) {
game.minPlayers = 1;
}
if (!game.maxPlayers) {
game.maxPlayers = 4;
}
console.assert(game.maxPlayers >= game.minPlayers);
return [...new Array(game.maxPlayers + 1).keys()].slice(game.minPlayers);
};

Expand Down Expand Up @@ -82,7 +94,7 @@ class LobbyCreateRoomForm extends React.Component {
let idx = Number.parseInt(event.target.value);
this.setState({
selectedGame: idx,
numPlayers: this._createNumPlayersRange(this.props.games[idx].game)[0],
numPlayers: this.props.games[idx].game.minPlayers,
});
};

Expand Down
30 changes: 19 additions & 11 deletions src/lobby/react.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ describe('lobby', () => {

beforeEach(async () => {
components = [
{ board: 'Board1', game: { name: 'GameName1' } },
{
board: 'Board2',
game: { name: 'GameName2', minPlayers: 2, maxPlayers: 3 },
board: 'Board1',
game: { name: 'GameName1', minPlayers: 3, maxPlayers: 5 },
},
{ board: 'Board2', game: { name: 'GameName2' } },
{
board: 'Board3',
game: { name: 'GameName3', maxPlayers: 1 },
Expand Down Expand Up @@ -209,6 +209,14 @@ describe('lobby', () => {
lobby.update();
});

test('room with default number of players', () => {
lobby.instance().connection.create = spy;
lobby
.find('LobbyCreateRoomForm')
.find('button')
.simulate('click');
expect(spy).toHaveBeenCalledWith('GameName1', 3);
});
test('room with 2 players', () => {
lobby.instance().connection.create = spy;
lobby
Expand Down Expand Up @@ -245,6 +253,13 @@ describe('lobby', () => {
).not.toBe('');
});
test('when game has no boundaries on the number of players', async () => {
// select 2nd game
lobby
.find('LobbyCreateRoomForm')
.find('select')
.first()
.props()
.onChange({ target: { value: '1' } });
expect(
lobby
.find('LobbyCreateRoomForm')
Expand All @@ -254,20 +269,13 @@ describe('lobby', () => {
).toBe('1234');
});
test('when game has boundaries on the number of players', async () => {
// select 2nd game
lobby
.find('LobbyCreateRoomForm')
.find('select')
.first()
.props()
.onChange({ target: { value: '1' } });
expect(
lobby
.find('LobbyCreateRoomForm')
.find('select')
.at(1)
.text()
).toBe('23');
).toBe('345');
});
});

Expand Down

0 comments on commit 8ed812e

Please sign in to comment.