Skip to content

Commit 1052688

Browse files
committed
update push to return thenable reference
resolve soumak77#83
1 parent bfdd8a0 commit 1052688

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/firebase.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,10 @@ MockFirebase.prototype.push = function (data, callback) {
238238
if (arguments.length && data !== null) {
239239
// currently, callback only invoked if child exists
240240
validate.data(data);
241-
child.set(data, callback);
241+
return utils.createThenableReference(child, child.set(data, callback));
242+
} else {
243+
return utils.createThenableReference(child, Promise.resolve(null));
242244
}
243-
return child;
244245
};
245246

246247
MockFirebase.prototype.once = function (event, callback, cancel, context) {

src/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,10 @@ exports.findUndefinedProperties = function (obj) {
199199
recurse(obj, path);
200200
return results;
201201
};
202+
203+
exports.createThenableReference = function(reference, promise) {
204+
reference.then = function(success, failure) {
205+
return promise.then(success).catch(failure);
206+
};
207+
return reference;
208+
};

test/unit/firebase.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,19 @@ describe('MockFirebase', function () {
874874
expect(ref.push.bind(ref, {someProp: undefined})).to.throw();
875875
});
876876

877+
it('should return thenable reference', function (done) {
878+
var thenable = ref.push({
879+
foo: 'bar'
880+
});
881+
thenable.then(function() {
882+
expect(thenable.getData()).to.deep.equal({
883+
foo: 'bar'
884+
});
885+
done();
886+
}, done);
887+
ref.flush();
888+
});
889+
877890
it('can add data by auto id', function () {
878891
var id = ref._newAutoId();
879892
sinon.stub(ref, '_newAutoId').returns(id);

0 commit comments

Comments
 (0)