@@ -13,6 +13,7 @@ import { SplitPipe } from 'src/app/shared/utils/split.pipe';
1313import { APP_DATA_SERVICES_MAP } from '../../../../config/app-config.interface' ;
1414import { RemoteDataBuildService } from '../../../core/cache/builders/remote-data-build.service' ;
1515import { ObjectCacheService } from '../../../core/cache/object-cache.service' ;
16+ import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service' ;
1617import { RequestService } from '../../../core/data/request.service' ;
1718import { QualityAssuranceSourceObject } from '../../../core/notifications/qa/models/quality-assurance-source.model' ;
1819import { QualityAssuranceSourceDataService } from '../../../core/notifications/qa/source/quality-assurance-source-data.service' ;
@@ -28,6 +29,7 @@ describe('QaEventNotificationComponent', () => {
2829 let component : QaEventNotificationComponent ;
2930 let fixture : ComponentFixture < QaEventNotificationComponent > ;
3031 let qualityAssuranceSourceDataServiceStub : any ;
32+ let authorizationServiceStub : any ;
3133
3234 const obj = Object . assign ( new QualityAssuranceSourceObject ( ) , {
3335 id : 'sourceName:target' ,
@@ -41,14 +43,18 @@ describe('QaEventNotificationComponent', () => {
4143 beforeEach ( async ( ) => {
4244
4345 qualityAssuranceSourceDataServiceStub = {
44- getSourcesByTarget : ( ) => objPL ,
46+ getSourcesByTarget : jasmine . createSpy ( 'getSourcesByTarget' ) . and . returnValue ( objPL ) ,
47+ } ;
48+ authorizationServiceStub = {
49+ isAuthorized : jasmine . createSpy ( 'isAuthorized' ) . and . returnValue ( of ( true ) ) ,
4550 } ;
4651 await TestBed . configureTestingModule ( {
4752 imports : [ CommonModule , TranslateModule . forRoot ( ) , QaEventNotificationComponent , SplitPipe ] ,
4853 providers : [
4954 { provide : APP_DATA_SERVICES_MAP , useValue : { } } ,
5055 { provide : ActivatedRoute , useValue : new ActivatedRouteStub ( ) } ,
5156 { provide : QualityAssuranceSourceDataService , useValue : qualityAssuranceSourceDataServiceStub } ,
57+ { provide : AuthorizationDataService , useValue : authorizationServiceStub } ,
5258 { provide : RequestService , useValue : { } } ,
5359 { provide : NotificationsService , useValue : { } } ,
5460 { provide : HALEndpointService , useValue : new HALEndpointServiceStub ( 'test' ) } ,
@@ -57,6 +63,12 @@ describe('QaEventNotificationComponent', () => {
5763 provideMockStore ( { } ) ,
5864 ] ,
5965 } )
66+ // The component declares QualityAssuranceSourceDataService in its own `providers`,
67+ // which would otherwise shadow the stub above with a real instance. Override it so
68+ // the component uses the stub we can assert against.
69+ . overrideComponent ( QaEventNotificationComponent , {
70+ set : { providers : [ { provide : QualityAssuranceSourceDataService , useValue : qualityAssuranceSourceDataServiceStub } ] } ,
71+ } )
6072 . compileComponents ( ) ;
6173 fixture = TestBed . createComponent ( QaEventNotificationComponent ) ;
6274 component = fixture . componentInstance ;
@@ -78,4 +90,22 @@ describe('QaEventNotificationComponent', () => {
7890 const route = component . getQualityAssuranceRoute ( ) ;
7991 expect ( route ) . toBe ( '/notifications/quality-assurance' ) ;
8092 } ) ;
93+
94+ it ( 'should request QA sources when the user is authorized to see QA' , ( ) => {
95+ authorizationServiceStub . isAuthorized . and . returnValue ( of ( true ) ) ;
96+ qualityAssuranceSourceDataServiceStub . getSourcesByTarget . calls . reset ( ) ;
97+ let result : QualityAssuranceSourceObject [ ] ;
98+ component . getQualityAssuranceSources$ ( ) . subscribe ( ( sources ) => result = sources ) ;
99+ expect ( qualityAssuranceSourceDataServiceStub . getSourcesByTarget ) . toHaveBeenCalled ( ) ;
100+ expect ( result ) . toEqual ( [ obj ] ) ;
101+ } ) ;
102+
103+ it ( 'should NOT request QA sources when the user is not authorized' , ( ) => {
104+ authorizationServiceStub . isAuthorized . and . returnValue ( of ( false ) ) ;
105+ qualityAssuranceSourceDataServiceStub . getSourcesByTarget . calls . reset ( ) ;
106+ let result : QualityAssuranceSourceObject [ ] ;
107+ component . getQualityAssuranceSources$ ( ) . subscribe ( ( sources ) => result = sources ) ;
108+ expect ( qualityAssuranceSourceDataServiceStub . getSourcesByTarget ) . not . toHaveBeenCalled ( ) ;
109+ expect ( result ) . toEqual ( [ ] ) ;
110+ } ) ;
81111} ) ;
0 commit comments