Skip to content

Commit

Permalink
Merge pull request dmurvihill#31 from dmurvihill/list-documents
Browse files Browse the repository at this point in the history
List documents
  • Loading branch information
dmurvihill authored Jan 13, 2020
2 parents eea4eca + 4dd67a7 commit ee51273
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/firestore-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
27 changes: 27 additions & 0 deletions test/unit/firestore-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,33 @@ 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);
});
});

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

0 comments on commit ee51273

Please sign in to comment.