1818import { expect } from 'chai' ;
1919import { stub } from 'sinon' ;
2020import { PerformanceController } from '../controllers/perf' ;
21- import { Trace } from '../resources/trace' ;
2221import { Api , setupApi } from '../services/api_service' ;
23- import { FirebaseApp } from '@firebase/app-types-exp' ;
2422import * as initializationService from '../services/initialization_service' ;
25- import * as FirebaseUtil from '@firebase/util ' ;
23+ import { SettingsService } from '../services/settings_service ' ;
2624import { consoleLogger } from '../utils/console_logger' ;
25+ import { FirebaseApp } from '@firebase/app-types-exp' ;
26+ import { _FirebaseInstallationsInternal } from '@firebase/installations-types-exp' ;
2727import '../../test/setup' ;
2828
2929describe ( 'Firebase Performance Test' , ( ) => {
@@ -43,95 +43,109 @@ describe('Firebase Performance Test', () => {
4343 options : fakeFirebaseConfig
4444 } as unknown ) as FirebaseApp ;
4545
46+ const fakeInstallations = ( { } as unknown ) as _FirebaseInstallationsInternal ;
47+
4648 describe ( '#constructor' , ( ) => {
4749 it ( 'does not initialize performance if the required apis are not available' , ( ) => {
4850 stub ( Api . prototype , 'requiredApisAvailable' ) . returns ( false ) ;
49- stub ( initializationService , 'getInitializationPromise' ) ;
50- new PerformanceController ( fakeFirebaseApp ) ;
51- expect ( initializationService . getInitializationPromise ) . not . be . called ;
52- } ) ;
53- it ( 'does not initialize performance if validateIndexedDBOpenable return false' , async ( ) => {
54- stub ( Api . prototype , 'requiredApisAvailable' ) . returns ( true ) ;
55- const validateStub = stub (
56- FirebaseUtil ,
57- 'validateIndexedDBOpenable'
58- ) . resolves ( false ) ;
59- stub ( initializationService , 'getInitializationPromise' ) ;
60- new PerformanceController ( fakeFirebaseApp ) ;
61- await validateStub ;
62- expect ( initializationService . getInitializationPromise ) . not . be . called ;
63- } ) ;
64-
65- it ( 'does not initialize performance if validateIndexedDBOpenable throws an error' , async ( ) => {
66- stub ( Api . prototype , 'requiredApisAvailable' ) . returns ( true ) ;
67- const validateStub = stub (
68- FirebaseUtil ,
69- 'validateIndexedDBOpenable'
70- ) . rejects ( ) ;
71-
7251 stub ( initializationService , 'getInitializationPromise' ) ;
7352 stub ( consoleLogger , 'info' ) ;
74- new PerformanceController ( fakeFirebaseApp ) ;
75- try {
76- await validateStub ;
77- expect ( initializationService . getInitializationPromise ) . not . be . called ;
78- expect ( consoleLogger . info ) . be . called ;
79- } catch ( ignored ) { }
80- } ) ;
81- } ) ;
53+ const performanceController = new PerformanceController (
54+ fakeFirebaseApp ,
55+ fakeInstallations
56+ ) ;
57+ performanceController . _init ( ) ;
8258
83- describe ( '#trace' , ( ) => {
84- it ( 'creates a custom trace' , ( ) => {
85- const controller = new PerformanceController ( fakeFirebaseApp ) ;
86- const myTrace = controller . trace ( 'myTrace' ) ;
87-
88- expect ( myTrace ) . to . be . instanceOf ( Trace ) ;
89- } ) ;
90-
91- it ( 'custom trace has the correct name' , ( ) => {
92- const controller = new PerformanceController ( fakeFirebaseApp ) ;
93- const myTrace = controller . trace ( 'myTrace' ) ;
94-
95- expect ( myTrace . name ) . is . equal ( 'myTrace' ) ;
96- } ) ;
97-
98- it ( 'custom trace is not auto' , ( ) => {
99- const controller = new PerformanceController ( fakeFirebaseApp ) ;
100- const myTrace = controller . trace ( 'myTrace' ) ;
101-
102- expect ( myTrace . isAuto ) . is . equal ( false ) ;
59+ expect ( initializationService . getInitializationPromise ) . not . be . called ;
60+ expect ( consoleLogger . info ) . to . be . calledWithMatch (
61+ / .* F e t c h .* P r o m i s e .* c o o k i e s .* /
62+ ) ;
10363 } ) ;
10464 } ) ;
10565
106- describe ( '#instrumentationEnabled' , ( ) => {
107- it ( 'sets instrumentationEnabled to enabled' , async ( ) => {
108- const controller = new PerformanceController ( fakeFirebaseApp ) ;
109-
110- controller . instrumentationEnabled = true ;
111- expect ( controller . instrumentationEnabled ) . is . equal ( true ) ;
66+ describe ( '#settings' , ( ) => {
67+ it ( 'applies the settings if provided' , async ( ) => {
68+ const settings = {
69+ instrumentationEnabled : false ,
70+ dataCollectionEnabled : false
71+ } ;
72+
73+ const performance = new PerformanceController (
74+ fakeFirebaseApp ,
75+ fakeInstallations
76+ ) ;
77+ performance . _init ( settings ) ;
78+
79+ expect ( performance . instrumentationEnabled ) . is . equal ( false ) ;
80+ expect ( performance . dataCollectionEnabled ) . is . equal ( false ) ;
11281 } ) ;
11382
114- it ( 'sets instrumentationEnabled to disabled' , async ( ) => {
115- const controller = new PerformanceController ( fakeFirebaseApp ) ;
116-
117- controller . instrumentationEnabled = false ;
118- expect ( controller . instrumentationEnabled ) . is . equal ( false ) ;
83+ it ( 'uses defaults when settings are not provided' , async ( ) => {
84+ const expectedInstrumentationEnabled = SettingsService . getInstance ( )
85+ . instrumentationEnabled ;
86+ const expectedDataCollectionEnabled = SettingsService . getInstance ( )
87+ . dataCollectionEnabled ;
88+
89+ const performance = new PerformanceController (
90+ fakeFirebaseApp ,
91+ fakeInstallations
92+ ) ;
93+ performance . _init ( ) ;
94+
95+ expect ( performance . instrumentationEnabled ) . is . equal (
96+ expectedInstrumentationEnabled
97+ ) ;
98+ expect ( performance . dataCollectionEnabled ) . is . equal (
99+ expectedDataCollectionEnabled
100+ ) ;
119101 } ) ;
120- } ) ;
121-
122- describe ( '#dataCollectionEnabled' , ( ) => {
123- it ( 'sets dataCollectionEnabled to enabled' , async ( ) => {
124- const controller = new PerformanceController ( fakeFirebaseApp ) ;
125102
126- controller . dataCollectionEnabled = true ;
127- expect ( controller . dataCollectionEnabled ) . is . equal ( true ) ;
103+ describe ( '#instrumentationEnabled' , ( ) => {
104+ it ( 'sets instrumentationEnabled to enabled' , async ( ) => {
105+ const performance = new PerformanceController (
106+ fakeFirebaseApp ,
107+ fakeInstallations
108+ ) ;
109+ performance . _init ( ) ;
110+
111+ performance . instrumentationEnabled = true ;
112+ expect ( performance . instrumentationEnabled ) . is . equal ( true ) ;
113+ } ) ;
114+
115+ it ( 'sets instrumentationEnabled to disabled' , async ( ) => {
116+ const performance = new PerformanceController (
117+ fakeFirebaseApp ,
118+ fakeInstallations
119+ ) ;
120+ performance . _init ( ) ;
121+
122+ performance . instrumentationEnabled = false ;
123+ expect ( performance . instrumentationEnabled ) . is . equal ( false ) ;
124+ } ) ;
128125 } ) ;
129126
130- it ( 'sets dataCollectionEnabled to disabled' , ( ) => {
131- const controller = new PerformanceController ( fakeFirebaseApp ) ;
132-
133- controller . dataCollectionEnabled = false ;
134- expect ( controller . dataCollectionEnabled ) . is . equal ( false ) ;
127+ describe ( '#dataCollectionEnabled' , ( ) => {
128+ it ( 'sets dataCollectionEnabled to enabled' , async ( ) => {
129+ const performance = new PerformanceController (
130+ fakeFirebaseApp ,
131+ fakeInstallations
132+ ) ;
133+ performance . _init ( ) ;
134+
135+ performance . dataCollectionEnabled = true ;
136+ expect ( performance . dataCollectionEnabled ) . is . equal ( true ) ;
137+ } ) ;
138+
139+ it ( 'sets dataCollectionEnabled to disabled' , ( ) => {
140+ const performance = new PerformanceController (
141+ fakeFirebaseApp ,
142+ fakeInstallations
143+ ) ;
144+ performance . _init ( ) ;
145+
146+ performance . dataCollectionEnabled = false ;
147+ expect ( performance . dataCollectionEnabled ) . is . equal ( false ) ;
148+ } ) ;
135149 } ) ;
136150 } ) ;
137151} ) ;
0 commit comments