Skip to content

Commit ba0576e

Browse files
committed
Add listDocuments to collection
1 parent 8fe31b9 commit ba0576e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/firestore-collection.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ MockFirestoreCollection.prototype.doc = function (path) {
6565
return child;
6666
};
6767

68+
MockFirestoreCollection.prototype.listDocuments = function () {
69+
var err = this._nextErr('listDocuments');
70+
var self = this;
71+
return new Promise(function (resolve, reject) {
72+
self._defer('listDocuments', _.toArray(arguments), function () {
73+
if (err === null) {
74+
var docs = _.map(self.data, function (value, key) {
75+
return self.doc(key);
76+
});
77+
resolve(docs);
78+
} else {
79+
reject(err);
80+
}
81+
});
82+
});
83+
};
84+
6885
MockFirestoreCollection.prototype._hasChild = function (key) {
6986
return _.isObject(this.data) && _.has(this.data, key);
7087
};

test/unit/firestore-collection.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,31 @@ describe('MockFirestoreCollection', function () {
356356
]);
357357
});
358358
});
359+
describe('#listDocuments', function () {
360+
it('retrieves all data for existing collection', function(done) {
361+
db.autoFlush();
362+
var keys = Object.keys(require('./data.json').collections);
363+
collection.listDocuments().then(function(refs) {
364+
expect(refs.length).to.equal(6);
365+
refs.forEach(function(ref) {
366+
expect(keys).to.contain(ref.id);
367+
});
368+
done();
369+
}).catch(done);
370+
});
371+
372+
it('retrieves data added to collection', function(done) {
373+
db.autoFlush();
374+
db.collection('group').add({
375+
name: 'test'
376+
});
377+
db.collection('group').listDocuments().then(function(refs) {
378+
expect(refs.length).to.equal(1);
379+
refs[0].get().then(function(doc) {
380+
expect(doc.data().name).to.equal('test');
381+
done();
382+
}).catch(done);
383+
}).catch(done);
384+
});
385+
});
359386
});

0 commit comments

Comments
 (0)