Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
tested join action via tryJoin
  • Loading branch information
vikkio88 committed Feb 5, 2020
1 parent 1634818 commit 63bec42
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions server/test/Room.test.js
Expand Up @@ -13,7 +13,8 @@ describe('Room class tests', () => {
const getGameLogic = () => ({});
const conf = {
maxPlayers: 2,
idGenerator: () => 'roomId'
idGenerator: () => 'roomId',
verbose: false
};

const factory = ({ creator = generateClient(), gameLogic = getGameLogic(), config = conf }) => {
Expand All @@ -25,6 +26,7 @@ describe('Room class tests', () => {
const roomInstance = factory({ creator });
expect(roomInstance.id).to.be.equal('roomId');
expect(roomInstance.maxPlayers).to.be.equal(2);
expect(roomInstance.verbose).to.be.equal(conf.verbose);
expect(creator.emit).to.have.been.calledTwice;
expect(creator.emit.getCall(0).args).to.deep.equal([
MESSAGES.STATE_UPDATE,
Expand All @@ -37,17 +39,39 @@ describe('Room class tests', () => {
});
});

describe('client actions', () => {
it('should allow clients to join, until reaches max client', () => {
describe('joining actions', () => {
it('should let clients join via tryJoin under the right conditions', () => {
const creator = generateClient();
const joiner1 = generateClient({ id: 'joiner1' });
const joiner2 = generateClient({ id: 'joiner2' });
const cannotJoiner = generateClient({ id: 'cannotJoinerId' });

const room = factory({ creator, config: { maxPlayers: 3 } });
const room = factory({ creator, config: { ...conf, maxPlayers: 3 } });
room.onJoin = sinon.spy();
let joined = room.tryJoin(joiner1);
expect(joined).to.be.true;
expect(creator.emit).to.have.been.calledThrice;
expect(joiner1.emit).to.have.been.calledOnce;
expect(room.has(joiner1)).to.be.true;

joined = room.tryJoin(joiner2);
expect(joined).to.be.true;
expect(creator.emit.callCount).to.be.equal(4);
expect(joiner1.emit).to.have.been.calledTwice;
expect(joiner2.emit).to.have.been.calledOnce;
expect(room.has(joiner2)).to.be.true;

joined = room.tryJoin(cannotJoiner);
expect(joined).to.be.false;
expect(creator.emit.callCount).to.be.equal(4);
expect(joiner1.emit).to.have.been.calledTwice;
expect(joiner2.emit).to.have.been.calledOnce;
expect(cannotJoiner.emit).to.have.been.calledWith(MESSAGES.ERROR, 'Cannot join room roomId');
expect(room.has(cannotJoiner)).to.be.false;

expect(room.onJoin).to.have.been.calledTwice;
expect(room.onJoin.getCall(0).args).to.deep.equal([{ joinerId: joiner1.id }]);
expect(room.onJoin.getCall(1).args).to.deep.equal([{ joinerId: joiner2.id }]);
});
})
});

0 comments on commit 63bec42

Please sign in to comment.