{"size": 3105, "ext": "js", "max_stars_repo_path": "tests/unit/client/controllers/dialogs/questions/rgiDeleteQuestionDialogCtrl.spec.js", "max_stars_repo_name": "NRGI/rgi-assessment-tool", "max_stars_repo_stars_event_min_datetime": "2015-05-18T05:17:28.000Z", "max_stars_repo_stars_event_max_datetime": "2016-01-07T12:27:24.000Z", "max_issues_repo_path": "tests/unit/client/controllers/dialogs/questions/rgiDeleteQuestionDialogCtrl.spec.js", "max_issues_repo_name": "NRGI/rgi-assessment-tool", "max_issues_repo_issues_event_min_datetime": "2015-04-01T17:37:44.000Z", "max_issues_repo_issues_event_max_datetime": "2020-02-10T11:07:50.000Z", "max_forks_repo_path": "tests/unit/client/controllers/dialogs/questions/rgiDeleteQuestionDialogCtrl.spec.js", "max_forks_repo_name": "NRGI/rgi-assessment-tool", "max_forks_repo_forks_event_min_datetime": "2015-04-07T17:46:49.000Z", "max_forks_repo_forks_event_max_datetime": "2017-02-17T23:43:07.000Z"}
{"max_stars_count": 7, "max_issues_count": 394, "max_forks_count": 12, "avg_line_length": 37.4096385542, "max_line_length": 136, "alphanum_fraction": 0.5471819646}
'use strict';

describe('rgiDeleteQuestionDialogCtrl', function () {
    beforeEach(module('app'));

    var $scope, $location, rgiNotifier, rgiQuestionMethodSrvc;

    beforeEach(inject(
        function ($rootScope, $controller, _$location_, _rgiNotifier_, _rgiQuestionMethodSrvc_) {
            $location = _$location_;
            rgiNotifier = _rgiNotifier_;
            rgiQuestionMethodSrvc = _rgiQuestionMethodSrvc_;

            $scope = $rootScope.$new();

            $controller('rgiDeleteQuestionDialogCtrl', {$scope: $scope});
        }
    ));

    describe('#deleteQuestion', function () {
        var questionMethodDeleteQuestionStub, questionMethodDeleteQuestionSpy, notifierMock,
            QUESTION_ID = 'QUESTION_ID';

        beforeEach(function () {
            notifierMock = sinon.mock(rgiNotifier);
            $scope.$parent.question = {_id: QUESTION_ID};
        });

        describe('POSITIVE CASE', function () {
            it('shows message and redirects', function () {
                questionMethodDeleteQuestionSpy = sinon.spy(function () {
                    return {
                        then: function (callback) {
                            callback();
                        }
                    };
                });
                questionMethodDeleteQuestionStub = sinon.stub(rgiQuestionMethodSrvc, 'deleteQuestion', questionMethodDeleteQuestionSpy);

                var $locationMock = sinon.mock($location);
                $scope.closeThisDialog = sinon.spy();

                $locationMock.expects('path').withArgs('/admin/question-admin');
                notifierMock.expects('notify').withArgs('Question has been deleted');

                $scope.deleteQuestion();

                $locationMock.verify();
                $locationMock.restore();
                $scope.closeThisDialog.called.should.be.equal(true);
            });
        });

        describe('NEGATIVE CASE', function () {
            it('shows error message', function () {
                var failureReason = 'REASON';
                questionMethodDeleteQuestionSpy =  sinon.spy(function () {
                    /*jshint unused: true*/
                    /*jslint unparam: true*/
                    return {
                        then: function (uselessCallbackPositive, callbackNegative) {
                            callbackNegative(failureReason);
                        }
                    };
                });
                /*jshint unused: false*/
                /*jslint unparam: false*/
                questionMethodDeleteQuestionStub = sinon.stub(rgiQuestionMethodSrvc, 'deleteQuestion', questionMethodDeleteQuestionSpy);

                notifierMock.expects('error').withArgs(failureReason);
                $scope.deleteQuestion();
            });
        });

        afterEach(function () {
            questionMethodDeleteQuestionSpy.withArgs(QUESTION_ID).called.should.be.equal(true);
            questionMethodDeleteQuestionStub.restore();
            notifierMock.verify();
            notifierMock.restore();
        });
    });
});
