@@ -22,6 +22,7 @@ describe('AdjustmentsModalController', function() {
2222 this . $q = $injector . get ( '$q' ) ;
2323 this . $rootScope = $injector . get ( '$rootScope' ) ;
2424 this . $controller = $injector . get ( '$controller' ) ;
25+ this . alertService = $injector . get ( 'alertService' ) ;
2526 } ) ;
2627
2728 this . modalDeferred = this . $q . defer ( ) ;
@@ -67,6 +68,7 @@ describe('AdjustmentsModalController', function() {
6768
6869 spyOn ( this . modalDeferred , 'resolve' ) ;
6970 spyOn ( this . modalDeferred , 'reject' ) ;
71+ spyOn ( this . alertService , 'error' ) ;
7072
7173 this . initController = initController ;
7274 } ) ;
@@ -191,6 +193,52 @@ describe('AdjustmentsModalController', function() {
191193 expect ( this . filterReasons ) . toHaveBeenCalledWith ( this . vm . adjustments ) ;
192194 } ) ;
193195
196+ it ( 'should not add adjustment and should show error when quantity is zero' , function ( ) {
197+ this . vm . newAdjustment = {
198+ reason : this . vm . reasons [ 0 ] ,
199+ quantity : 0
200+ } ;
201+
202+ this . vm . addAdjustment ( ) ;
203+
204+ expect ( this . vm . adjustments . length ) . toBe ( 2 ) ;
205+ expect ( this . alertService . error )
206+ . toHaveBeenCalledWith ( 'openlmisAdjustments.quantityGreaterThanZero' ) ;
207+ } ) ;
208+
209+ it ( 'should not add adjustment and should show error when quantity is empty' , function ( ) {
210+ this . vm . newAdjustment = {
211+ reason : this . vm . reasons [ 0 ] ,
212+ quantity : ''
213+ } ;
214+
215+ this . vm . addAdjustment ( ) ;
216+
217+ expect ( this . vm . adjustments . length ) . toBe ( 2 ) ;
218+ expect ( this . alertService . error )
219+ . toHaveBeenCalledWith ( 'openlmisAdjustments.quantityGreaterThanZero' ) ;
220+ } ) ;
221+
222+ it ( 'should not add adjustment and should show error when quantity is negative' , function ( ) {
223+ this . vm . newAdjustment = {
224+ reason : this . vm . reasons [ 0 ] ,
225+ quantity : - 5
226+ } ;
227+
228+ this . vm . addAdjustment ( ) ;
229+
230+ expect ( this . vm . adjustments . length ) . toBe ( 2 ) ;
231+ expect ( this . alertService . error )
232+ . toHaveBeenCalledWith ( 'openlmisAdjustments.quantityGreaterThanZero' ) ;
233+ } ) ;
234+
235+ it ( 'should not show error when quantity is greater than zero' , function ( ) {
236+ this . vm . addAdjustment ( ) ;
237+
238+ expect ( this . vm . adjustments . length ) . toBe ( 3 ) ;
239+ expect ( this . alertService . error ) . not . toHaveBeenCalled ( ) ;
240+ } ) ;
241+
194242 } ) ;
195243
196244 describe ( 'removeAdjustment' , function ( ) {
@@ -297,6 +345,33 @@ describe('AdjustmentsModalController', function() {
297345 expect ( this . modalDeferred . resolve ) . not . toHaveBeenCalled ( ) ;
298346 } ) ;
299347
348+ it ( 'should not resolve modalDeferred and should show error if any adjustment has invalid quantity' ,
349+ function ( ) {
350+ this . adjustments = [ {
351+ reason : this . reasons [ 0 ] ,
352+ quantity : 0
353+ } ] ;
354+
355+ this . initController ( ) ;
356+ this . vm . $onInit ( ) ;
357+
358+ this . vm . save ( ) ;
359+
360+ expect ( this . modalDeferred . resolve ) . not . toHaveBeenCalled ( ) ;
361+ expect ( this . alertService . error )
362+ . toHaveBeenCalledWith ( 'openlmisAdjustments.quantityGreaterThanZero' ) ;
363+ } ) ;
364+
365+ it ( 'should not show error if all adjustments have valid quantities' , function ( ) {
366+ this . initController ( ) ;
367+ this . vm . $onInit ( ) ;
368+
369+ this . vm . save ( ) ;
370+
371+ expect ( this . alertService . error ) . not . toHaveBeenCalled ( ) ;
372+ expect ( this . modalDeferred . resolve ) . toHaveBeenCalledWith ( this . vm . adjustments ) ;
373+ } ) ;
374+
300375 } ) ;
301376
302377 describe ( 'cancel' , function ( ) {
0 commit comments