-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathutils.spec.ts
More file actions
57 lines (48 loc) · 1.74 KB
/
Copy pathutils.spec.ts
File metadata and controls
57 lines (48 loc) · 1.74 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
import firebase from 'firebase/app';
import { AngularFirestoreCollection } from './collection/collection';
export interface Stock {
name: string;
price: number;
}
export const FAKE_STOCK_DATA = { name: 'FAKE', price: 1 };
export const randomName = (firestore): string => firestore.collection('a').doc().id;
export const createRandomStocks = async (
firestore: firebase.firestore.Firestore,
collectionRef: firebase.firestore.CollectionReference,
numberOfItems
) => {
// Create a batch to update everything at once
const batch = firestore.batch();
// Store the random names to delete them later
const count = 0;
let names: string[] = [];
Array.from(Array(numberOfItems)).forEach((a, i) => {
const name = randomName(firestore);
batch.set(collectionRef.doc(name), FAKE_STOCK_DATA);
names = [...names, name];
});
// Create the batch entries
// Commit!
await batch.commit();
return names;
};
export function deleteThemAll(names, ref) {
const promises = names.map(name => ref.doc(name).delete());
return Promise.all(promises);
}
export function delayUpdate<T>(collection: AngularFirestoreCollection<T>|firebase.firestore.CollectionReference, path, data, delay = 250) {
setTimeout(() => {
collection.doc(path).update(data);
}, delay);
}
export function delayAdd<T>(collection: AngularFirestoreCollection<T>|firebase.firestore.CollectionReference, path, data, delay = 250) {
setTimeout(() => {
collection.doc(path).set(data);
}, delay);
}
export function delayDelete<T>(collection: AngularFirestoreCollection<T>|firebase.firestore.CollectionReference, path, delay = 250) {
setTimeout(() => {
collection.doc(path).delete();
}, delay);
}
export const rando = () => (Math.random() + 1).toString(36).substring(7);