Skip to content

Commit

Permalink
Add Unit Test
Browse files Browse the repository at this point in the history
Tendrl-bug-id: #911
Signed-off-by: a2batic <[email protected]>
  • Loading branch information
a2batic committed Apr 5, 2018
1 parent baa676c commit 863668a
Show file tree
Hide file tree
Showing 11 changed files with 1,342 additions and 27 deletions.
7 changes: 1 addition & 6 deletions src/modules/clusters/cluster-hosts/cluster-hosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@
}
}

/*Cancelling interval when scope is destroy*/
$scope.$on("$destroy", function() {
$interval.cancel(clusterDetailTimer);
});

/***Private Functions***/

/**
Expand Down Expand Up @@ -81,4 +76,4 @@

}

})();
})();
5 changes: 0 additions & 5 deletions src/modules/clusters/cluster-volumes/cluster-volumes.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@
}
}

/*Cancelling interval when scope is destroy*/
$scope.$on("$destroy", function() {
$interval.cancel(clusterDetailTimer);
});

/***Private Functions***/

/**
Expand Down
21 changes: 6 additions & 15 deletions src/modules/clusters/import-cluster/import-cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@
/*@ngInject*/
function importClusterController($state, $rootScope, $stateParams, $uibModal, clusterStore) {

var vm = this,
hostList;
var vm = this;

vm.filtersText = "";
vm.enableProfiling = "enable";
vm.filterBy = "fqdn";
vm.filterByValue = "Name";
vm.filterPlaceholder = "Name";
vm.taskInitiated = false;
vm.importIcon = false;
vm.failedImport = false;
vm.isDataLoading = true;
vm.hostList = [];
vm.filteredHostList = [];
vm.filters = [];
vm.failedImport = false;
vm.isDataLoading = true;
vm.jobId = "";
vm.importCluster = importCluster;
vm.importCancel = importCancel;
vm.viewTaskProgress = viewTaskProgress;
Expand All @@ -53,7 +50,6 @@
onFilterChange: _filterChange,
};


vm.tableConfig = {
selectionMatchProp: "fqdn",
itemsAvailable: true,
Expand All @@ -63,7 +59,6 @@
vm.tableColumns = [
{ header: "Host", itemField: "fqdn" },
{ header: "Address", itemField: "ipAddress" }

];

init();
Expand Down Expand Up @@ -93,6 +88,7 @@
_setImportDetail();
vm.filteredHostList = vm.hostList;
vm.isDataLoading = false;
_filterChange(vm.filters);
}
}

Expand Down Expand Up @@ -179,10 +175,7 @@
vm.filters = filters;
filters.forEach(function(filter) {
vm.filtersText += filter.title + " : ";
if (filter.value.filterCategory) {
vm.filtersText += ((filter.value.filterCategory.title || filter.value.filterCategory) +
filter.value.filterDelimiter + (filter.value.filterValue.title || filter.value.filterValue));
} else if (filter.value.title) {
if (filter.value.title) {
vm.filtersText += filter.value.title;
} else {
vm.filtersText += filter.value;
Expand All @@ -205,7 +198,6 @@
vm.taskInitiated = true;
vm.jobId = data.job_id;
});

}

/**
Expand All @@ -223,7 +215,6 @@
* @memberOf importClusterController
*/
function viewTaskProgress() {

if (vm.clusterId) {
$state.go("global-task-detail", { clusterId: vm.clusterId, taskId: vm.jobId });
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/users/add-user/add-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
});

/*@ngInject*/
function addUserController($scope, $rootScope, $state, $stateParams, userStore, config, utils, Notifications) {
function addUserController($scope, $rootScope, $state, userStore, config, utils, Notifications) {

var vm = this,
typePassword,
Expand Down
98 changes: 98 additions & 0 deletions test/unit/add-user-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
describe("Unit Component: Add User", function() {
"use strict";

// Angular injectables
var $scope, $q, $httpBackend, $injector, $rootScope, $state, $templateCache, $compile, $interval, $destroy, $componentController, $event, $uibModal;

// Module defined (non-Angular) injectables
var config, utils, userStore, capitalizeFilter, Notifications;

// Local variables used for testing
var createUserDeferred, vm, clock, throttled, intervalSpy, timer, args, element;

// Initialize modules
beforeEach(function() {
module("TendrlModule");
module("TestDataModule");
module("templates");
});

beforeEach(function() {

var templateHtml;

inject(function(_$q_, _$componentController_, _$rootScope_, _$state_, _$templateCache_, _$compile_, _$interval_, _$uibModal_) {
$q = _$q_;
$componentController = _$componentController_;
$rootScope = _$rootScope_;
$state = _$state_;
$templateCache = _$templateCache_;
$compile = _$compile_;
$interval = _$interval_;
$uibModal = _$uibModal_;

$scope = $rootScope.$new();
templateHtml = $templateCache.get("/modules/users/add-user/add-user.html");

element = $compile(templateHtml)($scope);
});

inject(function(_utils_, _config_, _userStore_, _capitalizeFilter_, _Notifications_) {
utils = _utils_;
config = _config_;
userStore = _userStore_;
capitalizeFilter = _capitalizeFilter_;
Notifications = _Notifications_;
});

});

beforeEach(function() {
$state.current.name = "add-user";
createUserDeferred = $q.defer();

sinon.stub($state, "go");

clock = sinon.useFakeTimers();

});

it("Should initialize all the properties", function() {
vm = $componentController("addUser", { $scope: $scope });

expect(vm.typePassword).to.be.false;
expect(vm.confirmPassword).to.be.false;
expect(vm.errorMsg).to.be.equal("");
expect(vm.user).to.be.an("object").that.is.not.empty;
expect(vm.user.notification).to.be.true;
expect(vm.user.role).to.be.equal("admin");
});

describe("Add User List workflows", function() {
beforeEach(function() {
sinon.stub(userStore, "createUser").returns(createUserDeferred.promise);
vm = $componentController("addUser", { $scope: $scope });
$rootScope.$digest();
});

it("Should test toggleTypePassword", function() {
vm.typePassword = false;
vm.toggleTypePassword();
expect(vm.typePassword).to.be.true;
});

it("Should test toggleConfirmPassword", function() {
vm.confirmPassword = false;
vm.toggleConfirmPassword();
expect(vm.confirmPassword).to.be.true;
});

});

afterEach(function() {
// Tear down
$state.go.restore();
clock.restore();
});

});
104 changes: 104 additions & 0 deletions test/unit/cluster-hosts-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
describe("Unit Component: clusterHosts", function() {
"use strict";

// Angular injectables
var $scope, $q, $httpBackend, $injector, $rootScope, $state, $templateCache, $compile, $stateParams, $componentController;

// Module defined (non-Angular) injectables
var config, utils, clusterHosts, Notifications, clusterStore;

// Local variables used for testing
var getClusterListDeferred, vm, clock, throttled, intervalSpy, timer, args, dashboardStub, element;

// Initialize modules
beforeEach(function() {
module("TendrlModule");
module("TestDataModule");
module("templates");
});

beforeEach(function() {

var templateHtml;

inject(function(_$q_, _$componentController_, _$rootScope_, _$state_, _$templateCache_, _$compile_, _$stateParams_) {
$q = _$q_;
$componentController = _$componentController_;
$rootScope = _$rootScope_;
$state = _$state_;
$templateCache = _$templateCache_;
$compile = _$compile_;
$stateParams = _$stateParams_;

$scope = $rootScope.$new();
templateHtml = $templateCache.get("/modules/cluster/cluster-hosts/cluster-hosts.html");

element = $compile(templateHtml)($scope);
});

inject(function(_clusterStore_, _clusterHosts_) {
clusterHosts = _clusterHosts_;
clusterStore = _clusterStore_;
});

});

beforeEach(function() {
$stateParams.clusterId = clusterHosts.formattedOutput[0].clusterId;
$state.current.name = "cluster-hosts";
getClusterListDeferred = $q.defer();

sinon.stub(clusterStore, "getClusterList").returns(getClusterListDeferred.promise);
sinon.stub(clusterStore, "formatClusterData").returns(clusterHosts.formattedOutput);
sinon.stub(clusterStore, "getClusterDetails").returns(clusterHosts.formattedOutput[0]);

});

it("Should initialize all the properties", function() {
vm = $componentController("clusterHosts", { $scope: $scope });

expect(vm.isDataLoading).to.be.true;
expect(vm.clusterId).to.be.equal("e372c01c-5022-41ce-9412-e96038bca305");
expect($rootScope.selectedClusterOption).to.be.equal("e372c01c-5022-41ce-9412-e96038bca305");
});

it("Should call getClusterList if clusterData doens't exit", function() {
vm = $componentController("clusterHosts", { $scope: $scope });
getClusterListDeferred.resolve(clusterHosts.clusters);
$rootScope.$digest();

expect($rootScope.clusterData).to.deep.equal(clusterHosts.formattedOutput);
expect(vm.isDataLoading).to.be.false;
expect(vm.clusterObj).to.deep.equal(clusterHosts.formattedOutput[0]);
expect(vm.clusterName).to.be.equal(clusterHosts.formattedOutput[0].clusterId);
});

it("Should not call getClusterList if clusterData exists", function() {
$rootScope.clusterData = clusterHosts.clusters;
vm = $componentController("clusterHosts", { $scope: $scope });

expect(vm.isDataLoading).to.be.false;
expect(vm.clusterObj).to.deep.equal(clusterHosts.formattedOutput[0]);
expect(vm.clusterName).to.be.equal(clusterHosts.formattedOutput[0].clusterId);

});

it("Should listen GotClusterData event broadcast", function() {

vm = $componentController("clusterHosts", { $scope: $scope });
getClusterListDeferred.resolve(clusterHosts.clusters);
$rootScope.$digest();

$rootScope.clusterData = clusterHosts.formattedOutput;
$scope.$broadcast("GotClusterData");
expect($rootScope.selectedClusterOption).to.be.equal(clusterHosts.formattedOutput[0].clusterId);
});

afterEach(function() {
// Tear down
clusterStore.getClusterList.restore();
clusterStore.formatClusterData.restore();
clusterStore.getClusterDetails.restore();
});

});
Loading

0 comments on commit 863668a

Please sign in to comment.