From ba0576ed89e01e490792e5a49cc053821c1cbf37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96rjan=20Fors?= Date: Fri, 1 Mar 2019 16:58:39 +0100 Subject: [PATCH] Add listDocuments to collection --- src/firestore-collection.js | 17 +++++++++++++++++ test/unit/firestore-collection.js | 27 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/firestore-collection.js b/src/firestore-collection.js index 3d76ce47..50e8c2e4 100644 --- a/src/firestore-collection.js +++ b/src/firestore-collection.js @@ -65,6 +65,23 @@ MockFirestoreCollection.prototype.doc = function (path) { return child; }; +MockFirestoreCollection.prototype.listDocuments = function () { + var err = this._nextErr('listDocuments'); + var self = this; + return new Promise(function (resolve, reject) { + self._defer('listDocuments', _.toArray(arguments), function () { + if (err === null) { + var docs = _.map(self.data, function (value, key) { + return self.doc(key); + }); + resolve(docs); + } else { + reject(err); + } + }); + }); +}; + MockFirestoreCollection.prototype._hasChild = function (key) { return _.isObject(this.data) && _.has(this.data, key); }; diff --git a/test/unit/firestore-collection.js b/test/unit/firestore-collection.js index 2e4c8d47..0f0311bd 100644 --- a/test/unit/firestore-collection.js +++ b/test/unit/firestore-collection.js @@ -356,4 +356,31 @@ describe('MockFirestoreCollection', function () { ]); }); }); + describe('#listDocuments', function () { + it('retrieves all data for existing collection', function(done) { + db.autoFlush(); + var keys = Object.keys(require('./data.json').collections); + collection.listDocuments().then(function(refs) { + expect(refs.length).to.equal(6); + refs.forEach(function(ref) { + expect(keys).to.contain(ref.id); + }); + done(); + }).catch(done); + }); + + it('retrieves data added to collection', function(done) { + db.autoFlush(); + db.collection('group').add({ + name: 'test' + }); + db.collection('group').listDocuments().then(function(refs) { + expect(refs.length).to.equal(1); + refs[0].get().then(function(doc) { + expect(doc.data().name).to.equal('test'); + done(); + }).catch(done); + }).catch(done); + }); + }); });