15
15
* limitations under the License.
16
16
*/
17
17
18
- import { stub , useFakeTimers , SinonFakeTimers } from 'sinon' ;
18
+ import { stub , useFakeTimers , SinonFakeTimers , SinonStub } from 'sinon' ;
19
19
import { use , expect } from 'chai' ;
20
20
import sinonChai from 'sinon-chai' ;
21
21
import {
@@ -28,9 +28,9 @@ import { SettingsService } from './settings_service';
28
28
use ( sinonChai ) ;
29
29
30
30
/* eslint-disable no-restricted-properties */
31
- describe . only ( 'Firebase Performance > transport_service' , ( ) => {
32
- const sendBeaconStub = stub ( navigator , 'sendBeacon' ) ;
33
- const fetchStub = stub ( window , 'fetch' ) ;
31
+ describe ( 'Firebase Performance > transport_service' , ( ) => {
32
+ let sendBeaconStub : SinonStub < [ url : string | URL , data ?: BodyInit | null | undefined ] , boolean > ;
33
+ let fetchStub : SinonStub < [ RequestInfo | URL , RequestInit ? ] , Promise < Response > > ;
34
34
const INITIAL_SEND_TIME_DELAY_MS = 5.5 * 1000 ;
35
35
const DEFAULT_SEND_INTERVAL_MS = 10 * 1000 ;
36
36
const MAX_EVENT_COUNT_PER_REQUEST = 1000 ;
@@ -43,7 +43,9 @@ describe.only('Firebase Performance > transport_service', () => {
43
43
beforeEach ( ( ) => {
44
44
clock = useFakeTimers ( 1 ) ;
45
45
setupTransportService ( ) ;
46
+ sendBeaconStub = stub ( navigator , 'sendBeacon' ) ;
46
47
sendBeaconStub . returns ( true ) ;
48
+ fetchStub = stub ( window , 'fetch' ) ;
47
49
} ) ;
48
50
49
51
afterEach ( ( ) => {
@@ -128,49 +130,13 @@ describe.only('Firebase Performance > transport_service', () => {
128
130
129
131
it ( 'falls back to fetch if sendBeacon fails.' , async ( ) => {
130
132
sendBeaconStub . returns ( false ) ;
131
- // Arrange
132
- const setting = SettingsService . getInstance ( ) ;
133
- const flTransportFullUrl =
134
- setting . flTransportEndpointUrl + '?key=' + setting . transportKey ;
135
-
136
- // Act
137
- // Generate 1020 events, which should be dispatched in two batches (1000 events and 20 events).
138
- for ( let i = 0 ; i < 1020 ; i ++ ) {
139
- testTransportHandler ( 'event' + i ) ;
140
- }
141
- // Wait for first and second event dispatch to happen.
133
+ fetchStub . resolves ( new Response ( '{}' , {
134
+ status : 200 ,
135
+ headers : { 'Content-type' : 'application/json' }
136
+ } ) ) ;
137
+ testTransportHandler ( 'event1' ) ;
142
138
clock . tick ( INITIAL_SEND_TIME_DELAY_MS ) ;
143
- // This is to resolve the floating promise chain in transport service.
144
- await Promise . resolve ( ) . then ( ) . then ( ) . then ( ) ;
145
- clock . tick ( DEFAULT_SEND_INTERVAL_MS ) ;
146
-
147
- // Assert
148
- // Expects the first logRequest which contains first 1000 events.
149
- const firstLogRequest = generateLogRequest ( '5501' ) ;
150
- for ( let i = 0 ; i < MAX_EVENT_COUNT_PER_REQUEST ; i ++ ) {
151
- firstLogRequest [ 'log_event' ] . push ( {
152
- 'source_extension_json_proto3' : 'event' + i ,
153
- 'event_time_ms' : '1'
154
- } ) ;
155
- }
156
- expect ( fetchStub ) . to . not . have . been . called ;
157
- expect ( sendBeaconStub ) . which . to . have . been . calledWith (
158
- flTransportFullUrl ,
159
- JSON . stringify ( firstLogRequest )
160
- ) ;
161
- // Expects the second logRequest which contains remaining 20 events;
162
- const secondLogRequest = generateLogRequest ( '15501' ) ;
163
- for ( let i = 0 ; i < 20 ; i ++ ) {
164
- secondLogRequest [ 'log_event' ] . push ( {
165
- 'source_extension_json_proto3' :
166
- 'event' + ( MAX_EVENT_COUNT_PER_REQUEST + i ) ,
167
- 'event_time_ms' : '1'
168
- } ) ;
169
- }
170
- expect ( sendBeaconStub ) . calledWith (
171
- flTransportFullUrl ,
172
- JSON . stringify ( secondLogRequest )
173
- ) ;
139
+ expect ( fetchStub ) . to . have . been . calledOnce ;
174
140
} ) ;
175
141
176
142
function generateLogRequest ( requestTimeMs : string ) : any {
0 commit comments