@@ -16,17 +16,21 @@ import { AppComponent } from '@/app/app.component';
16
16
import { CoreEvents } from '@singletons/events' ;
17
17
import { CoreLang , CoreLangProvider } from '@services/lang' ;
18
18
19
- import { mockSingleton , renderComponent } from '@/testing/utils' ;
19
+ import { mock , mockSingleton , renderComponent , wait } from '@/testing/utils' ;
20
20
import { 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' ;
21
26
22
27
describe ( 'AppComponent' , ( ) => {
23
28
24
29
let langProvider : CoreLangProvider ;
25
- let navigator : CoreNavigatorService ;
26
-
27
30
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 ) } ) ;
30
34
} ) ;
31
35
32
36
it ( 'should render' , async ( ) => {
@@ -38,6 +42,7 @@ describe('AppComponent', () => {
38
42
39
43
it ( 'cleans up on logout' , async ( ) => {
40
44
const fixture = await renderComponent ( AppComponent ) ;
45
+ const navigator : CoreNavigatorService = mockSingleton ( CoreNavigator , [ 'navigate' ] ) ;
41
46
42
47
fixture . componentInstance . ngOnInit ( ) ;
43
48
CoreEvents . trigger ( CoreEvents . LOGOUT ) ;
@@ -46,4 +51,66 @@ describe('AppComponent', () => {
46
51
expect ( navigator . navigate ) . toHaveBeenCalledWith ( '/login/sites' , { reset : true } ) ;
47
52
} ) ;
48
53
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
+
49
116
} ) ;
0 commit comments