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

/*
# site-monitor/directives/siteItemDirective.spec.js
#                                                     Copyright(c) 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: false, describe: false, it: false, module: false, inject: false, beforeEach: false, expect: false */


define(["angular",
    "jquery",
    "cjt/core",
    "ngResource",
    "ngMocks",
    "site-monitor/directives/siteItemDirective"], function(angular, $) {
    "use strict";

    var updateCallback;
    var itemLister = angular.module("cpanel.siteMonitor.itemListerDirective", []);
    itemLister.directive("itemLister", function() {
        return {
            restrict: "EA",
            transclude: true,
            replace: false,
            template: "<div class='fake-item-lister'><div ng-transclude></div></div>",
            controller: ["$scope", function($scope) {
                /**
                 *
                 * @param {*} callback
                 */
                this.registerViewCallback = function(callback) {
                    updateCallback = callback;
                };

                /**
                 *
                 * @param {*} callback
                 */
                this.deregisterViewCallback = function(callback) {
                    for (var i = $scope.viewCallbacks.length - 1; i >= 0; i--) {
                        if ($scope.viewCallbacks[i] === callback) {
                            $scope.viewCallbacks.splice(i, 1);
                        }
                    }
                };

                /**
                 *
                 * @returns
                 */
                this.getHeaders = function() {
                    return [
                        { label: "label" },
                        { label: "label" },
                        { label: "label" },
                        { label: "label" },
                        { label: "label" },
                        { label: "label" }
                    ];
                };
            }]
        };
    });

    describe("<site-item>", function() {
        var $scope, $rootScope, $compile;

        beforeEach(function() {
            module(function($provide) {
                $provide.value("ITEM_LISTER_CONSTANTS", {
                    ITEM_CLICKED_EVENT: "aaaaaaaaaaaaaaaaa"
                });

                $provide.value("alertService", {
                    add: function() {
                        return 1;
                    }
                });
            });

            module("cpanel.siteMonitor.itemListerDirective");
            module("directives/siteItemDirective.phtml");
            module("directives/docrootDirective.phtml");
            module("directives/monitorsDirective.phtml");
            module("cpanel.siteMonitor.siteItemDirective");

        });

        beforeEach(inject(function(_$rootScope_, _$compile_) {
            $compile = _$compile_;
            $rootScope = _$rootScope_;
        }));

        /**
         * Compile the template into a directive object.
         *
         * @param {Object} scope
         * @returns {Directive}
         */
        function compileDirective(scope) {

            $scope = $rootScope.$new();
            angular.extend($scope, {}, scope);

            var $directive = $compile(
                "<div> \
                    <item-lister> \
                        <site-item> </site-item> \
                    </item-lister> \
                </div>")($scope);
            $scope.$digest();

            return $directive;
        }

        it("should generate a row for each item", function() {
            var scope = {};
            var $directive = compileDirective(scope);

            expect($directive.find("tr").length).toBe(0);

            updateCallback([
                { domain: "kermit.tld" }
            ]);

            $scope.$digest();

            expect($directive.find("tr").length).toBe(1);

            updateCallback([
                { domain: "kermit.tld" },
                { domain: "gonzo.tld" },
            ]);

            $scope.$digest();

            expect($directive.find("tr").length).toBe(2);

            updateCallback([
                { domain: "gonzo.tld" }
            ]);

            $scope.$digest();

            expect($directive.find("tr").length).toBe(1);

        });

        it("should generate rows containing each domain", function() {
            var scope = {};
            var $directive = compileDirective(scope);

            var sites = [
                { domain: "kermit.tld", docroot: "/a/home/muppets/kermit", homedir: "/a/home" },
                { domain: "fozzie.tld", docroot: "/a/home/muppets/fozzie", homedir: "/a/home" }
            ];

            updateCallback(sites);

            $scope.$digest();

            $directive.find("tr td:nth-child(2)").each(function(index, td) {
                expect($(td).text()).toMatch(sites[index].domain);
            });

        });

    });
});
Back to Directory File Manager