{"size": 5032, "ext": "js", "max_stars_repo_path": "tests/joinLadderHandler.test.js", "max_stars_repo_name": "kjendrzyca/Klesk", "max_stars_repo_stars_event_min_datetime": "2015-07-27T07:43:21.000Z", "max_stars_repo_stars_event_max_datetime": "2015-07-27T07:43:21.000Z", "max_issues_repo_path": "tests/joinLadderHandler.test.js", "max_issues_repo_name": "kjendrzyca/Klesk", "max_issues_repo_issues_event_min_datetime": "2015-07-18T09:44:03.000Z", "max_issues_repo_issues_event_max_datetime": "2015-10-03T17:30:02.000Z", "max_forks_repo_path": "tests/joinLadderHandler.test.js", "max_forks_repo_name": "kjendrzyca/Klesk", "max_forks_repo_forks_event_min_datetime": "2015-07-16T22:49:06.000Z", "max_forks_repo_forks_event_max_datetime": "2015-07-23T16:17:43.000Z"}
{"max_stars_count": 1, "max_issues_count": 24, "max_forks_count": 2, "avg_line_length": 29.0867052023, "max_line_length": 83, "alphanum_fraction": 0.478736089}
'use strict';

import assert from 'assertthat';
import _ from 'lodash';
import sinon from 'sinon';
import joinLadderHandler from '../app/handlers/joinLadderHandler.js';

let parsedCommand = {
    playerName: 'newPlayer',
    arguments: ['joinladder', 'normal']
};

describe('joinLadderHandler', () => {
    it('should join ladder\'s active season if there are other players', () => {
        // given
        let ladderToUpdate = {
            name: 'normal',
            seasons: [
                {
                    number: 1,
                    matches: []
                },
                {
                    number: 2,
                    matches: [
                        {player1: 'anarki', player2: 'sarge', winner: ''},
                        {player1: 'sarge', player2: 'klesk', winner: ''},
                        {player1: 'anarki', player2: 'klesk', winner: ''}
                    ]
                }
            ]
        };

        let fakePersistence = {
            update (filterDelegate, updateDelegate) {
                updateDelegate(ladderToUpdate);
            }
        };
        let handler = joinLadderHandler(fakePersistence);

        // when
        handler.makeItSo(parsedCommand, () => {}, {send: () => {}});

        // then
        let expectedMatches = [
            {player1: 'anarki', player2: 'sarge', winner: ''},
            {player1: 'sarge', player2: 'klesk', winner: ''},
            {player1: 'anarki', player2: 'klesk', winner: ''},
            {player1: 'newPlayer', player2: 'anarki', winner: ''},
            {player1: 'newPlayer', player2: 'sarge', winner: ''},
            {player1: 'newPlayer', player2: 'klesk', winner: ''}
        ];

        assert.that(ladderToUpdate.seasons[0].matches).is.equalTo(expectedMatches);
    });

    it('should join ladder if there is only one other player', () => {
        // given
        let ladderToUpdate = {
            name: 'normal',
            seasons: [
                {
                    matches: [
                        {player1: 'anarki', player2: '', winner: ''}
                    ]
                }
            ]
        };

        let fakePersistence = {
            update (filterDelegate, updateDelegate) {
                updateDelegate(ladderToUpdate);
            }
        };
        let handler = joinLadderHandler(fakePersistence);

        // when
        handler.makeItSo(parsedCommand, () => {}, {send: () => {}});

        // then
        let expectedMatches = [
            {player1: 'anarki', player2: 'newPlayer', winner: ''}
        ];

        assert.that(ladderToUpdate.seasons[0].matches).is.equalTo(expectedMatches);
    });

    it('should join ladder if there are no other players', () => {
        // given
        let ladderToUpdate = {
            name: 'normal',
            seasons: [
                {
                    matches: []
                }
            ]
        };

        let fakePersistence = {
            update (filterDelegate, updateDelegate) {
                updateDelegate(ladderToUpdate);
            }
        };
        let handler = joinLadderHandler(fakePersistence);

        // when
        handler.makeItSo(parsedCommand, () => {}, {send: () => {}});

        // then
        let expectedMatches = [
            {player1: 'newPlayer', player2: '', winner: ''}
        ];

        assert.that(ladderToUpdate.seasons[0].matches).is.equalTo(expectedMatches);
    });

    it('should send notification when user joins the ladder', () => {
        // given
        let ladderToUpdate = {
            name: 'normal',
            seasons: [
                {
                    matches: []
                }
            ]
        };

        let fakePersistence = {
            update (filterDelegate, updateDelegate) {
                updateDelegate(ladderToUpdate);
            }
        };
        let handler = joinLadderHandler(fakePersistence);

        let sendSpy = sinon.spy();

        // when
        handler.makeItSo(parsedCommand, () => {}, {send: sendSpy});

        // then
        let expectedMessage = 'Player `newPlayer` has joined the ladder `normal`';
        assert.that(sendSpy.calledWith(expectedMessage)).is.true();
        assert.that(sendSpy.calledOnce).is.true();
    });

    it('should not join ladder if already joined', () => {
        // given
        let ladderToUpdate = {
            name: 'normal',
            seasons: [
                {
                    matches: [{player1: 'newPlayer', player2: '', winner: ''}]
                }
            ]
        };

        let expectedMatches = _.cloneDeep(ladderToUpdate.matches);

        let fakePersistence = {
            update (filterDelegate, updateDelegate) {
                updateDelegate(ladderToUpdate);
            }
        };
        let handler = joinLadderHandler(fakePersistence);

        // when
        handler.makeItSo(parsedCommand, () => {});

        // then
        assert.that(ladderToUpdate.matches).is.equalTo(expectedMatches);
    });
});
