Skip to content

Commit f935658

Browse files
committed
Update tests
1 parent 90a4e3a commit f935658

File tree

1 file changed

+12
-46
lines changed

1 file changed

+12
-46
lines changed

packages/performance/src/services/transport_service.test.ts

+12-46
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { stub, useFakeTimers, SinonFakeTimers } from 'sinon';
18+
import { stub, useFakeTimers, SinonFakeTimers, SinonStub } from 'sinon';
1919
import { use, expect } from 'chai';
2020
import sinonChai from 'sinon-chai';
2121
import {
@@ -28,9 +28,9 @@ import { SettingsService } from './settings_service';
2828
use(sinonChai);
2929

3030
/* 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>>;
3434
const INITIAL_SEND_TIME_DELAY_MS = 5.5 * 1000;
3535
const DEFAULT_SEND_INTERVAL_MS = 10 * 1000;
3636
const MAX_EVENT_COUNT_PER_REQUEST = 1000;
@@ -43,7 +43,9 @@ describe.only('Firebase Performance > transport_service', () => {
4343
beforeEach(() => {
4444
clock = useFakeTimers(1);
4545
setupTransportService();
46+
sendBeaconStub = stub(navigator, 'sendBeacon');
4647
sendBeaconStub.returns(true);
48+
fetchStub = stub(window, 'fetch');
4749
});
4850

4951
afterEach(() => {
@@ -128,49 +130,13 @@ describe.only('Firebase Performance > transport_service', () => {
128130

129131
it('falls back to fetch if sendBeacon fails.', async () => {
130132
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');
142138
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;
174140
});
175141

176142
function generateLogRequest(requestTimeMs: string): any {

0 commit comments

Comments
 (0)