Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 2df3edc

Browse files
committed
Fixes #295 - add test units for $remove returning ref
1 parent 176bf7d commit 2df3edc

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

dist/angularfire.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* angularfire 0.8.0 2014-07-31
2+
* angularfire 0.8.0 2014-08-08
33
* https://github.com/firebase/angularfire
44
* Copyright (c) 2014 Firebase, Inc.
55
* MIT LICENSE: http://firebase.mit-license.org/
@@ -894,14 +894,15 @@
894894
},
895895

896896
$remove: function (key) {
897-
var ref = this._ref, self = this;
897+
var ref = this._ref, self = this, promise;
898+
var def = $firebaseUtils.defer();
898899
if (arguments.length > 0) {
899900
ref = ref.ref().child(key);
900901
}
901-
var def = $firebaseUtils.defer();
902902
if( angular.isFunction(ref.remove) ) {
903903
// self is not a query, just do a flat remove
904904
ref.remove(self._handle(def, ref));
905+
promise = def.promise;
905906
}
906907
else {
907908
var promises = [];
@@ -914,9 +915,12 @@
914915
ss.ref().remove(self._handle(d, ss.ref()));
915916
}, self);
916917
});
917-
self._handle($firebaseUtils.allPromises(promises), ref);
918+
promise = $firebaseUtils.allPromises(promises)
919+
.then(function() {
920+
return ref;
921+
});
918922
}
919-
return def.promise;
923+
return promise;
920924
},
921925

922926
$update: function (key, data) {

dist/angularfire.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/firebase.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@
7272
},
7373

7474
$remove: function (key) {
75-
var ref = this._ref, self = this;
75+
var ref = this._ref, self = this, promise;
76+
var def = $firebaseUtils.defer();
7677
if (arguments.length > 0) {
7778
ref = ref.ref().child(key);
7879
}
79-
var def = $firebaseUtils.defer();
8080
if( angular.isFunction(ref.remove) ) {
8181
// self is not a query, just do a flat remove
8282
ref.remove(self._handle(def, ref));
83+
promise = def.promise;
8384
}
8485
else {
8586
var promises = [];
@@ -92,9 +93,12 @@
9293
ss.ref().remove(self._handle(d, ss.ref()));
9394
}, self);
9495
});
95-
self._handle($firebaseUtils.allPromises(promises), ref);
96+
promise = $firebaseUtils.allPromises(promises)
97+
.then(function() {
98+
return ref;
99+
});
96100
}
97-
return def.promise;
101+
return promise;
98102
},
99103

100104
$update: function (key, data) {

tests/unit/FirebaseArray.spec.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,16 @@ describe('$FirebaseArray', function () {
216216
expect(arr.$remove(1)).toBeAPromise();
217217
});
218218

219-
it('should resolve promise on success', function() {
219+
it('should resolve promise to ref on success', function() {
220220
var whiteSpy = jasmine.createSpy('resolve');
221221
var blackSpy = jasmine.createSpy('reject');
222+
var expName = arr.$keyAt(1);
222223
arr.$remove(1).then(whiteSpy, blackSpy);
223224
flushAll();
225+
var resRef = whiteSpy.calls.argsFor(0)[0];
224226
expect(whiteSpy).toHaveBeenCalled();
227+
expect(resRef).toBeAFirebaseRef();
228+
expect(resRef.name()).toBe(expName);
225229
expect(blackSpy).not.toHaveBeenCalled();
226230
});
227231

@@ -692,15 +696,26 @@ describe('$FirebaseArray', function () {
692696
}
693697
}
694698

699+
function lastPartOfKey(key) {
700+
var parts = (key||'').split('/');
701+
return parts[parts.length-1];
702+
}
703+
695704
var pushCounter = 1;
696705
function stubRef(key) {
697706
var stub = {};
698707
stub.$lastPushRef = null;
699708
stub.ref = jasmine.createSpy('ref').and.returnValue(stub);
700709
stub.child = jasmine.createSpy('child').and.callFake(function(childKey) { return stubRef(key+'/'+childKey); });
701-
stub.name = jasmine.createSpy('name').and.returnValue(key);
710+
stub.name = jasmine.createSpy('name').and.returnValue(lastPartOfKey(key));
702711
stub.on = jasmine.createSpy('on');
703712
stub.off = jasmine.createSpy('off');
713+
stub.once = jasmine.createSpy('once');
714+
stub.set = jasmine.createSpy('set');
715+
stub.transaction = jasmine.createSpy('transaction');
716+
stub.toString = jasmine.createSpy('toString').and.callFake(function() {
717+
return 'stub://'+key;
718+
});
704719
stub.push = jasmine.createSpy('push').and.callFake(function() {
705720
stub.$lastPushRef = stubRef('newpushid-'+(pushCounter++));
706721
return stub.$lastPushRef;

tests/unit/firebase.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ describe('$firebase', function () {
189189
expect(whiteSpy).toHaveBeenCalledWith($fb.$ref());
190190
});
191191

192+
it('should resolve to ref if query', function() {
193+
var spy = jasmine.createSpy('resolve');
194+
var ref = new Firebase('Mock://').child('ordered').limit(2);
195+
var $fb = $firebase(ref);
196+
$fb.$remove().then(spy);
197+
flushAll();
198+
expect(spy).toHaveBeenCalledWith(ref);
199+
});
200+
192201
it('should resolve to child ref if key', function() {
193202
var whiteSpy = jasmine.createSpy('resolve');
194203
var blackSpy = jasmine.createSpy('reject');

0 commit comments

Comments
 (0)