-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathGeoWriteBatch.test.ts
145 lines (127 loc) · 4.55 KB
/
GeoWriteBatch.test.ts
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
import * as chai from 'chai';
import { GeoWriteBatch } from '../src/GeoWriteBatch';
import {
afterEachHelper, beforeEachHelper, collection, dummyData, failTestOnCaughtError,
firestore, geocollection, geofirestore, invalidFirestores, testCollectionName
} from './common';
const expect = chai.expect;
describe('GeoWriteBatch Tests:', () => {
// Reset the Firestore before each test
beforeEach((done) => {
beforeEachHelper(done);
});
afterEach((done) => {
afterEachHelper(done);
});
describe('Constructor:', () => {
it('Constructor throws errors given invalid Firestore WriteBatch', () => {
invalidFirestores.forEach((invalidFirestore) => {
// @ts-ignore
expect(() => new GeoWriteBatch(invalidFirestore)).to.throw(null, 'WriteBatch must be an instance of a Firestore WriteBatch');
});
});
it('Constructor does not throw errors given valid Firestore WriteBatch', () => {
expect(() => new GeoWriteBatch(firestore.batch())).not.to.throw();
});
});
describe('set():', () => {
it('set() returns the current GeoWriteBatch given a GeoDocumentReference', () => {
const geowritebatch = geofirestore.batch();
const set = geowritebatch.set(geocollection.doc(), dummyData[0]);
expect(set).to.deep.equal(geowritebatch);
});
it('set() without a GeoDocumentReference or DocumentReference, throws an error', () => {
const geowritebatch = geofirestore.batch();
failTestOnCaughtError(() => geowritebatch.set(null, {}));
});
it('set() successfully adds a document to a collection', () => {
const doc = geocollection.doc();
return doc.firestore
.batch()
.set(doc, dummyData[0])
.commit()
.then(() => doc.get())
.then(snapshot => {
expect(snapshot.exists).to.equal(true);
expect(snapshot.data()).to.deep.equal(dummyData[0]);
});
});
it('set() can merge documents', () => {
const doc = geocollection.doc();
return doc.firestore
.batch()
.set(doc, dummyData[0], { merge: true })
.commit()
.then(() => {
return doc.firestore
.batch()
.set(doc, { count: 10 }, { merge: true })
.commit();
})
.then(() => doc.get())
.then(snapshot => {
expect(snapshot.exists).to.equal(true);
expect(snapshot.data()).to.deep.equal({ ...dummyData[0], count: 10 });
});
});
});
describe('update():', () => {
it('update() returns the current GeoWriteBatch given a DocumentReference', () => {
return collection.add(dummyData[0]).then((value) => {
const geowritebatch = geofirestore.batch();
const update = geowritebatch.update(value, dummyData[1]);
expect(update).to.deep.equal(geowritebatch);
});
});
it('update() without a GeoDocumentReference or DocumentReference, throws an error', () => {
const geowritebatch = geofirestore.batch();
failTestOnCaughtError(() => geowritebatch.update(null, {}));
});
it('update() successfully updates a document in a collection', () => {
const doc = geocollection.doc();
return doc
.set(dummyData[0])
.then(() =>
doc.firestore
.batch()
.update(doc, dummyData[1])
.commit()
)
.then(() => doc.get())
.then(snapshot => {
expect(snapshot.exists).to.equal(true);
expect(snapshot.data()).to.deep.equal(dummyData[1]);
});
});
});
describe('delete():', () => {
it('delete() returns the current GeoWriteBatch given a GeoDocumentReference', () => {
const geowritebatch = geofirestore.batch();
const deleteBatch = geowritebatch.delete(geocollection.doc());
expect(deleteBatch).to.deep.equal(geowritebatch);
});
it('delete() without a GeoDocumentReference or DocumentReference, throws an error', () => {
const geowritebatch = geofirestore.batch();
failTestOnCaughtError(() => geowritebatch.delete(null));
});
it('delete() successfully deletes a document from a collection', () => {
const doc = geocollection.doc();
return doc
.set(dummyData[0])
.then(() => doc.get())
.then(snapshot => {
expect(snapshot.exists).to.equal(true);
})
.then(() =>
doc.firestore
.batch()
.delete(doc)
.commit()
)
.then(() => doc.get())
.then(snapshot => {
expect(snapshot.exists).to.equal(false);
});
});
});
});