11import {
22 fakeAsync ,
33 TestBed ,
4- tick ,
54} from '@angular/core/testing' ;
65import {
76 ActivatedRoute ,
@@ -20,12 +19,11 @@ import { Registration } from '../../core/shared/registration.model';
2019import { RouterMock } from '../../shared/mocks/router.mock' ;
2120import {
2221 createFailedRemoteDataObject$ ,
23- createNoContentRemoteDataObject$ ,
2422 createSuccessfulRemoteDataObject$ ,
2523} from '../../shared/remote-data.utils' ;
2624import { registrationTokenGuard } from './registration-token-guard' ;
2725
28- describe ( 'RegistrationTokenGuard' ,
26+ fdescribe ( 'RegistrationTokenGuard' ,
2927 ( ) => {
3028 const route = new RouterMock ( ) ;
3129 const registrationWithGroups = Object . assign ( new Registration ( ) ,
@@ -47,51 +45,79 @@ describe('RegistrationTokenGuard',
4745 uuid : 'test-eperson' ,
4846 } ) ;
4947
50- let arouteStub = {
51- snapshot : {
52- params : {
53- token : '123456789' ,
54- } ,
55- } ,
56- } ;
57-
58- beforeEach ( ( ) => {
59- TestBed . configureTestingModule ( {
60- providers : [ { provide : Router , useValue : route } ,
61- {
62- provide : ActivatedRoute ,
63- useValue : arouteStub ,
48+ describe ( 'when token provided' , ( ) => {
49+
50+ let arouteStub = {
51+ snapshot : {
52+ params : {
53+ token : '123456789' ,
6454 } ,
65- { provide : EpersonRegistrationService , useValue : epersonRegistrationService } ,
66- { provide : AuthService , useValue : authService } ,
67- ] ,
55+ } ,
56+ } ;
57+
58+ beforeEach ( ( ) => {
59+ TestBed . configureTestingModule ( {
60+ providers : [ { provide : Router , useValue : route } ,
61+ {
62+ provide : ActivatedRoute ,
63+ useValue : arouteStub ,
64+ } ,
65+ { provide : EpersonRegistrationService , useValue : epersonRegistrationService } ,
66+ { provide : AuthService , useValue : authService } ,
67+ ] ,
68+ } ) ;
6869 } ) ;
69- } ) ;
7070
71- describe ( 'when token provided' , ( ) => {
72- it ( 'can activate must return true when registration data includes groups' , fakeAsync ( ( ) => {
71+ it ( 'can activate must return true when registration data includes groups' , ( ( async ) => {
7372 const activatedRoute = TestBed . inject ( ActivatedRoute ) ;
73+ epersonRegistrationService . searchByTokenAndHandleError . and . returnValue ( createSuccessfulRemoteDataObject$ ( registrationWithGroups ) ) ;
74+ activatedRoute . snapshot . params . token = arouteStub . snapshot . params . token ;
7475
7576 const result$ = TestBed . runInInjectionContext ( ( ) => {
7677 return registrationTokenGuard ( activatedRoute . snapshot , { } as RouterStateSnapshot ) as Observable < boolean > ;
7778 } ) ;
7879
79- let output = null ;
80- result$ . subscribe ( ( result ) => ( output = result ) ) ;
81- tick ( 100 ) ;
82- expect ( output ) . toBeTrue ( ) ;
80+ result$ . subscribe ( ( result ) => {
81+ expect ( result ) . toBeTrue ( ) ;
82+ async ( ) ;
83+ } ) ;
8384 } ) ) ;
8485 } ) ;
8586
8687 describe ( 'when no token provided' , ( ) => {
87- it ( 'can activate must return false when registration data includes groups' , fakeAsync ( ( ) => {
88- const registrationWithDifferentUserFromLoggedIn = Object . assign ( new Registration ( ) , {
89- email : 't1@email.org' ,
90- token : 'test-token' ,
88+
89+ let noTokenRoute = {
90+ snapshot : {
91+ params : {
92+ token : null ,
93+ } ,
94+ } ,
95+ } ;
96+
97+ const registrationWithDifferentUserFromLoggedIn = Object . assign ( new Registration ( ) , {
98+ email : 't1@email.org' ,
99+ token : 'test-token' ,
100+ } ) ;
101+
102+ const epersonDifferentUserFromLoggedIn = jasmine . createSpyObj ( 'epersonRegistrationService' , {
103+ searchByTokenAndHandleError : observableOf ( registrationWithDifferentUserFromLoggedIn ) ,
104+ } ) ;
105+
106+ beforeEach ( ( ) => {
107+ TestBed . configureTestingModule ( {
108+ providers : [ { provide : Router , useValue : route } ,
109+ {
110+ provide : ActivatedRoute ,
111+ useValue : noTokenRoute ,
112+ } ,
113+ { provide : EpersonRegistrationService , useValue : epersonDifferentUserFromLoggedIn } ,
114+ { provide : AuthService , useValue : authService } ,
115+ ] ,
91116 } ) ;
92- epersonRegistrationService . searchByTokenAndHandleError . and . returnValue ( observableOf ( registrationWithDifferentUserFromLoggedIn ) ) ;
117+ } ) ;
118+
119+ it ( 'can activate must return false when registration data includes groups' , fakeAsync ( ( ) => {
93120 let activatedRoute = TestBed . inject ( ActivatedRoute ) ;
94- activatedRoute . snapshot . params . token = null ;
95121
96122 const result$ = TestBed . runInInjectionContext ( ( ) => {
97123 return registrationTokenGuard ( activatedRoute . snapshot , { } as RouterStateSnapshot ) as Observable < boolean > ;
@@ -100,26 +126,38 @@ describe('RegistrationTokenGuard',
100126 let output = null ;
101127 result$ . subscribe ( ( result ) => ( output = result ) ) ;
102128 expect ( output ) . toBeFalse ( ) ;
129+ expect ( route . navigate ) . toHaveBeenCalledWith ( [ '/404' ] ) ;
103130 } ) ) ;
104131 } ) ;
105132
106133 describe ( 'when invalid token provided' , ( ) => {
107- it ( 'can activate must navigate through 404 when no content received' , fakeAsync ( ( ) => {
108- epersonRegistrationService . searchByTokenAndHandleError . and . returnValue ( createNoContentRemoteDataObject$ ( ) ) ;
109- let activatedRoute = TestBed . inject ( ActivatedRoute ) ;
110- activatedRoute . snapshot . params . token = 'invalid-token' ;
134+ let invalidTokenRoute = {
135+ snapshot : {
136+ params : {
137+ token : 'invalid-token' ,
138+ } ,
139+ } ,
140+ } ;
111141
112- const result$ = TestBed . runInInjectionContext ( ( ) => {
113- return registrationTokenGuard ( activatedRoute . snapshot , { } as RouterStateSnapshot ) as Observable < boolean > ;
142+ const failedRegistationService = jasmine . createSpyObj ( 'epersonRegistrationService' , {
143+ searchByTokenAndHandleError : createFailedRemoteDataObject$ ( 'invalid token' , 404 ) ,
144+ } ) ;
145+
146+ beforeEach ( ( ) => {
147+ TestBed . configureTestingModule ( {
148+ providers : [ { provide : Router , useValue : route } ,
149+ {
150+ provide : ActivatedRoute ,
151+ useValue : invalidTokenRoute ,
152+ } ,
153+ { provide : EpersonRegistrationService , useValue : failedRegistationService } ,
154+ { provide : AuthService , useValue : authService } ,
155+ ] ,
114156 } ) ;
157+ } ) ;
115158
116- result$ . subscribe ( ( _ ) => { } ) ;
117- expect ( route . navigate ) . toHaveBeenCalledWith ( [ '/404' ] ) ;
118- } ) ) ;
119- it ( 'can activate must navigate through 404 when no failed response received' , fakeAsync ( ( ) => {
120- epersonRegistrationService . searchByTokenAndHandleError . and . returnValue ( createFailedRemoteDataObject$ ( 'invalid token' , 404 ) ) ;
159+ it ( 'can activate must navigate through 404 when failed response received' , fakeAsync ( ( ) => {
121160 let activatedRoute = TestBed . inject ( ActivatedRoute ) ;
122- activatedRoute . snapshot . params . token = 'error-invalid-token' ;
123161
124162 const result$ = TestBed . runInInjectionContext ( ( ) => {
125163 return registrationTokenGuard ( activatedRoute . snapshot , { } as RouterStateSnapshot ) as Observable < boolean > ;
0 commit comments