Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add listDocuments to collection #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this going to silently swallow exceptions that occur before any assertions are run and give us a false pass?

Copy link

@SidneyNemzer SidneyNemzer Nov 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Mocha's done accepts an error argument, so this should work as intended. https://mochajs.org/#asynchronous-code

});

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');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just go all the way and check for deep equality here.

done();
}).catch(done);
}).catch(done);
});
});
});