@@ -34,30 +34,37 @@ try {
34
34
}
35
35
36
36
const RETRY_INTERVAL = 1000 ;
37
+ const TIMEOUT_MILLIS = 20000 ;
38
+
39
+ async function checkForEventCalls ( retryCount = 0 ) : Promise < PerformanceEntry [ ] > {
40
+ if ( retryCount > TIMEOUT_MILLIS / RETRY_INTERVAL ) {
41
+ return Promise . resolve ( [ ] ) ;
42
+ }
43
+ await new Promise ( resolve => setTimeout ( resolve , RETRY_INTERVAL ) ) ;
44
+ const resources = performance . getEntriesByType ( 'resource' ) ;
45
+ performance . clearResourceTimings ( ) ;
46
+ const callsWithEvent = resources . filter (
47
+ resource =>
48
+ resource . name . includes ( 'google-analytics.com' ) &&
49
+ resource . name . includes ( 'en=login' )
50
+ ) ;
51
+ if ( callsWithEvent . length === 0 ) {
52
+ return checkForEventCalls ( retryCount + 1 ) ;
53
+ } else {
54
+ return callsWithEvent ;
55
+ }
56
+ }
37
57
38
58
describe ( 'FirebaseAnalytics Integration Smoke Tests' , ( ) => {
39
59
let app : FirebaseApp ;
40
60
describe ( 'Using getAnalytics()' , ( ) => {
41
61
afterEach ( ( ) => deleteApp ( app ) ) ;
42
62
it ( 'logEvent() sends correct network request.' , async ( ) => {
43
63
app = initializeApp ( config ) ;
44
- logEvent ( getAnalytics ( app ) , 'login' , { method : 'email' } ) ;
45
- async function checkForEventCalls ( ) : Promise < number > {
46
- await new Promise ( resolve => setTimeout ( resolve , RETRY_INTERVAL ) ) ;
47
- const resources = performance . getEntriesByType ( 'resource' ) ;
48
- const callsWithEvent = resources . filter (
49
- resource =>
50
- resource . name . includes ( 'google-analytics.com' ) &&
51
- resource . name . includes ( 'en=login' )
52
- ) ;
53
- if ( callsWithEvent . length === 0 ) {
54
- return checkForEventCalls ( ) ;
55
- } else {
56
- return callsWithEvent . length ;
57
- }
58
- }
59
- const eventCallCount = await checkForEventCalls ( ) ;
60
- expect ( eventCallCount ) . to . equal ( 1 ) ;
64
+ logEvent ( getAnalytics ( app ) , 'login' , { method : 'phone' } ) ;
65
+ const eventCalls = await checkForEventCalls ( ) ;
66
+ expect ( eventCalls . length ) . to . equal ( 1 ) ;
67
+ expect ( eventCalls [ 0 ] . name ) . to . include ( 'method=phone' ) ;
61
68
} ) ;
62
69
it ( "Warns if measurement ID doesn't match." , done => {
63
70
const warnStub = stub ( console , 'warn' ) . callsFake ( ( ) => {
@@ -75,20 +82,6 @@ describe('FirebaseAnalytics Integration Smoke Tests', () => {
75
82
it ( 'logEvent() sends correct network request.' , async ( ) => {
76
83
app = initializeApp ( config ) ;
77
84
logEvent ( initializeAnalytics ( app ) , 'login' , { method : 'email' } ) ;
78
- async function checkForEventCalls ( ) : Promise < PerformanceEntry [ ] > {
79
- await new Promise ( resolve => setTimeout ( resolve , RETRY_INTERVAL ) ) ;
80
- const resources = performance . getEntriesByType ( 'resource' ) ;
81
- const callsWithEvent = resources . filter (
82
- resource =>
83
- resource . name . includes ( 'google-analytics.com' ) &&
84
- resource . name . includes ( 'en=login' )
85
- ) ;
86
- if ( callsWithEvent . length === 0 ) {
87
- return checkForEventCalls ( ) ;
88
- } else {
89
- return callsWithEvent ;
90
- }
91
- }
92
85
const eventCalls = await checkForEventCalls ( ) ;
93
86
expect ( eventCalls . length ) . to . equal ( 1 ) ;
94
87
expect ( eventCalls [ 0 ] . name ) . to . include ( 'method=email' ) ;
0 commit comments