Skip to content

Commit 579014d

Browse files
Robert LeggettRobert Leggett
authored andcommitted
Added conditional checks in controller for better testing
Added angularjs tests for CustomerController
1 parent 5c47d0f commit 579014d

2 files changed

Lines changed: 136 additions & 15 deletions

File tree

src/main/webapp/WEB-INF/resources/js/custom/controller.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,31 @@ app.controller('CustomerController', function ($scope, CustomerService) {
1919

2020
$scope.delete = function(id) {
2121
CustomerService.deleteCustomer(id).then(function(response) {
22-
angular.forEach($scope.customers, function(customer, index) {
23-
if(id == customer.id) {
24-
$scope.customers.splice(index, 1);
22+
if(response) {
23+
angular.forEach($scope.customers, function (customer, index) {
24+
if (id == customer.id) {
25+
$scope.customers.splice(index, 1);
2526

26-
console.info("Customer " + id + " has been deleted.")
27-
}
28-
});
27+
console.info("Customer " + id + " has been deleted.")
28+
}
29+
});
30+
}
31+
else {
32+
console.error("Customer " + id + " was unable to be deleted.")
33+
}
2934
});
3035
};
3136

3237
$scope.save = function(id) {
3338
angular.forEach($scope.customers, function(customer, index) {
3439
if(id == customer.id) {
3540
CustomerService.saveCustomer(customer).then(function(response) {
36-
console.info("Customer " + id + " has been saved.")
41+
if(response) {
42+
console.info("Customer " + id + " has been saved.")
43+
}
44+
else {
45+
console.error("Customer " + id + " was unable to be saved.")
46+
}
3747
});
3848
}
3949
});

src/test/webapp/WEB-INF/resources/js/custom/controller.spec.js

Lines changed: 119 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe('CustomerController Tests', function() {
2-
var $rootScope, $controller, mockCustomerService, deferred, spyPromise;
2+
var $rootScope, $controller, mockCustomerService, deferred, spyPromise, customerData;
33

44
beforeEach(function() { module('app'); });
55

@@ -17,6 +17,14 @@ describe('CustomerController Tests', function() {
1717

1818
// initialize controller
1919
$controller = _$controller_('CustomerController', {'$scope': $rootScope, CustomerService : mockCustomerService });
20+
21+
// define initial customer data
22+
customerData = [
23+
{"id": 1, "firstName": "Foo", "lastName": "Bar"},
24+
{"id": 2, "firstName": "Jim", "lastName": "Sunny"},
25+
{"id": 3, "firstName": "Peter", "lastName": "Prone"},
26+
{"id": 4, "firstName": "Sam", "lastName": "Sully"}
27+
];
2028
})
2129
);
2230

@@ -49,19 +57,122 @@ describe('CustomerController Tests', function() {
4957
it('should have scope variable customers populated', function () {
5058
$rootScope.init();
5159

52-
deferred.resolve([
53-
{"id": 1, "firstName": "Foo", "lastName": "Bar"},
54-
{"id": 2, "firstName": "Jim", "lastName": "Sunny"},
55-
{"id": 3, "firstName": "Peter", "lastName": "Prone"},
56-
{"id": 4, "firstName": "Sam", "lastName": "Sully"}
57-
]);
60+
deferred.resolve(customerData);
5861

5962
$rootScope.$apply();
6063

61-
expect($rootScope.customers).toEqual([ { id : 1, firstName : 'Foo', lastName : 'Bar' }, { id : 2, firstName : 'Jim', lastName : 'Sunny' }, { id : 3, firstName : 'Peter', lastName : 'Prone' }, { id : 4, firstName : 'Sam', lastName : 'Sully' } ]);
64+
expect($rootScope.customers).toEqual(customerData);
6265
expect(mockCustomerService.getCustomers).toHaveBeenCalled();
6366
});
6467
});
68+
69+
describe('CustomerController Save Tests', function() {
70+
71+
beforeEach(function() {
72+
spyOn(console, 'error');
73+
74+
$rootScope.customers = customerData;
75+
76+
//setup the spy to always return the spyPromise for the entire test spec
77+
mockCustomerService.saveCustomer.and.returnValue(spyPromise);
78+
});
79+
80+
it('should initially has customers populated before any calls are made', function(){
81+
expect($rootScope.customers).toEqual(customerData);
82+
});
83+
84+
it('should have not saved any data with service call returning true', function () {
85+
$rootScope.save(2);
86+
87+
deferred.resolve(true);
88+
89+
$rootScope.$apply();
90+
91+
expect($rootScope.customers).toEqual(customerData);
92+
expect(mockCustomerService.saveCustomer).toHaveBeenCalled();
93+
expect(console.error).not.toHaveBeenCalled();
94+
});
95+
96+
it('should have not saved any data with service call returning false', function () {
97+
$rootScope.save(2);
98+
99+
deferred.resolve(false);
100+
101+
$rootScope.$apply();
102+
103+
expect($rootScope.customers).toEqual(customerData);
104+
expect(mockCustomerService.saveCustomer).toHaveBeenCalled();
105+
expect(console.error).toHaveBeenCalled();
106+
});
107+
108+
it('should have not saved any data with customers undefined', function () {
109+
$rootScope.customers = undefined;
110+
111+
$rootScope.save(2);
112+
113+
deferred.resolve(false);
114+
115+
$rootScope.$apply();
116+
117+
expect($rootScope.customers).toEqual(undefined);
118+
expect(mockCustomerService.saveCustomer).not.toHaveBeenCalled();
119+
expect(console.error).not.toHaveBeenCalled();
120+
});
121+
});
122+
123+
describe('CustomerController Delete Tests', function() {
124+
125+
beforeEach(function() {
126+
spyOn(console, 'error');
127+
128+
$rootScope.customers = customerData;
129+
130+
//setup the spy to always return the spyPromise for the entire test spec
131+
mockCustomerService.deleteCustomer.and.returnValue(spyPromise);
132+
});
133+
134+
it('should initially has customers populated before any calls are made', function(){
135+
expect($rootScope.customers).toEqual(customerData);
136+
});
137+
138+
it('should have scope variable customers populated with deleted customer removed', function () {
139+
$rootScope.delete(2);
140+
141+
deferred.resolve(true);
142+
143+
$rootScope.$apply();
144+
145+
expect($rootScope.customers).toEqual([ { id : 1, firstName : 'Foo', lastName : 'Bar' }, { id : 3, firstName : 'Peter', lastName : 'Prone' }, { id : 4, firstName : 'Sam', lastName : 'Sully' } ]);
146+
expect(mockCustomerService.deleteCustomer).toHaveBeenCalled();
147+
expect(console.error).not.toHaveBeenCalled();
148+
});
149+
150+
it('should have not deleted any data with service call returning false', function () {
151+
$rootScope.delete(2);
152+
153+
deferred.resolve(false);
154+
155+
$rootScope.$apply();
156+
157+
expect($rootScope.customers).toEqual(customerData);
158+
expect(mockCustomerService.deleteCustomer).toHaveBeenCalled();
159+
expect(console.error).toHaveBeenCalled();
160+
});
161+
162+
it('should have not deleted any data with customers undefined', function () {
163+
$rootScope.customers = undefined;
164+
165+
$rootScope.delete(2);
166+
167+
deferred.resolve(false);
168+
169+
$rootScope.$apply();
170+
171+
expect($rootScope.customers).toEqual(undefined);
172+
expect(mockCustomerService.deleteCustomer).toHaveBeenCalled();
173+
expect(console.error).toHaveBeenCalled();
174+
});
175+
});
65176
});
66177

67178
describe('MainController Tests', function() {

0 commit comments

Comments
 (0)