forked from invertase/react-native-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
366 lines (346 loc) · 13.2 KB
/
Copy pathapp.js
File metadata and controls
366 lines (346 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* eslint-disable no-console */
/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import React, { useState } from 'react';
import { StyleSheet, View, StatusBar, AppRegistry, Text, Button } from 'react-native';
import { JetProvider, ConnectionText, StatusEmoji, StatusText } from 'jet';
import { TestComponents } from './local-tests';
let harnessOverrides = {};
try {
harnessOverrides = require('./harness.overrides.js');
} catch (e) {
// Optional local overrides — see harness.overrides.example.js
}
const platformSupportedModules = [];
if (Platform.other) {
platformSupportedModules.push('app');
platformSupportedModules.push('functions');
platformSupportedModules.push('firestore');
platformSupportedModules.push('database');
platformSupportedModules.push('auth');
platformSupportedModules.push('storage');
platformSupportedModules.push('remoteConfig');
platformSupportedModules.push('analytics');
platformSupportedModules.push('appCheck');
platformSupportedModules.push('ai');
// TODO add more modules here once they are supported.
}
if (!Platform.other) {
platformSupportedModules.push('app');
platformSupportedModules.push('functions');
platformSupportedModules.push('auth');
platformSupportedModules.push('database');
platformSupportedModules.push('firestore');
platformSupportedModules.push('storage');
platformSupportedModules.push('messaging');
platformSupportedModules.push('perf');
platformSupportedModules.push('analytics');
platformSupportedModules.push('remoteConfig');
platformSupportedModules.push('crashlytics');
platformSupportedModules.push('inAppMessaging');
platformSupportedModules.push('installations');
platformSupportedModules.push('appCheck');
platformSupportedModules.push('appDistribution');
platformSupportedModules.push('ml');
platformSupportedModules.push('phoneNumberVerification');
platformSupportedModules.push('ai');
}
if (Array.isArray(harnessOverrides.modules) && harnessOverrides.modules.length) {
const allowed = new Set(harnessOverrides.modules);
const filtered = platformSupportedModules.filter(module => allowed.has(module));
platformSupportedModules.splice(0, platformSupportedModules.length, ...filtered);
}
// Registering an error handler that always throw unhandled exceptions
// This is to enable Jet to exit on uncaught errors
const originalHandler = ErrorUtils.getGlobalHandler();
ErrorUtils.setGlobalHandler((err, isFatal) => {
originalHandler(err, isFatal);
throw err;
});
function loadTests(_) {
// this will only execute if Jet executes
// ...so manual tests will not have this setup - emulators etc
// ...that allows them the freedom to define their own test environment
describe('React Native Firebase', function () {
if (!globalThis.RNFBDebug) {
// Only retry tests if not debugging or hunting deprecated API usage locally,
// otherwise it gets annoying to debug.
this.retries(4);
}
before(async function () {
if (platformSupportedModules.includes('functions')) {
const { connectFunctionsEmulator, getFunctions } = functionsModular;
connectFunctionsEmulator(getFunctions(), 'localhost', 5001);
}
if (platformSupportedModules.includes('database')) {
const { connectDatabaseEmulator, getDatabase } = databaseModular;
connectDatabaseEmulator(getDatabase(), 'localhost', 9000);
}
if (platformSupportedModules.includes('auth')) {
const { connectAuthEmulator, getAuth } = authModular;
connectAuthEmulator(getAuth(), 'http://localhost:9099');
}
if (platformSupportedModules.includes('firestore')) {
const { getApp } = modular;
const { connectFirestoreEmulator, clearIndexedDbPersistence, getFirestore } =
firestoreModular;
connectFirestoreEmulator(getFirestore(), 'localhost', 8080);
connectFirestoreEmulator(getFirestore(getApp(), 'second-rnfb'), 'localhost', 8080);
// Firestore caches documents locally (a great feature!) and that confounds tests
// as data from previous runs pollutes following runs until re-install the app. Clear it.
if (!Platform.other) {
clearIndexedDbPersistence(getFirestore());
}
}
if (platformSupportedModules.includes('storage')) {
const { getApp } = modular;
const { getStorage, connectStorageEmulator } = storageModular;
connectStorageEmulator(getStorage(), 'localhost', 9199);
connectStorageEmulator(getStorage(getApp('secondaryFromNative')), 'localhost', 9199);
connectStorageEmulator(
getStorage(getApp(), 'gs://react-native-firebase-testing'),
'localhost',
9199,
);
}
});
afterEach(async function afterEachTest() {
globalThis.RNFBDebugLastTest = this.currentTest.title;
globalThis.RNFBDebugInTestLeakDetection = false;
if (RNFBDebug) {
const emoji = this.currentTest.state === 'passed' ? '✅' : '❌';
console.debug(`[TEST->Finish][${emoji}] ${this.currentTest.title}`);
console.debug('');
}
// Allow time for things to settle between tests.
await Utils.sleep(25);
});
beforeEach(async function beforeEachTest() {
const retry = this.currentTest.currentRetry();
if (RNFBDebug) {
console.debug('');
console.debug('');
console.debug(`[TEST-->Start][🧪] ${this.currentTest.title}`);
}
if (retry > 0) {
if (retry === 1) {
console.log('');
console.warn('⚠️ A test failed:');
console.warn(`️ -> ${this.currentTest.title}`);
}
if (retry > 1) {
console.warn(` 🔴 Retry #${retry - 1} failed...`);
}
console.warn(`️ -> Retrying in ${5 * retry} seconds ... (${retry})`);
await Utils.sleep(5000 * retry);
} else {
// Allow time for things to settle between tests.
await Utils.sleep(50);
}
globalThis.RNFBDebugInTestLeakDetection = true;
});
// Load tests for each Firebase module.
if (platformSupportedModules.includes('app')) {
const appTests = require.context('../packages/app/e2e', true, /\.e2e\.js$/);
appTests.keys().forEach(appTests);
}
if (platformSupportedModules.includes('functions')) {
const functionsTests = require.context('../packages/functions/e2e', true, /\.e2e\.js$/);
functionsTests.keys().forEach(functionsTests);
}
if (platformSupportedModules.includes('appCheck')) {
const appCheckTests = require.context('../packages/app-check/e2e', true, /\.e2e\.js$/);
appCheckTests.keys().forEach(appCheckTests);
}
if (platformSupportedModules.includes('appDistribution')) {
const appDistributionTests = require.context(
'../packages/app-distribution/e2e',
true,
/\.e2e\.js$/,
);
appDistributionTests.keys().forEach(appDistributionTests);
}
if (platformSupportedModules.includes('analytics')) {
const analyticsTests = require.context('../packages/analytics/e2e', true, /\.e2e\.js$/);
analyticsTests.keys().forEach(analyticsTests);
}
if (platformSupportedModules.includes('auth')) {
const authTests = require.context('../packages/auth/e2e', true, /\.e2e\.js$/);
authTests.keys().forEach(authTests);
}
if (platformSupportedModules.includes('crashlytics')) {
const crashlyticsTests = require.context('../packages/crashlytics/e2e', true, /\.e2e\.js$/);
crashlyticsTests.keys().forEach(crashlyticsTests);
}
if (platformSupportedModules.includes('database')) {
const databaseTests = require.context('../packages/database/e2e', true, /\.e2e\.js$/);
databaseTests.keys().forEach(databaseTests);
}
if (platformSupportedModules.includes('firestore')) {
const firestoreTests = require.context('../packages/firestore/e2e', true, /\.e2e\.js$/);
firestoreTests.keys().forEach(firestoreTests);
}
if (platformSupportedModules.includes('perf')) {
const perfTests = require.context('../packages/perf/e2e', true, /\.e2e\.js$/);
perfTests.keys().forEach(perfTests);
}
if (platformSupportedModules.includes('messaging')) {
const messagingTests = require.context('../packages/messaging/e2e', true, /\.e2e\.js$/);
messagingTests.keys().forEach(messagingTests);
}
if (platformSupportedModules.includes('ml')) {
const mlTests = require.context('../packages/ml/e2e', true, /\.e2e\.js$/);
mlTests.keys().forEach(mlTests);
}
if (platformSupportedModules.includes('inAppMessaging')) {
const inAppMessagingTests = require.context(
'../packages/in-app-messaging/e2e',
true,
/\.e2e\.js$/,
);
inAppMessagingTests.keys().forEach(inAppMessagingTests);
}
if (platformSupportedModules.includes('installations')) {
const installationsTests = require.context(
'../packages/installations/e2e',
true,
/\.e2e\.js$/,
);
installationsTests.keys().forEach(installationsTests);
}
if (platformSupportedModules.includes('remoteConfig')) {
const remoteConfigTests = require.context(
'../packages/remote-config/e2e',
true,
/\.e2e\.js$/,
);
remoteConfigTests.keys().forEach(remoteConfigTests);
}
if (platformSupportedModules.includes('phoneNumberVerification')) {
const pnvTests = require.context(
'../packages/phone-number-verification/e2e',
true,
/\.e2e\.js$/,
);
pnvTests.keys().forEach(pnvTests);
}
if (platformSupportedModules.includes('ai')) {
const aiTests = require.context('../packages/ai/e2e', true, /\.e2e\.js$/);
aiTests.keys().forEach(aiTests);
}
if (platformSupportedModules.includes('storage')) {
const storageTests = require.context('../packages/storage/e2e', true, /\.e2e\.js$/);
storageTests.keys().forEach(storageTests);
}
after(async function flushNativeCoverageProfile() {
if (Platform.OS !== 'ios' && Platform.OS !== 'android') {
return;
}
const { NativeModules } = require('react-native');
const coverageModule = NativeModules.RNFBTestingCoverage;
if (coverageModule?.flush) {
console.log(`[native-coverage] flushing ${Platform.OS} coverage from Jet after hook`);
try {
await coverageModule.flush();
} catch (error) {
if (error?.code === 'coverage_not_enabled') {
console.warn(
`[native-coverage] ${Platform.OS} coverage not enabled in this build; skipping flush`,
);
} else {
console.error(`[native-coverage] failed to flush ${Platform.OS} coverage:`, error);
}
}
} else {
console.warn(
'[native-coverage] RNFBTestingCoverage native module not available; skipping flush',
);
}
});
});
}
function App() {
const [showManualTestPicker, setShowManualTestPicker] = useState(false);
if (showManualTestPicker) {
return (
<>
<StatusBar hidden />
<View style={styles.container}>
<View style={styles.hardRule} />
<Button title="Hide Manual Tests" onPress={() => setShowManualTestPicker(false)} />
<View style={styles.hardRule} />
<TestComponents />
</View>
</>
);
}
return (
<>
<StatusBar hidden />
<View style={styles.container}>
<View style={styles.hardRule} />
<Button title="Show Manual Tests" onPress={() => setShowManualTestPicker(true)} />
<View style={styles.hardRule} />
<Text>Automated Tests:</Text>
<JetProvider tests={loadTests}>
<ConnectionText style={styles.connectionText} />
<View style={styles.statusContainer}>
<StatusEmoji style={styles.statusEmoji} />
<StatusText style={styles.statusText} />
</View>
</JetProvider>
</View>
</>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
// instead of depending on react-native-safe-area-context to avoid
// colliding with system UI we are just going to pad things out for simplicity
marginTop: 60,
paddingHorizontal: '5%',
paddingBottom: '5%',
},
statusContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
statusEmoji: {
fontSize: 30,
margin: 30,
textAlign: 'center',
},
statusText: {
fontSize: 20,
margin: 20,
textAlign: 'center',
color: 'black',
},
connectionText: {
textAlign: 'center',
color: 'black',
},
hardRule: {
height: 1,
backgroundColor: 'black',
width: '100%',
},
});
AppRegistry.registerComponent('testing', () => App);