11describe ( '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
67178describe ( 'MainController Tests' , function ( ) {
0 commit comments