Skip to content

Commit ed74856

Browse files
committed
enable chaining of firestore batch methods: set, update, and delete
See return type of each method in https://firebase.google.com/docs/reference/js/firebase.firestore.WriteBatch#delete
1 parent 241af33 commit ed74856

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/firestore.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ MockFirestore.prototype.runTransaction = function(transFunc) {
7171

7272
MockFirestore.prototype.batch = function () {
7373
var self = this;
74-
return {
74+
var batch = {
7575
set: function(doc, data, opts) {
7676
var _opts = _.assign({}, { merge: false }, opts);
7777
if (_opts.merge) {
@@ -80,12 +80,15 @@ MockFirestore.prototype.batch = function () {
8080
else {
8181
doc.set(data);
8282
}
83+
return batch;
8384
},
8485
update: function(doc, data) {
8586
doc.update(data);
87+
return batch;
8688
},
8789
delete: function(doc) {
8890
doc.delete();
91+
return batch;
8992
},
9093
commit: function() {
9194
if (self.queue.events.length > 0) {
@@ -94,6 +97,7 @@ MockFirestore.prototype.batch = function () {
9497
return Promise.resolve();
9598
}
9699
};
100+
return batch;
97101
};
98102

99103
MockFirestore.prototype.collection = function (path) {

test/unit/firestore.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,35 @@ describe('MockFirestore', function () {
185185

186186
db.flush();
187187
});
188+
189+
it('supports method chaining', function () {
190+
var doc1 = db.doc('doc1');
191+
var doc2 = db.doc('doc2');
192+
var doc3 = db.doc('doc3');
193+
var doc4 = db.doc('doc4');
194+
195+
doc3.set({value: -1});
196+
doc4.set({value: 4});
197+
198+
db.batch()
199+
.set(doc1, {value: 1})
200+
.set(doc2, {value: 2})
201+
.update(doc3, {value: 3})
202+
.delete(doc4)
203+
.commit();
204+
205+
var awaitChecks = Promise
206+
.all([doc1.get(), doc2.get(), doc3.get(), doc4.get()])
207+
.then(function(snaps) {
208+
expect(snaps[0].data()).to.deep.equal({value: 1});
209+
expect(snaps[1].data()).to.deep.equal({value: 2});
210+
expect(snaps[2].data()).to.deep.equal({value: 3});
211+
expect(snaps[3].exists).to.equal(false);
212+
});
213+
214+
db.flush();
215+
216+
return awaitChecks;
217+
});
188218
});
189219
});

0 commit comments

Comments
 (0)