1+ import {
2+ Injector ,
3+ runInInjectionContext ,
4+ } from '@angular/core' ;
15import {
26 fakeAsync ,
37 TestBed ,
@@ -53,6 +57,7 @@ describe('MatomoService', () => {
5357 { provide : OrejimeService , useValue : orejimeService } ,
5458 { provide : NativeWindowService , useValue : nativeWindowService } ,
5559 { provide : ConfigurationDataService , useValue : configService } ,
60+ { provide : Injector , useValue : TestBed } ,
5661 ] ,
5762 } ) ;
5863
@@ -70,11 +75,13 @@ describe('MatomoService', () => {
7075 } ) ;
7176
7277 it ( 'should call setConsentGiven when consent is true' , ( ) => {
78+ service . matomoTracker = matomoTracker ;
7379 service . changeMatomoConsent ( true ) ;
7480 expect ( matomoTracker . setConsentGiven ) . toHaveBeenCalled ( ) ;
7581 } ) ;
7682
7783 it ( 'should call forgetConsentGiven when consent is false' , ( ) => {
84+ service . matomoTracker = matomoTracker ;
7885 service . changeMatomoConsent ( false ) ;
7986 expect ( matomoTracker . forgetConsentGiven ) . toHaveBeenCalled ( ) ;
8087 } ) ;
@@ -91,7 +98,10 @@ describe('MatomoService', () => {
9198 configService . findByPropertyName . withArgs ( MATOMO_SITE_ID ) . and . returnValue (
9299 createSuccessfulRemoteDataObject$ ( Object . assign ( new ConfigurationProperty ( ) , { values : [ '1' ] } ) ) ) ;
93100 orejimeService . getSavedPreferences . and . returnValue ( of ( { matomo : true } ) ) ;
94- service . init ( ) ;
101+
102+ runInInjectionContext ( TestBed , ( ) => {
103+ service . init ( ) ;
104+ } ) ;
95105
96106 expect ( matomoTracker . setConsentGiven ) . toHaveBeenCalled ( ) ;
97107 expect ( matomoInitializer . initializeTracker ) . toHaveBeenCalledWith ( {
@@ -100,7 +110,7 @@ describe('MatomoService', () => {
100110 } ) ;
101111 } ) ;
102112
103- it ( 'should initialize tracker with REST configuration correct parameters in production' , ( ) => {
113+ it ( 'should initialize tracker with REST configuration correct parameters in production' , fakeAsync ( ( ) => {
104114 environment . production = true ;
105115 environment . matomo = { trackerUrl : '' } ;
106116 configService . findByPropertyName . withArgs ( MATOMO_TRACKER_URL ) . and . returnValue (
@@ -113,19 +123,25 @@ describe('MatomoService', () => {
113123 createSuccessfulRemoteDataObject$ ( Object . assign ( new ConfigurationProperty ( ) , { values : [ '1' ] } ) ) ) ;
114124 orejimeService . getSavedPreferences . and . returnValue ( of ( { matomo : true } ) ) ;
115125
116- service . init ( ) ;
126+ runInInjectionContext ( TestBed , ( ) => {
127+ service . init ( ) ;
128+ } ) ;
129+
130+ tick ( ) ;
117131
118132 expect ( matomoTracker . setConsentGiven ) . toHaveBeenCalled ( ) ;
119133 expect ( matomoInitializer . initializeTracker ) . toHaveBeenCalledWith ( {
120134 siteId : '1' ,
121135 trackerUrl : 'http://example.com' ,
122136 } ) ;
123- } ) ;
137+ } ) ) ;
124138
125139 it ( 'should not initialize tracker if not in production' , ( ) => {
126140 environment . production = false ;
127141
128- service . init ( ) ;
142+ runInInjectionContext ( TestBed , ( ) => {
143+ service . init ( ) ;
144+ } ) ;
129145
130146 expect ( matomoInitializer . initializeTracker ) . not . toHaveBeenCalled ( ) ;
131147 } ) ;
@@ -143,14 +159,17 @@ describe('MatomoService', () => {
143159 createSuccessfulRemoteDataObject$ ( Object . assign ( new ConfigurationProperty ( ) , { values : [ '1' ] } ) ) ) ;
144160 orejimeService . getSavedPreferences . and . returnValue ( of ( { matomo : true } ) ) ;
145161
146- service . init ( ) ;
162+ runInInjectionContext ( TestBed , ( ) => {
163+ service . init ( ) ;
164+ } ) ;
147165
148166 expect ( matomoInitializer . initializeTracker ) . not . toHaveBeenCalled ( ) ;
149167 } ) ;
150168
151169 describe ( 'with visitorId set' , ( ) => {
152170 beforeEach ( ( ) => {
153171 matomoTracker . getVisitorId . and . returnValue ( Promise . resolve ( '12345' ) ) ;
172+ service . matomoTracker = matomoTracker ;
154173 } ) ;
155174
156175 it ( 'should add trackerId parameter' , fakeAsync ( ( ) => {
0 commit comments