Commit b278eefa authored by Leonardo Menezes's avatar Leonardo Menezes
Browse files

added tests to RestController

parent 2814afaa
Loading
Loading
Loading
Loading
+6 −7
Original line number Diff line number Diff line
@@ -792,11 +792,11 @@ angular.module('cerebro').controller('OverviewController', ['$scope', '$http', '

  }]);

angular.module('cerebro').controller('RestController', ['$scope', '$http', '$sce', 'DataService', 'AlertService', 'ModalService', 'AceEditorService',
angular.module('cerebro').controller('RestController', ['$scope', '$http', '$sce', 'DataService', 'AlertService',
    'ModalService', 'AceEditorService',
    function ($scope, $http, $sce, DataService, AlertService, ModalService, AceEditorService) {

        $scope.editor = AceEditorService.init('rest-client-editor');
        $scope.editor.setValue("{}");
        $scope.editor = undefined;
        $scope.response = undefined;

        $scope.mappings = undefined;
@@ -820,6 +820,8 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http', '$sce
        };

        $scope.initializeController = function() {
            $scope.editor = AceEditorService.init('rest-client-editor');
            $scope.editor.setValue("{}");
            DataService.getClusterMapping(
                function(response) {
                    $scope.mappings = response;
@@ -833,12 +835,9 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http', '$sce

        $scope.updateOptions = function(text) {
            if ($scope.mappings) {
                var autocomplete = new URLAutocomplete($scope.mappings);
                $scope.options = autocomplete.getAlternatives(text);
                console.log($scope.options);
                $scope.options = new URLAutocomplete($scope.mappings).getAlternatives(text);
            }
        };

    }]);

angular.module('cerebro').directive('ngPagination', ['$document', function($document) {
+6 −7
Original line number Diff line number Diff line
angular.module('cerebro').controller('RestController', ['$scope', '$http', '$sce', 'DataService', 'AlertService', 'ModalService', 'AceEditorService',
angular.module('cerebro').controller('RestController', ['$scope', '$http', '$sce', 'DataService', 'AlertService',
    'ModalService', 'AceEditorService',
    function ($scope, $http, $sce, DataService, AlertService, ModalService, AceEditorService) {

        $scope.editor = AceEditorService.init('rest-client-editor');
        $scope.editor.setValue("{}");
        $scope.editor = undefined;
        $scope.response = undefined;

        $scope.mappings = undefined;
@@ -26,6 +26,8 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http', '$sce
        };

        $scope.initializeController = function() {
            $scope.editor = AceEditorService.init('rest-client-editor');
            $scope.editor.setValue("{}");
            DataService.getClusterMapping(
                function(response) {
                    $scope.mappings = response;
@@ -39,10 +41,7 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http', '$sce

        $scope.updateOptions = function(text) {
            if ($scope.mappings) {
                var autocomplete = new URLAutocomplete($scope.mappings);
                $scope.options = autocomplete.getAlternatives(text);
                console.log($scope.options);
                $scope.options = new URLAutocomplete($scope.mappings).getAlternatives(text);
            }
        };

    }]);
+105 −0
Original line number Diff line number Diff line
describe('RestController', function() {

    beforeEach(angular.mock.module('cerebro'));

    beforeEach(angular.mock.inject(function($rootScope, $controller, $injector) {
        this.scope = $rootScope.$new();
        this.$http = $injector.get('$http');
        this.$sce = $injector.get('$sce');
        this.DataService = $injector.get('DataService');
        this.AlertService = $injector.get('AlertService');
        this.ModalService = $injector.get('ModalService');
        this.AceEditorService = $injector.get('AceEditorService');
        this.createController = function() {
            return $controller('RestController',
                {$scope: this.scope}, this.$http, this.$window, this.DataService, this.AlertService, this.ModalService, this.AceEditorService);
        };
        this._controller = this.createController();
    }));

    it('should have intial state correctly set', function () {
        expect(this.scope.response).toEqual(undefined);
        expect(this.scope.mappings).toEqual(undefined);
        expect(this.scope.method).toEqual("POST");
        expect(this.scope.path).toEqual("");
        expect(this.scope.options).toEqual([]);
    });

    describe('initializeController', function() {
        it('initializes Ace editor', function () {
            var fakeEditor = {setValue: function() {}};
            spyOn(this.AceEditorService, "init").andReturn(fakeEditor);
            this.scope.initializeController();
            expect(this.AceEditorService.init).toHaveBeenCalledWith('rest-client-editor');
            expect(this.scope.editor).toEqual(fakeEditor);
        });

        it('loads mappings', function () {
            var fakeEditor = {setValue: function() {}};
            spyOn(this.AceEditorService, "init").andReturn(fakeEditor);
            this.DataService.getClusterMapping = function(success, error) {
              success({"mappings":"mappingsValue"});
            };
            spyOn(this.DataService, "getClusterMapping").andCallThrough();
            spyOn(this.scope, "updateOptions").andReturn();
            this.scope.initializeController();
            expect(this.DataService.getClusterMapping).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function));
            expect(this.scope.mappings).toEqual({"mappings":"mappingsValue"});
            expect(this.scope.updateOptions).toHaveBeenCalled();
        });

        it('alerts when loading mappings fail', function () {
            var fakeEditor = {setValue: function() {}};
            spyOn(this.AceEditorService, "init").andReturn(fakeEditor);
            this.DataService.getClusterMapping = function(success, error) {
                error("some reason");
            };
            spyOn(this.DataService, "getClusterMapping").andCallThrough();
            spyOn(this.AlertService, "error").andReturn();
            spyOn(this.scope, "updateOptions").andReturn();
            this.scope.initializeController();
            expect(this.scope.updateOptions).not.toHaveBeenCalled();
            expect(this.AlertService.error).toHaveBeenCalledWith('Error while loading cluster mappings', 'some reason');
        });
    });

    describe('execute', function() {
        it('cleans response when executing a request', function () {
            this.scope.response = "{}";
            expect(this.scope.response).toEqual("{}");
            this.scope.editor = {
                getValue: function () {
                    return "";
                }
            };
            spyOn(this.DataService, "getClusterMapping").andReturn();
            this.scope.execute();
            expect(this.scope.response).toEqual(undefined);
        });

        it('uses path, method and body from ace editor', function () {
            this.scope.editor = {
                getValue: function () {
                    return "some value";
                }
            };
            spyOn(this.DataService, "execute").andReturn();
            this.scope.execute();
            expect(this.DataService.execute).toHaveBeenCalledWith("POST", "", "some value", jasmine.any(Function), jasmine.any(Function));
        });
    });

    describe('updateOptions', function() {
        it('loads all possible autocompletion options', function () {
            this.scope.mappings = {};
            this.scope.updateOptions("");
            expect(this.scope.options).toEqual(['_msearch', '_search', '_suggest']);
        });
        it('skip autocompletion if mappings is absent', function () {
            this.scope.mappings = undefined;
            this.scope.updateOptions("");
            expect(this.scope.options).toEqual([]);
        });
    });

});