Viewing File: /usr/local/cpanel/base/frontend/jupiter/site-monitor/services/monitoring.spec.js

/*
 * site-monitor/services/monitoring.spec.js         Copyright 2021 cPanel, L.L.C.
 *                                                           All rights reserved.
 * copyright@cpanel.net                                         http://cpanel.net
 * This code is subject to the cPanel license. Unauthorized copying is prohibited
 */

/* global define, PAGE */

define([
    "angular",
    "cjt/core",
    "cjt/util/test",
    "ngMocks",
    "site-monitor/services/monitoring",
  ],
    function() {
        "use strict";
        var service, q, $scope, apiHandler;

        var recentApiCalls = [];

        beforeEach(function() {
            module("cpanel.siteMonitor.monitoringService", function($provide) {
                $provide.value("PAGE", {});
            });

            inject(function($injector) {
                service = $injector.get("monitoring");
                apiHandler = $injector.get("APICatcher");
                q = $injector.get("$q");
                $scope = $injector.get("$rootScope").$new();
            });
        
            $scope.$digest();
        });

        function testApiCalled(apiCall, params, apiNamesOrModule, func, apiParams) {

            apiNamesOrModule = typeof (apiNamesOrModule) === "string" ? [{
                module: apiNamesOrModule,
                func: func,
            }] : apiNamesOrModule;

            var spy = spyOn(apiHandler, "promise");
            spy.and.callFake(function(apiCall) {
                recentApiCalls.push(apiCall);

                // of the remaining calls, is it in there
                for (var i = 0; i < apiNamesOrModule.length; i++) {
                    if (apiNamesOrModule[i].module === apiCall.module && apiNamesOrModule[i].func === apiCall.func) {

                        // limit based on apiParams
                        if (apiParams) {
                            var someNotFound = Object.keys(apiParams).some(function(param) {
                                var value = apiParams[param];
                                return apiCall.args[param] !== value;
                            });
                            if (!someNotFound) {

                                // Remove them when found
                                apiNamesOrModule.splice(i);
                                break;
                            }
                        } else {

                            // Remove them when found
                            apiNamesOrModule.splice(i);
                            break;
                        }

                    }
                }

                return q.resolve({ data: {"response" : "ok" }});
            });

            service[apiCall].apply(service, params).then(function() {

                // Should be empty now
                expect(apiNamesOrModule.length).toBe(0);
            });

            $scope.$digest();
        };

        describe("list", function() {
           var expectedAPICalls = [
                {
                    module: "Monitoring",
                    func: "get_all_site_monitors"
                },
           ];

           it("should call only one API to get monitors", function() {
                testApiCalled("list", null, expectedAPICalls);
           });
        });

        describe("get", function() {
            var expectedAPICalls = [
                {
                    module: "Monitoring",
                    func: "get_site_monitor"
                },
            ];

            it("should call only one API to get single monitor", function() {
                testApiCalled("get", ["123"], expectedAPICalls);
            });

            it("should fail with no argument", function() {
                expect(function(){service.get()}).toThrow();
            });

        });

        describe('enable', function(){
            var expectedAPICalls = [
                {
                    module: "Monitoring",
                    func: "create_site_monitor"
                },
           ];

           it("should call only one API to get domains", function() {
               testApiCalled("enable", ["123"], expectedAPICalls);
           });

            it("should fail with no argument", function() {
                expect(function(){service.enable()}).toThrow();
            });
        });

        describe("disable", function() {
            var expectedAPICalls = [
                {
                    module: "Monitoring",
                    func: "remove_site_monitor"
                },
            ];

            it("should call only one API to get domains", function() {
                testApiCalled("disable", ["123"], expectedAPICalls);
            });

            it("should fail with no argument", function() {
                expect(function(){service.disable()}).toThrow();
            });
        });

});
Back to Directory File Manager