15
15
* limitations under the License.
16
16
*/
17
17
18
- import { stub , useFakeTimers , SinonStub , SinonFakeTimers , match } from 'sinon' ;
18
+ import { spy , useFakeTimers , SinonFakeTimers } from 'sinon' ;
19
19
import { use , expect } from 'chai' ;
20
20
import sinonChai from 'sinon-chai' ;
21
21
import {
@@ -27,31 +27,27 @@ import { SettingsService } from './settings_service';
27
27
28
28
use ( sinonChai ) ;
29
29
30
- describe ( 'Firebase Performance > transport_service' , ( ) => {
31
- let fetchStub : SinonStub <
32
- [ RequestInfo | URL , RequestInit ?] ,
33
- Promise < Response >
34
- > ;
30
+ // eslint-disable-next-line no-restricted-properties
31
+ describe . only ( 'Firebase Performance > transport_service' , ( ) => {
32
+ const sendBeaconSpy = spy ( navigator , 'sendBeacon' ) ;
35
33
const INITIAL_SEND_TIME_DELAY_MS = 5.5 * 1000 ;
36
34
const DEFAULT_SEND_INTERVAL_MS = 10 * 1000 ;
37
35
const MAX_EVENT_COUNT_PER_REQUEST = 1000 ;
38
- const TRANSPORT_DELAY_INTERVAL = 10000 ;
39
36
// Starts date at timestamp 1 instead of 0, otherwise it causes validation errors.
40
37
let clock : SinonFakeTimers ;
41
38
const testTransportHandler = transportHandler ( ( ...args ) => {
42
39
return args [ 0 ] ;
43
40
} ) ;
44
41
45
42
beforeEach ( ( ) => {
46
- fetchStub = stub ( window , 'fetch' ) ;
47
43
clock = useFakeTimers ( 1 ) ;
48
44
setupTransportService ( ) ;
49
45
} ) ;
50
46
51
47
afterEach ( ( ) => {
52
- fetchStub . restore ( ) ;
53
48
clock . restore ( ) ;
54
49
resetTransportService ( ) ;
50
+ sendBeaconSpy . resetHistory ( ) ;
55
51
} ) ;
56
52
57
53
it ( 'throws an error when logging an empty message' , ( ) => {
@@ -61,43 +57,21 @@ describe('Firebase Performance > transport_service', () => {
61
57
} ) ;
62
58
63
59
it ( 'does not attempt to log an event after INITIAL_SEND_TIME_DELAY_MS if queue is empty' , ( ) => {
64
- fetchStub . resolves (
65
- new Response ( '' , {
66
- status : 200 ,
67
- headers : { 'Content-type' : 'application/json' }
68
- } )
69
- ) ;
70
-
71
60
clock . tick ( INITIAL_SEND_TIME_DELAY_MS ) ;
72
- expect ( fetchStub ) . to . not . have . been . called ;
61
+ expect ( sendBeaconSpy ) . to . not . have . been . called ;
73
62
} ) ;
74
63
75
64
it ( 'attempts to log an event after DEFAULT_SEND_INTERVAL_MS if queue not empty' , async ( ) => {
76
- fetchStub . resolves (
77
- new Response ( '' , {
78
- status : 200 ,
79
- headers : { 'Content-type' : 'application/json' }
80
- } )
81
- ) ;
82
-
83
65
clock . tick ( INITIAL_SEND_TIME_DELAY_MS ) ;
84
66
testTransportHandler ( 'someEvent' ) ;
85
67
clock . tick ( DEFAULT_SEND_INTERVAL_MS ) ;
86
- expect ( fetchStub ) . to . have . been . calledOnce ;
68
+ expect ( sendBeaconSpy ) . to . have . been . calledOnce ;
87
69
} ) ;
88
70
89
71
it ( 'successful send a message to transport' , ( ) => {
90
- const setting = SettingsService . getInstance ( ) ;
91
- const flTransportFullUrl =
92
- setting . flTransportEndpointUrl + '?key=' + setting . transportKey ;
93
- fetchStub . withArgs ( flTransportFullUrl , match . any ) . resolves (
94
- // DELETE_REQUEST means event dispatch is successful.
95
- generateSuccessResponse ( )
96
- ) ;
97
-
98
72
testTransportHandler ( 'event1' ) ;
99
73
clock . tick ( INITIAL_SEND_TIME_DELAY_MS ) ;
100
- expect ( fetchStub ) . to . have . been . calledOnce ;
74
+ expect ( sendBeaconSpy ) . to . have . been . calledOnce ;
101
75
} ) ;
102
76
103
77
it ( 'sends up to the maximum event limit in one request' , async ( ) => {
@@ -106,11 +80,6 @@ describe('Firebase Performance > transport_service', () => {
106
80
const flTransportFullUrl =
107
81
setting . flTransportEndpointUrl + '?key=' + setting . transportKey ;
108
82
109
- // Returns successful response from fl for logRequests.
110
- const response = generateSuccessResponse ( ) ;
111
- stub ( response , 'json' ) . resolves ( JSON . parse ( generateSuccessResponseBody ( ) ) ) ;
112
- fetchStub . resolves ( response ) ;
113
-
114
83
// Act
115
84
// Generate 1020 events, which should be dispatched in two batches (1000 events and 20 events).
116
85
for ( let i = 0 ; i < 1020 ; i ++ ) {
@@ -131,10 +100,10 @@ describe('Firebase Performance > transport_service', () => {
131
100
'event_time_ms' : '1'
132
101
} ) ;
133
102
}
134
- expect ( fetchStub ) . which . to . have . been . calledWith ( flTransportFullUrl , {
135
- method : 'POST' ,
136
- body : JSON . stringify ( firstLogRequest )
137
- } ) ;
103
+ expect ( sendBeaconSpy ) . which . to . have . been . calledWith (
104
+ flTransportFullUrl ,
105
+ JSON . stringify ( firstLogRequest )
106
+ ) ;
138
107
// Expects the second logRequest which contains remaining 20 events;
139
108
const secondLogRequest = generateLogRequest ( '15501' ) ;
140
109
for ( let i = 0 ; i < 20 ; i ++ ) {
@@ -144,10 +113,10 @@ describe('Firebase Performance > transport_service', () => {
144
113
'event_time_ms' : '1'
145
114
} ) ;
146
115
}
147
- expect ( fetchStub ) . calledWith ( flTransportFullUrl , {
148
- method : 'POST' ,
149
- body : JSON . stringify ( secondLogRequest )
150
- } ) ;
116
+ expect ( sendBeaconSpy ) . calledWith (
117
+ flTransportFullUrl ,
118
+ JSON . stringify ( secondLogRequest )
119
+ ) ;
151
120
} ) ;
152
121
153
122
function generateLogRequest ( requestTimeMs : string ) : any {
@@ -161,26 +130,4 @@ describe('Firebase Performance > transport_service', () => {
161
130
'log_event' : [ ] as any
162
131
} ;
163
132
}
164
-
165
- function generateSuccessResponse ( ) : Response {
166
- return new Response ( generateSuccessResponseBody ( ) , {
167
- status : 200 ,
168
- headers : { 'Content-type' : 'application/json' }
169
- } ) ;
170
- }
171
-
172
- function generateSuccessResponseBody ( ) : string {
173
- return (
174
- '{\
175
- "nextRequestWaitMillis": "' +
176
- TRANSPORT_DELAY_INTERVAL +
177
- '",\
178
- "logResponseDetails": [\
179
- {\
180
- "responseAction": "DELETE_REQUEST"\
181
- }\
182
- ]\
183
- }'
184
- ) ;
185
- }
186
133
} ) ;
0 commit comments