1515 * limitations under the License.
1616 */
1717
18- import { stub , useFakeTimers , SinonStub , SinonFakeTimers , match } from 'sinon' ;
18+ import { spy , useFakeTimers , SinonFakeTimers } from 'sinon' ;
1919import { use , expect } from 'chai' ;
2020import sinonChai from 'sinon-chai' ;
2121import {
@@ -27,31 +27,27 @@ import { SettingsService } from './settings_service';
2727
2828use ( sinonChai ) ;
2929
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' ) ;
3533 const INITIAL_SEND_TIME_DELAY_MS = 5.5 * 1000 ;
3634 const DEFAULT_SEND_INTERVAL_MS = 10 * 1000 ;
3735 const MAX_EVENT_COUNT_PER_REQUEST = 1000 ;
38- const TRANSPORT_DELAY_INTERVAL = 10000 ;
3936 // Starts date at timestamp 1 instead of 0, otherwise it causes validation errors.
4037 let clock : SinonFakeTimers ;
4138 const testTransportHandler = transportHandler ( ( ...args ) => {
4239 return args [ 0 ] ;
4340 } ) ;
4441
4542 beforeEach ( ( ) => {
46- fetchStub = stub ( window , 'fetch' ) ;
4743 clock = useFakeTimers ( 1 ) ;
4844 setupTransportService ( ) ;
4945 } ) ;
5046
5147 afterEach ( ( ) => {
52- fetchStub . restore ( ) ;
5348 clock . restore ( ) ;
5449 resetTransportService ( ) ;
50+ sendBeaconSpy . resetHistory ( ) ;
5551 } ) ;
5652
5753 it ( 'throws an error when logging an empty message' , ( ) => {
@@ -61,43 +57,21 @@ describe('Firebase Performance > transport_service', () => {
6157 } ) ;
6258
6359 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-
7160 clock . tick ( INITIAL_SEND_TIME_DELAY_MS ) ;
72- expect ( fetchStub ) . to . not . have . been . called ;
61+ expect ( sendBeaconSpy ) . to . not . have . been . called ;
7362 } ) ;
7463
7564 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-
8365 clock . tick ( INITIAL_SEND_TIME_DELAY_MS ) ;
8466 testTransportHandler ( 'someEvent' ) ;
8567 clock . tick ( DEFAULT_SEND_INTERVAL_MS ) ;
86- expect ( fetchStub ) . to . have . been . calledOnce ;
68+ expect ( sendBeaconSpy ) . to . have . been . calledOnce ;
8769 } ) ;
8870
8971 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-
9872 testTransportHandler ( 'event1' ) ;
9973 clock . tick ( INITIAL_SEND_TIME_DELAY_MS ) ;
100- expect ( fetchStub ) . to . have . been . calledOnce ;
74+ expect ( sendBeaconSpy ) . to . have . been . calledOnce ;
10175 } ) ;
10276
10377 it ( 'sends up to the maximum event limit in one request' , async ( ) => {
@@ -106,11 +80,6 @@ describe('Firebase Performance > transport_service', () => {
10680 const flTransportFullUrl =
10781 setting . flTransportEndpointUrl + '?key=' + setting . transportKey ;
10882
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-
11483 // Act
11584 // Generate 1020 events, which should be dispatched in two batches (1000 events and 20 events).
11685 for ( let i = 0 ; i < 1020 ; i ++ ) {
@@ -131,10 +100,10 @@ describe('Firebase Performance > transport_service', () => {
131100 'event_time_ms' : '1'
132101 } ) ;
133102 }
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+ ) ;
138107 // Expects the second logRequest which contains remaining 20 events;
139108 const secondLogRequest = generateLogRequest ( '15501' ) ;
140109 for ( let i = 0 ; i < 20 ; i ++ ) {
@@ -144,10 +113,10 @@ describe('Firebase Performance > transport_service', () => {
144113 'event_time_ms' : '1'
145114 } ) ;
146115 }
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+ ) ;
151120 } ) ;
152121
153122 function generateLogRequest ( requestTimeMs : string ) : any {
@@ -161,26 +130,4 @@ describe('Firebase Performance > transport_service', () => {
161130 'log_event' : [ ] as any
162131 } ;
163132 }
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- }
186133} ) ;
0 commit comments