@@ -23,6 +23,13 @@ describe('AdjustmentsModalController', function() {
2323 this . $rootScope = $injector . get ( '$rootScope' ) ;
2424 this . $controller = $injector . get ( '$controller' ) ;
2525 this . alertService = $injector . get ( 'alertService' ) ;
26+ this . messageService = $injector . get ( 'messageService' ) ;
27+ } ) ;
28+
29+ this . $scope = this . $rootScope . $new ( ) ;
30+
31+ spyOn ( this . messageService , 'get' ) . andCallFake ( function ( key ) {
32+ return key ;
2633 } ) ;
2734
2835 this . modalDeferred = this . $q . defer ( ) ;
@@ -427,8 +434,121 @@ describe('AdjustmentsModalController', function() {
427434
428435 } ) ;
429436
437+ describe ( 'validateAdjustment' , function ( ) {
438+
439+ beforeEach ( function ( ) {
440+ this . initController ( ) ;
441+ } ) ;
442+
443+ it ( 'should clear quantityInvalid when quantity is greater than zero' , function ( ) {
444+ var adjustment = {
445+ quantity : 5 ,
446+ quantityInvalid : 'openlmisAdjustments.quantityGreaterThanZero'
447+ } ;
448+
449+ this . vm . validateAdjustment ( adjustment ) ;
450+
451+ expect ( adjustment . quantityInvalid ) . toBeUndefined ( ) ;
452+ } ) ;
453+
454+ it ( 'should set quantityInvalid message when quantity is zero' , function ( ) {
455+ var adjustment = {
456+ quantity : 0
457+ } ;
458+
459+ this . vm . validateAdjustment ( adjustment ) ;
460+
461+ expect ( adjustment . quantityInvalid ) . toBe ( 'openlmisAdjustments.quantityGreaterThanZero' ) ;
462+ } ) ;
463+
464+ it ( 'should set quantityInvalid message when quantity is negative' , function ( ) {
465+ var adjustment = {
466+ quantity : - 5
467+ } ;
468+
469+ this . vm . validateAdjustment ( adjustment ) ;
470+
471+ expect ( adjustment . quantityInvalid ) . toBe ( 'openlmisAdjustments.quantityGreaterThanZero' ) ;
472+ } ) ;
473+
474+ it ( 'should set quantityInvalid message when quantity is empty' , function ( ) {
475+ var adjustment = {
476+ quantity : ''
477+ } ;
478+
479+ this . vm . validateAdjustment ( adjustment ) ;
480+
481+ expect ( adjustment . quantityInvalid ) . toBe ( 'openlmisAdjustments.quantityGreaterThanZero' ) ;
482+ } ) ;
483+
484+ it ( 'should set quantityInvalid message when quantity is undefined' , function ( ) {
485+ var adjustment = { } ;
486+
487+ this . vm . validateAdjustment ( adjustment ) ;
488+
489+ expect ( adjustment . quantityInvalid ) . toBe ( 'openlmisAdjustments.quantityGreaterThanZero' ) ;
490+ } ) ;
491+
492+ it ( 'should set quantityInvalid message when quantity is not a number' , function ( ) {
493+ var adjustment = {
494+ quantity : 'abc'
495+ } ;
496+
497+ this . vm . validateAdjustment ( adjustment ) ;
498+
499+ expect ( adjustment . quantityInvalid ) . toBe ( 'openlmisAdjustments.quantityGreaterThanZero' ) ;
500+ } ) ;
501+
502+ } ) ;
503+
504+ describe ( 'save marking' , function ( ) {
505+
506+ it ( 'should mark every invalid adjustment when saving' , function ( ) {
507+ var message = 'openlmisAdjustments.quantityGreaterThanZero' ;
508+
509+ this . adjustments = [ {
510+ reason : this . reasons [ 0 ] ,
511+ quantity : 0
512+ } , {
513+ reason : this . reasons [ 1 ] ,
514+ quantity : 5
515+ } , {
516+ reason : this . reasons [ 2 ] ,
517+ quantity : ''
518+ } ] ;
519+
520+ this . initController ( ) ;
521+ this . vm . $onInit ( ) ;
522+
523+ this . vm . save ( ) ;
524+
525+ expect ( this . vm . adjustments [ 0 ] . quantityInvalid ) . toBe ( message ) ;
526+ expect ( this . vm . adjustments [ 1 ] . quantityInvalid ) . toBeUndefined ( ) ;
527+ expect ( this . vm . adjustments [ 2 ] . quantityInvalid ) . toBe ( message ) ;
528+ } ) ;
529+
530+ it ( 'should broadcast openlmis-form-submit when saving invalid adjustments' , function ( ) {
531+ spyOn ( this . $scope , '$broadcast' ) . andCallThrough ( ) ;
532+
533+ this . adjustments = [ {
534+ reason : this . reasons [ 0 ] ,
535+ quantity : 0
536+ } ] ;
537+
538+ this . initController ( ) ;
539+ this . vm . $onInit ( ) ;
540+
541+ this . vm . save ( ) ;
542+
543+ expect ( this . $scope . $broadcast ) . toHaveBeenCalledWith ( 'openlmis-form-submit' ) ;
544+ } ) ;
545+
546+ } ) ;
547+
430548 function initController ( ) {
431549 this . vm = this . $controller ( 'AdjustmentsModalController' , {
550+ $scope : this . $scope ,
551+ messageService : this . messageService ,
432552 modalDeferred : this . modalDeferred ,
433553 adjustments : this . adjustments ,
434554 reasons : this . reasons ,
0 commit comments