@@ -10,100 +10,119 @@ import { AuthService } from 'src/app/auth.service'
1010import { ToolbarComponent } from './toolbar.component'
1111import { BehaviorSubject , of } from 'rxjs'
1212import { HttpClient , provideHttpClient } from '@angular/common/http'
13- import { TranslateLoader , TranslateModule , TranslateService } from '@ngx-translate/core'
13+ import { TranslateLoader , TranslateModule } from '@ngx-translate/core'
1414import { TranslateHttpLoader } from '@ngx-translate/http-loader'
1515import { provideHttpClientTesting } from '@angular/common/http/testing'
16+ import { environment } from 'src/environments/environment'
17+
18+ // Factory function for the TranslateHttpLoader
19+ function createTranslateLoader ( http : HttpClient ) {
20+ return new TranslateHttpLoader ( http , '/assets/i18n/' , '.json' )
21+ }
1622
1723describe ( 'ToolbarComponent' , ( ) => {
1824 let component : ToolbarComponent
1925 let fixture : ComponentFixture < ToolbarComponent >
2026 let authService : jasmine . SpyObj < AuthService >
21- let getMPSVersionSpy : jasmine . Spy
22- let getRPSVersionSpy : jasmine . Spy
23- // let getConsoleVersionSpy: jasmine.Spy
24- // let logoutSpy: jasmine.Spy
25- let translate : TranslateService
26-
27- // Factory function for the TranslateHttpLoader
28- function HttpLoaderFactory ( http : HttpClient ) {
29- return new TranslateHttpLoader ( http , '/assets/i18n/' , '.json' )
30- }
27+ let matDialog : jasmine . SpyObj < MatDialog >
28+ let isLoggedInSubject : BehaviorSubject < boolean >
29+
3130 beforeEach ( async ( ) => {
31+ // Override environment.cloud for testing
32+ Object . defineProperty ( environment , 'cloud' , {
33+ writable : true ,
34+ value : true
35+ } )
36+
37+ isLoggedInSubject = new BehaviorSubject < boolean > ( false )
38+
39+ const authServiceSpy = jasmine . createSpyObj (
40+ 'AuthService' ,
41+ [
42+ 'logout' ,
43+ 'getMPSVersion' ,
44+ 'getRPSVersion' ,
45+ 'getConsoleVersion' ,
46+ 'compareSemver'
47+ ] ,
48+ {
49+ isLoggedIn : isLoggedInSubject . asObservable ( ) ,
50+ loggedInSubject$ : isLoggedInSubject
51+ }
52+ )
53+
54+ authServiceSpy . getMPSVersion . and . returnValue ( of ( { version : '1.0.0' } ) )
55+ authServiceSpy . getRPSVersion . and . returnValue ( of ( { version : '1.0.0' } ) )
56+ authServiceSpy . getConsoleVersion . and . returnValue ( of ( { version : '1.0.0' } ) )
57+ authServiceSpy . compareSemver . and . returnValue ( 1 )
58+
59+ const matDialogSpy = jasmine . createSpyObj ( 'MatDialog' , [ 'open' ] )
3260 const routerSpy = jasmine . createSpyObj ( 'Router' , [ 'navigate' ] )
33- authService = jasmine . createSpyObj ( 'AuthService' , [
34- 'logout' ,
35- 'getMPSVersion' ,
36- 'getRPSVersion' ,
37- 'getConsoleVersion'
38- ] )
39- getMPSVersionSpy = authService . getMPSVersion . and . returnValue ( of ( { } ) )
40- getRPSVersionSpy = authService . getRPSVersion . and . returnValue ( of ( { } ) )
41- // getConsoleVersionSpy = authService.getConsoleVersion.and.returnValue(of({}))
42- const authServiceStub = {
43- loggedInSubject$ : new BehaviorSubject < boolean > ( true )
44- }
45-
46- TestBed . configureTestingModule ( {
61+
62+ await TestBed . configureTestingModule ( {
4763 imports : [
4864 ToolbarComponent ,
4965 TranslateModule . forRoot ( {
5066 loader : {
5167 provide : TranslateLoader ,
52- useFactory : HttpLoaderFactory ,
68+ useFactory : createTranslateLoader ,
5369 deps : [ HttpClient ]
5470 }
5571 } )
5672 ] ,
5773 providers : [
58- { provide : Router , useValue : routerSpy } ,
59- { provide : AuthService , useValue : { ...authServiceStub , ...authService } } ,
60- TranslateService ,
6174 provideHttpClient ( ) ,
62- provideHttpClientTesting ( )
63- ]
64- } )
75+ provideHttpClientTesting ( ) ,
76+ { provide : AuthService , useValue : authServiceSpy } ,
77+ { provide : MatDialog , useValue : matDialogSpy } ,
78+ { provide : Router , useValue : routerSpy } ]
79+ } ) . compileComponents ( )
80+
6581 fixture = TestBed . createComponent ( ToolbarComponent )
6682 component = fixture . componentInstance
67- translate = TestBed . inject ( TranslateService )
68- translate . setDefaultLang ( 'en' )
69- fixture . detectChanges ( )
83+ authService = TestBed . inject ( AuthService ) as jasmine . SpyObj < AuthService >
84+ matDialog = TestBed . inject ( MatDialog ) as jasmine . SpyObj < MatDialog >
7085 } )
7186
7287 afterEach ( ( ) => {
7388 TestBed . resetTestingModule ( )
74- component . cloudMode = true
7589 } )
7690
7791 it ( 'should create' , ( ) => {
92+ fixture . detectChanges ( )
7893 expect ( component ) . toBeTruthy ( )
79- expect ( component . isLoggedIn ) . toBeTrue ( )
80- expect ( component . cloudMode ) . toBeTrue ( )
8194 } )
8295
8396 it ( 'should display dialog' , ( ) => {
84- const dialogSpy = spyOn ( TestBed . inject ( MatDialog ) , 'open' )
97+ fixture . detectChanges ( )
8598 component . displayAbout ( )
86- expect ( dialogSpy ) . toHaveBeenCalled ( )
99+ expect ( matDialog . open ) . toHaveBeenCalled ( )
87100 } )
88101
89102 it ( 'should logout and redirect to login page' , ( ) => {
103+ fixture . detectChanges ( )
90104 component . logout ( )
91105 expect ( authService . logout ) . toHaveBeenCalled ( )
92106 } )
93107
94- it ( 'should call getMPSVersion' , ( ) => {
95- expect ( component ) . toBeTruthy ( )
96- expect ( getMPSVersionSpy ) . toHaveBeenCalled ( )
108+ it ( 'should call getMPSVersion when logged in and cloud mode' , ( ) => {
109+ fixture . detectChanges ( )
110+ isLoggedInSubject . next ( true )
111+
112+ expect ( authService . getMPSVersion ) . toHaveBeenCalled ( )
97113 } )
98114
99- it ( 'should call getRPSVersion' , ( ) => {
100- expect ( component ) . toBeTruthy ( )
101- expect ( getRPSVersionSpy ) . toHaveBeenCalled ( )
115+ it ( 'should call getRPSVersion when logged in and cloud mode' , ( ) => {
116+ fixture . detectChanges ( )
117+ isLoggedInSubject . next ( true )
118+
119+ expect ( authService . getRPSVersion ) . toHaveBeenCalled ( )
102120 } )
103121
104122 it ( 'should subscribe to loggedInSubject on init' , ( ) => {
105- component . authService . loggedInSubject$ . next ( true )
106123 fixture . detectChanges ( )
124+ isLoggedInSubject . next ( true )
125+
107126 expect ( component . isLoggedIn ) . toBeTruthy ( )
108127 } )
109128} )
0 commit comments