@@ -46,6 +46,7 @@ function assertSuccess(
4646 doi : string ,
4747 event : DataciteEvent
4848) {
49+ assertSendBeacon ( dataciteTrackerAddress , dataciteTrackerRepoId , doi , event ) ;
4950 const req = httpMock . expectOne ( dataciteTrackerAddress ) ;
5051 expect ( req . request . method ) . toBe ( 'POST' ) ;
5152 expect ( req . request . body ) . toEqual ( {
@@ -58,6 +59,24 @@ function assertSuccess(
5859 req . flush ( { } ) ;
5960}
6061
62+ function assertSendBeacon (
63+ dataciteTrackerAddress : string ,
64+ dataciteTrackerRepoId : string ,
65+ doi : string ,
66+ event : DataciteEvent
67+ ) {
68+ expect ( navigator . sendBeacon ) . toBeCalledTimes ( 1 ) ;
69+ expect ( navigator . sendBeacon ) . toHaveBeenCalledWith (
70+ dataciteTrackerAddress ,
71+ JSON . stringify ( {
72+ n : event ,
73+ u : window . location . href ,
74+ i : dataciteTrackerRepoId ,
75+ p : doi ,
76+ } )
77+ ) ;
78+ }
79+
6180describe ( 'DataciteService' , ( ) => {
6281 let service : DataciteService ;
6382 let sentry : jest . Mocked < any > ;
@@ -67,10 +86,11 @@ describe('DataciteService', () => {
6786 const apiDomainUrl = 'https://osf.io' ;
6887 const dataciteTrackerRepoId = 'repo-123' ;
6988 describe ( 'with proper configuration' , ( ) => {
70- sentry = {
71- captureException : jest . fn ( ) ,
72- } ;
7389 beforeEach ( ( ) => {
90+ Object . defineProperty ( navigator , 'sendBeacon' , {
91+ configurable : true ,
92+ value : jest . fn ( ( ) => false ) , // mock implementation that returns true
93+ } ) ;
7494 TestBed . configureTestingModule ( {
7595 providers : [
7696 DataciteService ,
@@ -164,33 +184,15 @@ describe('DataciteService', () => {
164184 assertSuccess ( httpMock , dataciteTrackerAddress , dataciteTrackerRepoId , doi , DataciteEvent . DOWNLOAD ) ;
165185 } ) ;
166186
167- it ( 'should log error to sentry' , ( done : jest . DoneCallback ) => {
168- const doi = 'qwerty' ;
169- const event = 'view' ;
170- service . logIdentifiableView ( buildObservable ( doi ) ) . subscribe ( {
171- next : ( ) => { } ,
172- error : ( ) => {
173- throw new Error ( 'The error should have been caught and suppressed by the service.' ) ;
174- } ,
175- complete : ( ) => {
176- expect ( sentry . captureException ) . toHaveBeenCalled ( ) ;
177-
178- done ( ) ;
179- } ,
180- } ) ;
187+ it ( 'navigator success' , ( ) => {
188+ ( navigator . sendBeacon as jest . Mock ) . mockReturnValueOnce ( true ) ;
181189
182- const req = httpMock . expectOne ( dataciteTrackerAddress ) ;
183- expect ( req . request . method ) . toBe ( 'POST' ) ;
184- expect ( req . request . body ) . toEqual ( {
185- n : event ,
186- u : window . location . href ,
187- i : dataciteTrackerRepoId ,
188- p : doi ,
189- } ) ;
190- expect ( req . request . headers . get ( 'Content-Type' ) ) . toBe ( 'application/json' ) ;
190+ const doi = 'qwerty' ;
191+ const event = DataciteEvent . VIEW ;
192+ service . logIdentifiableView ( buildObservable ( doi ) ) . subscribe ( ) ;
191193
192- const mockError = new ProgressEvent ( 'Internal Server Error' ) ;
193- req . error ( mockError ) ;
194+ httpMock . expectNone ( dataciteTrackerAddress ) ;
195+ assertSendBeacon ( dataciteTrackerAddress , dataciteTrackerRepoId , doi , event ) ;
194196 } ) ;
195197 } ) ;
196198
0 commit comments