11import { IRateLimiter , RateLimiter , ReportingLogger } from '../../src/logging/reportingLogger' ;
22import { WSDKErrorSeverity } from '../../src/logging/logRequest' ;
33import { ErrorCodes } from '../../src/logging/errorCodes' ;
4- import { SDKConfig } from '../../src/store' ;
4+ import { IStore , SDKConfig } from '../../src/store' ;
55
66describe ( 'ReportingLogger' , ( ) => {
77 let logger : ReportingLogger ;
8- const baseUrl = 'https://test-url.com' ;
8+ const loggingUrl = 'https://test-url.com/v1/log' ;
9+ const errorUrl = 'https://test-url.com/v1/errors' ;
910 const sdkVersion = '1.2.3' ;
1011 let mockFetch : jest . Mock ;
1112 const accountId = '1234567890' ;
13+ const mockStore : IStore = { getRoktAccountId : ( ) => accountId } as IStore ;
14+ let sdkConfig : SDKConfig ;
1215 beforeEach ( ( ) => {
16+ sdkConfig = {
17+ loggingUrl : loggingUrl ,
18+ errorUrl : errorUrl ,
19+ isWebSdkLoggingEnabled : true ,
20+ } as SDKConfig ;
21+
1322 mockFetch = jest . fn ( ) . mockResolvedValue ( { ok : true } ) ;
1423 global . fetch = mockFetch ;
1524
@@ -26,10 +35,11 @@ describe('ReportingLogger', () => {
2635 fetch : mockFetch
2736 } ) ;
2837 logger = new ReportingLogger (
29- { loggingUrl : baseUrl , errorUrl : baseUrl } as SDKConfig ,
38+ sdkConfig ,
3039 sdkVersion ,
3140 'test-launcher-instance-guid'
3241 ) ;
42+ logger . setStore ( mockStore ) ;
3343 } ) ;
3444
3545 afterEach ( ( ) => {
@@ -42,7 +52,7 @@ describe('ReportingLogger', () => {
4252 logger . error ( 'msg' , ErrorCodes . UNHANDLED_EXCEPTION , 'stack' ) ;
4353 expect ( mockFetch ) . toHaveBeenCalled ( ) ;
4454 const fetchCall = mockFetch . mock . calls [ 0 ] ;
45- expect ( fetchCall [ 0 ] ) . toContain ( '/v1/log ' ) ;
55+ expect ( fetchCall [ 0 ] ) . toContain ( '/v1/error ' ) ;
4656 const body = JSON . parse ( fetchCall [ 1 ] . body ) ;
4757 expect ( body ) . toMatchObject ( {
4858 severity : WSDKErrorSeverity . ERROR ,
@@ -55,7 +65,7 @@ describe('ReportingLogger', () => {
5565 logger . warning ( 'warn' ) ;
5666 expect ( mockFetch ) . toHaveBeenCalled ( ) ;
5767 const fetchCall = mockFetch . mock . calls [ 0 ] ;
58- expect ( fetchCall [ 0 ] ) . toContain ( '/v1/log ' ) ;
68+ expect ( fetchCall [ 0 ] ) . toContain ( '/v1/error ' ) ;
5969 const body = JSON . parse ( fetchCall [ 1 ] . body ) ;
6070 expect ( body ) . toMatchObject ( {
6171 severity : WSDKErrorSeverity . WARNING
@@ -66,7 +76,7 @@ describe('ReportingLogger', () => {
6676 it ( 'does not log if ROKT_DOMAIN missing' , ( ) => {
6777 delete ( globalThis as any ) . ROKT_DOMAIN ;
6878 logger = new ReportingLogger (
69- { loggingUrl : baseUrl , errorUrl : baseUrl } as SDKConfig ,
79+ sdkConfig ,
7080 sdkVersion ,
7181 'test-launcher-instance-guid'
7282 ) ;
@@ -75,10 +85,10 @@ describe('ReportingLogger', () => {
7585 } ) ;
7686
7787 it ( 'does not log if feature flag and debug mode off' , ( ) => {
78- window . mParticle . config . isWebSdkLoggingEnabled = false ;
88+ sdkConfig . isWebSdkLoggingEnabled = false ;
7989 window . location . search = '' ;
8090 logger = new ReportingLogger (
81- { loggingUrl : baseUrl , errorUrl : baseUrl } as SDKConfig ,
91+ sdkConfig ,
8292 sdkVersion ,
8393 'test-launcher-instance-guid'
8494 ) ;
@@ -87,13 +97,14 @@ describe('ReportingLogger', () => {
8797 } ) ;
8898
8999 it ( 'logs if debug mode on even if feature flag off' , ( ) => {
90- window . mParticle . config . isWebSdkLoggingEnabled = false ;
100+ sdkConfig . isWebSdkLoggingEnabled = false ;
91101 window . location . search = '?mp_enable_logging=true' ;
92102 logger = new ReportingLogger (
93- { loggingUrl : baseUrl , errorUrl : baseUrl } as SDKConfig ,
103+ sdkConfig ,
94104 sdkVersion ,
95105 'test-launcher-instance-guid'
96106 ) ;
107+
97108 logger . error ( 'x' ) ;
98109 expect ( mockFetch ) . toHaveBeenCalled ( ) ;
99110 } ) ;
@@ -106,7 +117,7 @@ describe('ReportingLogger', () => {
106117 } ) ,
107118 } ;
108119 logger = new ReportingLogger (
109- { loggingUrl : baseUrl , errorUrl : baseUrl } as SDKConfig ,
120+ sdkConfig ,
110121 sdkVersion ,
111122 'test-launcher-instance-guid' ,
112123 mockRateLimiter
@@ -115,33 +126,6 @@ describe('ReportingLogger', () => {
115126 for ( let i = 0 ; i < 5 ; i ++ ) logger . error ( 'err' ) ;
116127 expect ( mockFetch ) . toHaveBeenCalledTimes ( 3 ) ;
117128 } ) ;
118-
119- it ( 'uses default account id when accountId is empty' , ( ) => {
120- logger = new ReportingLogger (
121- { loggingUrl : baseUrl , errorUrl : baseUrl } as SDKConfig ,
122- sdkVersion ,
123- 'test-launcher-instance-guid'
124- ) ;
125- logger . error ( 'msg' ) ;
126- expect ( mockFetch ) . toHaveBeenCalled ( ) ;
127- const fetchCall = mockFetch . mock . calls [ 0 ] ;
128- expect ( fetchCall [ 1 ] . headers [ 'rokt-account-id' ] ) . toBe ( '0' ) ;
129- } ) ;
130-
131- it ( 'uses default user agent when user agent is empty' , ( ) => {
132- logger = new ReportingLogger (
133- { loggingUrl : baseUrl , errorUrl : baseUrl } as SDKConfig ,
134- sdkVersion ,
135- 'test-launcher-instance-guid'
136- ) ;
137- delete ( globalThis as any ) . navigator ;
138- delete ( globalThis as any ) . location ;
139- logger . error ( 'msg' ) ;
140- expect ( mockFetch ) . toHaveBeenCalled ( ) ;
141- const fetchCall = mockFetch . mock . calls [ 0 ] ;
142- const body = JSON . parse ( fetchCall [ 1 ] . body ) ;
143- expect ( body ) . toMatchObject ( { deviceInfo : 'no-user-agent-set' , url : 'no-url-set' } ) ;
144- } ) ;
145129} ) ;
146130
147131describe ( 'RateLimiter' , ( ) => {
0 commit comments