16
16
*/
17
17
18
18
import { expect } from 'chai' ;
19
- import { stub } from 'sinon' ;
20
19
import { initializePerformance } from './index' ;
21
20
import { ERROR_FACTORY , ErrorCode } from './utils/errors' ;
22
- import * as firebase from '@firebase/app-exp' ;
23
- import { Provider } from '@firebase/component' ;
24
21
import '../test/setup' ;
22
+ import { deleteApp , FirebaseApp , initializeApp } from '@firebase/app-exp' ;
25
23
26
24
const fakeFirebaseConfig = {
27
25
apiKey : 'api-key' ,
@@ -33,18 +31,43 @@ const fakeFirebaseConfig = {
33
31
appId : '1:111:web:a1234'
34
32
} ;
35
33
36
- const fakeFirebaseApp = ( {
37
- options : fakeFirebaseConfig
38
- } as unknown ) as firebase . FirebaseApp ;
39
-
40
34
describe ( 'Firebase Performance > initializePerformance()' , ( ) => {
41
- it ( 'throws if a perf instance has already been created' , ( ) => {
42
- stub ( firebase , '_getProvider' ) . returns ( ( {
43
- isInitialized : ( ) => true
44
- } as unknown ) as Provider < 'performance-exp' > ) ;
35
+ let app : FirebaseApp ;
36
+ beforeEach ( ( ) => {
37
+ app = initializeApp ( fakeFirebaseConfig ) ;
38
+ } ) ;
39
+ afterEach ( ( ) => {
40
+ return deleteApp ( app ) ;
41
+ } ) ;
42
+ it ( 'returns same instance if given same (no) params a second time' , ( ) => {
43
+ const performanceInstance = initializePerformance ( app ) ;
44
+ expect ( initializePerformance ( app ) ) . to . equal ( performanceInstance ) ;
45
+ } ) ;
46
+ it ( 'returns same instance if given same params a second time' , ( ) => {
47
+ const performanceInstance = initializePerformance ( app , {
48
+ dataCollectionEnabled : false
49
+ } ) ;
50
+ expect (
51
+ initializePerformance ( app , { dataCollectionEnabled : false } )
52
+ ) . to . equal ( performanceInstance ) ;
53
+ } ) ;
54
+ it ( 'throws if called with params after being called with no params' , ( ) => {
55
+ initializePerformance ( app ) ;
56
+ const expectedError = ERROR_FACTORY . create ( ErrorCode . ALREADY_INITIALIZED ) ;
57
+ expect ( ( ) =>
58
+ initializePerformance ( app , { dataCollectionEnabled : false } )
59
+ ) . to . throw ( expectedError . message ) ;
60
+ } ) ;
61
+ it ( 'throws if called with no params after being called with params' , ( ) => {
62
+ initializePerformance ( app , { instrumentationEnabled : false } ) ;
63
+ const expectedError = ERROR_FACTORY . create ( ErrorCode . ALREADY_INITIALIZED ) ;
64
+ expect ( ( ) => initializePerformance ( app ) ) . to . throw ( expectedError . message ) ;
65
+ } ) ;
66
+ it ( 'throws if called a second time with different params' , ( ) => {
67
+ initializePerformance ( app , { instrumentationEnabled : true } ) ;
45
68
const expectedError = ERROR_FACTORY . create ( ErrorCode . ALREADY_INITIALIZED ) ;
46
- expect ( ( ) => initializePerformance ( fakeFirebaseApp ) ) . to . throw (
47
- expectedError . message
48
- ) ;
69
+ expect ( ( ) =>
70
+ initializePerformance ( app , { instrumentationEnabled : false } )
71
+ ) . to . throw ( expectedError . message ) ;
49
72
} ) ;
50
73
} ) ;
0 commit comments