@@ -16,17 +16,21 @@ import { AppComponent } from '@/app/app.component';
1616import { CoreEvents } from '@singletons/events' ;
1717import { CoreLang , CoreLangProvider } from '@services/lang' ;
1818
19- import { mockSingleton , renderComponent } from '@/testing/utils' ;
19+ import { mock , mockSingleton , renderComponent , wait } from '@/testing/utils' ;
2020import { CoreNavigator , CoreNavigatorService } from '@services/navigator' ;
21+ import { CoreSites } from '@services/sites' ;
22+ import { Http } from '@singletons' ;
23+ import { of } from 'rxjs' ;
24+ import { CoreSite } from '@classes/sites/site' ;
25+ import { CoreUtils } from '@services/utils/utils' ;
2126
2227describe ( 'AppComponent' , ( ) => {
2328
2429 let langProvider : CoreLangProvider ;
25- let navigator : CoreNavigatorService ;
26-
2730 beforeEach ( ( ) => {
28- navigator = mockSingleton ( CoreNavigator , [ 'navigate' ] ) ;
29- langProvider = mockSingleton ( CoreLang , [ 'clearCustomStrings' ] ) ;
31+ langProvider = mockSingleton ( CoreLang , mock ( { getCurrentLanguage : async ( ) => 'en' , clearCustomStrings : ( ) => null } ) ) ;
32+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
33+ mockSingleton ( Http , { get : ( ) => of ( null as any ) } ) ;
3034 } ) ;
3135
3236 it ( 'should render' , async ( ) => {
@@ -38,6 +42,7 @@ describe('AppComponent', () => {
3842
3943 it ( 'cleans up on logout' , async ( ) => {
4044 const fixture = await renderComponent ( AppComponent ) ;
45+ const navigator : CoreNavigatorService = mockSingleton ( CoreNavigator , [ 'navigate' ] ) ;
4146
4247 fixture . componentInstance . ngOnInit ( ) ;
4348 CoreEvents . trigger ( CoreEvents . LOGOUT ) ;
@@ -46,4 +51,66 @@ describe('AppComponent', () => {
4651 expect ( navigator . navigate ) . toHaveBeenCalledWith ( '/login/sites' , { reset : true } ) ;
4752 } ) ;
4853
54+ it ( 'adds ionic platform and theme classes' , async ( ) => {
55+ const fixture = await renderComponent ( AppComponent ) ;
56+ const siteUrl = 'https://campus.example.edu' ;
57+ const themeName = 'mytheme' ;
58+ const themeName2 = 'anothertheme' ;
59+
60+ fixture . componentInstance . ngOnInit ( ) ;
61+
62+ expect ( document . documentElement . classList . contains ( 'ionic7' ) ) . toBe ( true ) ;
63+
64+ const site = mock ( new CoreSite ( '42' , siteUrl , 'token' , { info : {
65+ sitename : 'Example Campus' ,
66+ username : 'admin' ,
67+ firstname : 'Admin' ,
68+ lastname : 'User' ,
69+ fullname : 'Admin User' ,
70+ lang : 'en' ,
71+ userid : 1 ,
72+ siteurl : siteUrl ,
73+ userpictureurl : '' ,
74+ theme : themeName ,
75+ functions : [ ] ,
76+ } } ) ) ;
77+
78+ mockSingleton ( CoreSites , {
79+ getSite : ( ) => Promise . resolve ( site ) ,
80+ getCurrentSiteId : ( ) => '42' ,
81+ } ) ;
82+
83+ CoreEvents . trigger ( CoreEvents . LOGIN , { } , '42' ) ;
84+ // Wait the event to be processed.
85+ await wait ( 50 ) ;
86+
87+ expect ( document . documentElement . classList . contains ( 'theme-site-' + themeName ) ) . toBe ( true ) ;
88+ expect ( document . documentElement . classList . contains ( 'theme-site-' + themeName2 ) ) . toBe ( false ) ;
89+
90+ if ( site . infos ) {
91+ site . infos . theme = themeName2 ;
92+ }
93+
94+ CoreEvents . trigger ( CoreEvents . SITE_UPDATED , site . infos , '42' ) ;
95+
96+ // Wait the event to be processed.
97+ await CoreUtils . nextTick ( ) ;
98+
99+ expect ( document . documentElement . classList . contains ( 'theme-site-' + themeName2 ) ) . toBe ( true ) ;
100+ expect ( document . documentElement . classList . contains ( 'theme-site-' + themeName ) ) . toBe ( false ) ;
101+
102+ CoreEvents . trigger ( CoreEvents . LOGOUT ) ;
103+
104+ expect ( document . documentElement . classList . contains ( 'theme-site-' + themeName ) ) . toBe ( false ) ;
105+ expect ( document . documentElement . classList . contains ( 'theme-site-' + themeName2 ) ) . toBe ( false ) ;
106+
107+ CoreEvents . trigger ( CoreEvents . SITE_ADDED , site . infos , '42' ) ;
108+
109+ // Wait the event to be processed.
110+ await CoreUtils . nextTick ( ) ;
111+
112+ expect ( document . documentElement . classList . contains ( 'theme-site-' + themeName2 ) ) . toBe ( true ) ;
113+ expect ( document . documentElement . classList . contains ( 'theme-site-' + themeName ) ) . toBe ( false ) ;
114+ } ) ;
115+
49116} ) ;
0 commit comments