Skip to content

Commit ee51273

Browse files
authored
Merge pull request dmurvihill#31 from dmurvihill/list-documents
List documents
2 parents eea4eca + 4dd67a7 commit ee51273

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
@@ -528,6 +528,33 @@ describe('MockFirestoreCollection', function () {
528528
]);
529529
});
530530
});
531+
describe('#listDocuments', function () {
532+
it('retrieves all data for existing collection', function(done) {
533+
db.autoFlush();
534+
var keys = Object.keys(require('./data.json').collections);
535+
collection.listDocuments().then(function(refs) {
536+
expect(refs.length).to.equal(6);
537+
refs.forEach(function(ref) {
538+
expect(keys).to.contain(ref.id);
539+
});
540+
done();
541+
}).catch(done);
542+
});
543+
544+
it('retrieves data added to collection', function(done) {
545+
db.autoFlush();
546+
db.collection('group').add({
547+
name: 'test'
548+
});
549+
db.collection('group').listDocuments().then(function(refs) {
550+
expect(refs.length).to.equal(1);
551+
refs[0].get().then(function(doc) {
552+
expect(doc.data().name).to.equal('test');
553+
done();
554+
}).catch(done);
555+
}).catch(done);
556+
});
557+
});
531558

532559
describe('#onSnapshot', function () {
533560
it('returns value after collection is updated', function (done) {

0 commit comments

Comments
 (0)