'use strict';

var Promise = require('bluebird');
var requestPromise = require('request-promise');

var commentOnClosedIssue = require('../../lib/commentOnClosedIssue');
var RepositorySettings = require('../../lib/RepositorySettings');

describe('commentOnClosedIssue', function () {
    var repositorySettings = new RepositorySettings();
    repositorySettings.contributorsPath = 'CONTRIBUTORS.md';
    var commonOptions;

    var issueEventJson;
    var pullRequestEventJson;
    var issueUrl = 'issueUrl';
    var commentsUrl = 'commentsUrl';
    var isMergedUrl = issueUrl + '/merge';
    var userName = 'Joan';

    beforeEach(function () {
        commonOptions = {
            url: issueUrl,
            commentsUrl: commentsUrl,
            repositorySettings: repositorySettings,
            isPullRequest: true,
            userName: userName
        };

        issueEventJson = {
            issue: {
                url: issueUrl,
                comments_url: commentsUrl
            }
        };

        pullRequestEventJson = {
            pull_request: {
                url: issueUrl,
                comments_url: commentsUrl,
                user: {
                    login: userName
                },
                base: {
                    ref: 'master',
                    repo: {
                    }
                }
            }
        };
    });

    it('throws if body is undefined', function () {
        expect(function () {
            commentOnClosedIssue(undefined, repositorySettings);
        }).toThrowError();
    });

    it('throws if `repositorySettings` is undefined', function () {
        expect(function () {
            commentOnClosedIssue(issueEventJson);
        }).toThrowError();
    });

    it('rejects with unknown body type', function (done) {
        commentOnClosedIssue({}, repositorySettings)
            .then(done.fail)
            .catch(function (error) {
                expect(error.message).toBe('Unknown body type');
                done();
            });
    });

    it('passes expected parameters to implementation for closed issue', function () {
        var isPullRequest = false;

        spyOn(commentOnClosedIssue, '_implementation');
        commentOnClosedIssue(issueEventJson, repositorySettings);
        expect(commentOnClosedIssue._implementation).toHaveBeenCalledWith({
            url: issueUrl,
            commentsUrl: commentsUrl,
            isPullRequest: isPullRequest,
            repositorySettings: repositorySettings
        });
    });

    it('passes expected parameters to implementation for closed pull request', function () {
        var isPullRequest = true;

        spyOn(commentOnClosedIssue, '_implementation');
        commentOnClosedIssue(pullRequestEventJson, repositorySettings);
        expect(commentOnClosedIssue._implementation).toHaveBeenCalledWith({
            url: issueUrl,
            commentsUrl: commentsUrl,
            isPullRequest: isPullRequest,
            userName: userName,
            repositorySettings: repositorySettings
        });
    });

    it('commentOnClosedIssue._implementation rejects if issueUrl cannot be retrieved.', function (done) {
        var commentsJson = [];

        spyOn(repositorySettings, 'fetchSettings').and.callFake(function() {
            return Promise.resolve(repositorySettings);
        });
        spyOn(requestPromise, 'get').and.callFake(function (options) {
            if (options.url === issueUrl) {
                return Promise.reject('Bad request');
            }
            if (options.url === commentsUrl) {
                return Promise.resolve(commentsJson);
            }

            return Promise.reject('Unknown url.');
        });
        spyOn(requestPromise, 'post');

        var options = {
            url: issueUrl,
            commentsUrl: commentsUrl,
            repositorySettings: repositorySettings
        };

        commentOnClosedIssue._implementation(options)
            .then(done.fail)
            .catch(function (error) {
                expect(error).toEqual('Bad request');
                expect(requestPromise.post).not.toHaveBeenCalled();
                done();
            });
    });

    it('commentOnClosedIssue._implementation propagates unexpected errors.', function (done) {
        spyOn(repositorySettings, 'fetchSettings').and.callFake(function() {
            return Promise.resolve(repositorySettings);
        });
        spyOn(requestPromise, 'get').and.callFake(function (options) {
            if (options.url === issueUrl) {
                return Promise.resolve(pullRequestEventJson);
            }
            if (options.url === isMergedUrl) {
                return Promise.reject('Unexpected Error');
            }

            if (options.url === commentsUrl) {
                return Promise.resolve([]);
            }

            return Promise.reject('Unknown url: ' + options.url);
        });
        spyOn(requestPromise, 'post');

        commentOnClosedIssue._implementation(commonOptions)
            .then(done.fail)
            .catch(function() {
                done();
            });
    });
});

@@ -3084,1137 +3084,8 @@
 );%0A%0A
-    it('commentOnClosedIssue._implementation rejects if issueUrl cannot be retrieved.', function (done) %7B%0A        var commentsJson = %5B%5D;%0A%0A        spyOn(repositorySettings, 'fetchSettings').and.callFake(function() %7B%0A            return Promise.resolve(repositorySettings);%0A        %7D);%0A        spyOn(requestPromise, 'get').and.callFake(function (options) %7B%0A            if (options.url === issueUrl) %7B%0A                return Promise.reject('Bad request');%0A            %7D%0A            if (options.url === commentsUrl) %7B%0A                return Promise.resolve(commentsJson);%0A            %7D%0A%0A            return Promise.reject('Unknown url.');%0A        %7D);%0A        spyOn(requestPromise, 'post');%0A%0A        var options = %7B%0A            url: issueUrl,%0A            commentsUrl: commentsUrl,%0A            repositorySettings: repositorySettings%0A        %7D;%0A%0A        commentOnClosedIssue._implementation(options)%0A            .then(done.fail)%0A            .catch(function (error) %7B%0A                expect(error).toEqual('Bad request');%0A                expect(requestPromise.post).not.toHaveBeenCalled();%0A                done();%0A            %7D);%0A    %7D);%0A%0A
     

remove old spec