33describe ( 'Unit tests for challenge host team controller' , function ( ) {
44 beforeEach ( angular . mock . module ( 'evalai' ) ) ;
55
6- var $controller , createController , $injector , $mdDialog , $rootScope , $state , $scope , loaderService , utilities , $http , $compile , vm ;
6+ var $controller , createController , $injector , $mdDialog , $rootScope , $state , $scope , loaderService , utilities , $http , $compile , vm ;
77
8- beforeEach ( inject ( function ( _$controller_ , _$injector_ , _$mdDialog_ , _$rootScope_ , _$state_ , _utilities_ , _loaderService_ , _$http_ , _$compile_ ) {
8+ beforeEach ( inject ( function ( _$controller_ , _$injector_ , _$mdDialog_ , _$rootScope_ , _$state_ , _utilities_ , _loaderService_ , _$http_ , _$compile_ ) {
99 $controller = _$controller_ ;
1010 $injector = _$injector_ ;
1111 $rootScope = _$rootScope_ ;
@@ -18,7 +18,7 @@ describe('Unit tests for challenge host team controller', function () {
1818
1919 $scope = $rootScope . $new ( ) ;
2020 createController = function ( ) {
21- return $controller ( 'ChallengeHostTeamsCtrl' , { $scope : $scope } ) ;
21+ return $controller ( 'ChallengeHostTeamsCtrl' , { $scope : $scope } ) ;
2222 } ;
2323 vm = $controller ( 'ChallengeHostTeamsCtrl' , { $scope : $scope } ) ;
2424 } ) ) ;
@@ -110,7 +110,8 @@ describe('Unit tests for challenge host team controller', function () {
110110
111111 hostTeamList . forEach ( response => {
112112 it ( 'when pagination next is ' + response . next + 'and previous is ' + response . previous + '\
113- `hosts/challenge_host_team/`' , function ( ) { ;
113+ `hosts/challenge_host_team/`' , function ( ) {
114+ ;
114115 success = true ;
115116 successResponse = response ;
116117 vm = createController ( ) ;
@@ -156,10 +157,10 @@ describe('Unit tests for challenge host team controller', function () {
156157 it ( 'to load data with pagination `load` function' , function ( ) {
157158 success = true ;
158159 successResponse = {
159- // host team pagination response
160+
160161 next : 'page=4' ,
161162 previous : 'page=2' ,
162- } ;
163+ } ;
163164 vm = createController ( ) ;
164165 spyOn ( vm , 'startLoader' ) ;
165166 spyOn ( $http , 'get' ) . and . callFake ( function ( ) {
@@ -173,7 +174,7 @@ describe('Unit tests for challenge host team controller', function () {
173174 var headers = {
174175 'Authorization' : "Token " + utilities . getData ( 'userKey' )
175176 } ;
176- expect ( $http . get ) . toHaveBeenCalledWith ( url , { headers : headers } ) ;
177+ expect ( $http . get ) . toHaveBeenCalledWith ( url , { headers : headers } ) ;
177178 } ) ;
178179 } ) ;
179180
@@ -185,7 +186,7 @@ describe('Unit tests for challenge host team controller', function () {
185186 } ;
186187 var errorResponse = {
187188 error : 'error'
188- } ;
189+ } ;
189190
190191 beforeEach ( function ( ) {
191192 spyOn ( $rootScope , 'notify' ) ;
@@ -361,7 +362,7 @@ describe('Unit tests for challenge host team controller', function () {
361362 it ( 'create new host team when pagination next is ' + response . next + 'and previous is ' + response . previous , function ( ) {
362363 success = true ;
363364 successResponse = response ;
364-
365+
365366 vm . createNewTeam ( ) ;
366367 expect ( vm . isExistLoader ) . toEqual ( true ) ;
367368 expect ( vm . loaderTitle ) . toEqual ( '' ) ;
@@ -414,7 +415,7 @@ describe('Unit tests for challenge host team controller', function () {
414415 } ) ;
415416 } ) ;
416417
417- it ( 'open dialog to confirm delete' , function ( ) {
418+ it ( 'open dialog to confirm delete' , function ( ) {
418419 var hostTeamId = 1 ;
419420 var ev = new Event ( '$click' ) ;
420421 var confirm = $mdDialog . confirm ( )
@@ -427,17 +428,89 @@ describe('Unit tests for challenge host team controller', function () {
427428 vm . confirmDelete ( ev , hostTeamId ) ;
428429 expect ( $mdDialog . show ) . toHaveBeenCalledWith ( confirm ) ;
429430 } ) ;
431+
432+ it ( 'should remove self from host team successfully and update team list' , function ( done ) {
433+ var hostTeamId = 1 ;
434+ var ev = new Event ( '$click' ) ;
435+
436+ $mdDialog . show . and . returnValue ( Promise . resolve ( ) ) ;
437+
438+ spyOn ( utilities , 'sendRequest' ) . and . callFake ( function ( params ) {
439+ if ( params . method === 'DELETE' ) {
440+ params . callback . onSuccess ( ) ;
441+ } else if ( params . method === 'GET' ) {
442+ params . callback . onSuccess ( {
443+ status : 200 ,
444+ data : {
445+ next : null ,
446+ previous : null ,
447+ count : 0
448+ }
449+ } ) ;
450+ }
451+ } ) ;
452+ spyOn ( vm , 'startLoader' ) . and . callThrough ( ) ;
453+ spyOn ( vm , 'stopLoader' ) . and . callThrough ( ) ;
454+ spyOn ( $rootScope , 'notify' ) ;
455+ vm . confirmDelete ( ev , hostTeamId ) ;
456+ setTimeout ( function ( ) {
457+ expect ( $mdDialog . show ) . toHaveBeenCalled ( ) ;
458+ expect ( vm . startLoader ) . toHaveBeenCalled ( ) ;
459+ expect ( utilities . sendRequest ) . toHaveBeenCalled ( ) ;
460+ expect ( $rootScope . notify ) . toHaveBeenCalledWith ( "info" , "You have removed yourself successfully" ) ;
461+ expect ( vm . existTeam . count ) . toBe ( 0 ) ;
462+ expect ( vm . showPagination ) . toBe ( false ) ;
463+ expect ( vm . paginationMsg ) . toBe ( "No team exists for now, start by creating a new team!" ) ;
464+ expect ( vm . stopLoader ) . toHaveBeenCalled ( ) ;
465+ done ( ) ;
466+ } , 0 ) ;
467+ } ) ;
468+
469+ it ( 'should show error notification if remove self from host team fails' , function ( done ) {
470+ var hostTeamId = 1 ;
471+ var ev = new Event ( '$click' ) ;
472+ $mdDialog . show . and . returnValue ( Promise . resolve ( ) ) ;
473+ spyOn ( vm , 'startLoader' ) . and . callThrough ( ) ;
474+ spyOn ( vm , 'stopLoader' ) . and . callThrough ( ) ;
475+ spyOn ( $rootScope , 'notify' ) ;
476+ spyOn ( utilities , 'sendRequest' ) . and . callFake ( function ( params ) {
477+ if ( params . method === 'DELETE' ) {
478+ params . callback . onError ( ) ;
479+ }
480+ } ) ;
481+ vm . confirmDelete ( ev , hostTeamId ) ;
482+ setTimeout ( function ( ) {
483+ expect ( vm . startLoader ) . toHaveBeenCalled ( ) ;
484+ expect ( $rootScope . notify ) . toHaveBeenCalledWith ( "error" , "Couldn't remove you from the challenge" ) ;
485+ expect ( vm . stopLoader ) . toHaveBeenCalled ( ) ;
486+ done ( ) ;
487+ } , 0 ) ;
488+ } ) ;
489+
490+ it ( 'should do nothing if dialog is cancelled' , function ( done ) {
491+ var hostTeamId = 1 ;
492+ var ev = new Event ( '$click' ) ;
493+ $mdDialog . show . and . returnValue ( Promise . reject ( ) ) ;
494+ spyOn ( vm , 'startLoader' ) ;
495+ spyOn ( utilities , 'sendRequest' ) ;
496+ vm . confirmDelete ( ev , hostTeamId ) ;
497+ setTimeout ( function ( ) {
498+ expect ( vm . startLoader ) . not . toHaveBeenCalled ( ) ;
499+ expect ( utilities . sendRequest ) . not . toHaveBeenCalled ( ) ;
500+ done ( ) ;
501+ } , 0 ) ;
502+ } ) ;
430503 } ) ;
431504
432505 describe ( 'Unit tests for inviteOthers function' , function ( ) {
433- beforeEach ( function ( ) {
506+ beforeEach ( function ( ) {
434507 spyOn ( $mdDialog , 'show' ) . and . callFake ( function ( ) {
435508 var deferred = $injector . get ( '$q' ) . defer ( ) ;
436509 return deferred . promise ;
437510 } ) ;
438511 } ) ;
439512
440- it ( 'open dialog to invite others' , function ( ) {
513+ it ( 'open dialog to invite others' , function ( ) {
441514 var hostId = 1 ;
442515 var ev = new Event ( '$click' ) ;
443516 var confirm = $mdDialog . prompt ( )
@@ -451,10 +524,71 @@ describe('Unit tests for challenge host team controller', function () {
451524 vm . inviteOthers ( ev , hostId ) ;
452525 expect ( $mdDialog . show ) . toHaveBeenCalledWith ( confirm ) ;
453526 } ) ;
527+
528+ it ( 'should successfully invite user when dialog is confirmed' , function ( done ) {
529+ var hostTeamId = 1 ;
530+ var ev = new Event ( '$click' ) ;
531+ var email = 'test@example.com' ;
532+
533+ $mdDialog . show . and . returnValue ( Promise . resolve ( email ) ) ;
534+
535+ spyOn ( utilities , 'sendRequest' ) . and . callFake ( function ( params ) {
536+ expect ( params . url ) . toBe ( 'hosts/challenge_host_teams/' + hostTeamId + '/invite' ) ;
537+ expect ( params . method ) . toBe ( 'POST' ) ;
538+ expect ( params . data . email ) . toBe ( email ) ;
539+ expect ( params . token ) . toBe ( utilities . getData ( 'userKey' ) ) ;
540+
541+
542+ params . callback . onSuccess ( ) ;
543+ } ) ;
544+
545+ spyOn ( $rootScope , 'notify' ) ;
546+
547+ vm . inviteOthers ( ev , hostTeamId ) ;
548+
549+ setTimeout ( function ( ) {
550+ expect ( utilities . sendRequest ) . toHaveBeenCalled ( ) ;
551+ expect ( $rootScope . notify ) . toHaveBeenCalledWith ( "success" , email + " has been added successfully" ) ;
552+ done ( ) ;
553+ } , 0 ) ;
554+ } ) ;
555+
556+ it ( 'should show error notification when invitation fails' , function ( done ) {
557+ var hostTeamId = 1 ;
558+ var ev = new Event ( '$click' ) ;
559+ var email = 'test@example.com' ;
560+ var errorMessage = 'User not found' ;
561+
562+ $mdDialog . show . and . returnValue ( Promise . resolve ( email ) ) ;
563+
564+ spyOn ( utilities , 'sendRequest' ) . and . callFake ( function ( params ) {
565+ expect ( params . url ) . toBe ( 'hosts/challenge_host_teams/' + hostTeamId + '/invite' ) ;
566+ expect ( params . method ) . toBe ( 'POST' ) ;
567+ expect ( params . data . email ) . toBe ( email ) ;
568+ expect ( params . token ) . toBe ( utilities . getData ( 'userKey' ) ) ;
569+
570+
571+ params . callback . onError ( {
572+ data : {
573+ error : errorMessage
574+ }
575+ } ) ;
576+ } ) ;
577+
578+ spyOn ( $rootScope , 'notify' ) ;
579+
580+ vm . inviteOthers ( ev , hostTeamId ) ;
581+
582+ setTimeout ( function ( ) {
583+ expect ( utilities . sendRequest ) . toHaveBeenCalled ( ) ;
584+ expect ( $rootScope . notify ) . toHaveBeenCalledWith ( "error" , errorMessage ) ;
585+ done ( ) ;
586+ } , 0 ) ;
587+ } ) ;
454588 } ) ;
455589
456590 describe ( 'Unit tests for storeChallengeHostTeamId function' , function ( ) {
457- beforeEach ( function ( ) {
591+ beforeEach ( function ( ) {
458592 spyOn ( utilities , 'storeData' ) ;
459593 spyOn ( $state , 'go' ) ;
460594 } ) ;
@@ -466,4 +600,4 @@ describe('Unit tests for challenge host team controller', function () {
466600 expect ( $state . go ) . toHaveBeenCalledWith ( 'web.challenge-create' ) ;
467601 } ) ;
468602 } ) ;
469- } ) ;
603+ } ) ;
0 commit comments