forked from invertase/react-native-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype-test.ts
More file actions
304 lines (247 loc) · 7.35 KB
/
Copy pathtype-test.ts
File metadata and controls
304 lines (247 loc) · 7.35 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
import database, {
firebase,
FirebaseDatabaseTypes,
getDatabase,
connectDatabaseEmulator,
goOffline,
goOnline,
ref,
refFromURL,
setPersistenceEnabled,
setLoggingEnabled,
setPersistenceCacheSizeBytes,
serverTimestamp,
getServerTime,
increment,
ServerValue,
endAt,
endBefore,
startAt,
startAfter,
limitToFirst,
limitToLast,
orderByChild,
orderByKey,
orderByPriority,
orderByValue,
equalTo,
query,
onValue,
onChildAdded,
onChildChanged,
onChildMoved,
onChildRemoved,
set,
setPriority,
setWithPriority,
get,
child,
onDisconnect,
keepSynced,
push,
remove,
update,
runTransaction,
} from '.';
console.log(database().app);
// checks module exists at root
console.log(firebase.database().app.name);
console.log(firebase.database().ref('foo/bar'));
// checks module exists at app level
console.log(firebase.app().database().app.name);
// app level module accepts string arg
console.log(firebase.app().database('some-string').app.name);
console.log(firebase.app().database('some-string').ref('foo'));
// checks statics exist
console.log(firebase.database.SDK_VERSION);
// checks statics exist on defaultExport
console.log(database.firebase.SDK_VERSION);
// checks root exists
console.log(firebase.SDK_VERSION);
// checks multi-app support exists
console.log(firebase.database(firebase.app()).app.name);
// checks default export supports app arg
console.log(database(firebase.app()).app.name);
// checks statics - ServerValue
console.log(firebase.database.ServerValue.TIMESTAMP);
console.log(firebase.database.ServerValue.increment(1));
// checks Module instance APIs
const dbInstance = firebase.database();
console.log(dbInstance.ref('foo/bar'));
console.log(dbInstance.refFromURL('https://example.firebaseio.com'));
dbInstance.goOnline().then(() => {
console.log('Online');
});
dbInstance.goOffline().then(() => {
console.log('Offline');
});
dbInstance.setPersistenceEnabled(true);
dbInstance.setLoggingEnabled(true);
dbInstance.setPersistenceCacheSizeBytes(2000000);
dbInstance.useEmulator('localhost', 9000);
const serverTime = dbInstance.getServerTime();
console.log(serverTime);
const rootRef = dbInstance.ref();
const rootChildRef = rootRef.child('users');
rootChildRef.push({ name: 'test' });
rootRef.set({ foo: 'bar' }).then(() => {
console.log('Set complete');
});
rootRef
.update({ foo: 'bar' }, () => {})
.then(() => {
console.log('Update complete');
});
rootRef.once('value').then((snapshot: FirebaseDatabaseTypes.DataSnapshot) => {
console.log(snapshot.exists());
console.log(snapshot.val());
console.log(snapshot.key);
console.log(snapshot.ref);
});
rootRef.on('value', (snapshot: FirebaseDatabaseTypes.DataSnapshot) => {
console.log(snapshot.val());
});
rootRef.off('value');
// checks modular API functions
const dbModular1 = getDatabase();
console.log(dbModular1.app.name);
const dbModular2 = getDatabase(firebase.app());
console.log(dbModular2.app.name);
const dbModular3 = getDatabase(firebase.app(), 'https://example.firebaseio.com');
console.log(dbModular3.app.name);
connectDatabaseEmulator(dbInstance, 'localhost', 9000);
goOffline(dbInstance).then(() => {
console.log('Offline');
});
goOnline(dbInstance).then(() => {
console.log('Online');
});
const modularRef = ref(dbInstance, 'users');
const modularRef2 = ref(dbInstance);
console.log(modularRef.key);
console.log(modularRef2.key);
const modularRefFromURL = refFromURL(dbInstance, 'https://example.firebaseio.com/users');
console.log(modularRefFromURL.key);
setPersistenceEnabled(dbInstance, true);
setLoggingEnabled(dbInstance, true);
setPersistenceCacheSizeBytes(dbInstance, 2000000);
const timestamp = serverTimestamp();
console.log(timestamp);
getServerTime(dbInstance).then((time: number) => {
console.log(time);
});
const incrementValue = increment(1);
console.log(incrementValue);
console.log(ServerValue.TIMESTAMP);
console.log(ServerValue.increment(1));
// checks modular query functions
const testRef = ref(dbInstance, 'users');
const testQuery = query(testRef, orderByChild('name'), limitToFirst(10));
console.log(testQuery);
// Test all query constraint functions
console.log(query(testRef, endAt('value')));
console.log(query(testRef, endBefore('value')));
console.log(query(testRef, startAt('value')));
console.log(query(testRef, startAfter('value')));
console.log(query(testRef, limitToFirst(5)));
console.log(query(testRef, limitToLast(5)));
console.log(query(testRef, orderByChild('age')));
console.log(query(testRef, orderByKey()));
console.log(query(testRef, orderByPriority()));
console.log(query(testRef, orderByValue()));
console.log(query(testRef, equalTo('value')));
const modularUnsubscribe1 = onValue(testRef, (snapshot: FirebaseDatabaseTypes.DataSnapshot) => {
console.log(snapshot.val());
});
const modularUnsubscribe2 = onValue(
testRef,
(snapshot: FirebaseDatabaseTypes.DataSnapshot) => {
console.log(snapshot.val());
},
(error: Error) => {
console.log(error.message);
},
);
const modularUnsubscribe3 = onValue(
testRef,
(snapshot: FirebaseDatabaseTypes.DataSnapshot) => {
console.log(snapshot.val());
},
{ onlyOnce: true },
);
modularUnsubscribe1();
modularUnsubscribe2();
modularUnsubscribe3();
const unsubscribeChildAdded = onChildAdded(
testRef,
(snapshot: FirebaseDatabaseTypes.DataSnapshot, previousChildName: string | null) => {
console.log(snapshot.val());
console.log(previousChildName);
},
);
const unsubscribeChildChanged = onChildChanged(
testRef,
(snapshot: FirebaseDatabaseTypes.DataSnapshot, previousChildName: string | null) => {
console.log(snapshot.val());
console.log(previousChildName);
},
);
const unsubscribeChildMoved = onChildMoved(
testRef,
(snapshot: FirebaseDatabaseTypes.DataSnapshot, previousChildName: string | null) => {
console.log(snapshot.val());
console.log(previousChildName);
},
);
const unsubscribeChildRemoved = onChildRemoved(
testRef,
(snapshot: FirebaseDatabaseTypes.DataSnapshot) => {
console.log(snapshot.val());
},
);
unsubscribeChildAdded();
unsubscribeChildChanged();
unsubscribeChildMoved();
unsubscribeChildRemoved();
set(testRef, { foo: 'bar' }).then(() => {
console.log('Set complete');
});
setPriority(testRef, 'high').then(() => {
console.log('Priority set');
});
setWithPriority(testRef, { foo: 'bar' }, 'high').then(() => {
console.log('Set with priority complete');
});
get(testQuery).then((snapshot: FirebaseDatabaseTypes.DataSnapshot) => {
console.log(snapshot.val());
});
const modularChildRef = child(testRef, 'child');
console.log(modularChildRef.key);
const onDisconnectRef = onDisconnect(testRef);
console.log(onDisconnectRef);
keepSynced(testRef, true).then(() => {
console.log('Keep synced set');
});
const modularPushRef = push(testRef, { name: 'test' });
console.log(modularPushRef.key);
remove(testRef).then(() => {
console.log('Remove complete');
});
update(testRef, { foo: 'bar' }).then(() => {
console.log('Update complete');
});
runTransaction(testRef, (currentData: any) => {
return { ...currentData, updated: true };
}).then((result: FirebaseDatabaseTypes.TransactionResult) => {
console.log(result.committed);
console.log(result.snapshot.val());
});
runTransaction(
testRef,
(currentData: any) => {
return { ...currentData, updated: true };
},
{ applyLocally: true },
).then((result: FirebaseDatabaseTypes.TransactionResult) => {
console.log(result.committed);
});