Skip to content

Commit 10be2b8

Browse files
authored
Merge pull request soumak77#101 from Storr-co/firestore-batch-chaining
Enable chaining of firestore batch methods
2 parents 44c4fec + ed74856 commit 10be2b8

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,15 +80,18 @@ MockFirestore.prototype.batch = function () {
8080
else {
8181
doc.set(data);
8282
}
83+
return batch;
8384
},
8485
create: function(doc, data) {
8586
doc.create(data);
8687
},
8788
update: function(doc, data) {
8889
doc.update(data);
90+
return batch;
8991
},
9092
delete: function(doc) {
9193
doc.delete();
94+
return batch;
9295
},
9396
commit: function() {
9497
if (self.queue.events.length > 0) {
@@ -97,6 +100,7 @@ MockFirestore.prototype.batch = function () {
97100
return Promise.resolve();
98101
}
99102
};
103+
return batch;
100104
};
101105

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

test/unit/firestore.js

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

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

0 commit comments

Comments
 (0)