diff --git a/src/firebaseAuthService.js b/src/firebaseAuthService.js index ad400508..b08374c8 100644 --- a/src/firebaseAuthService.js +++ b/src/firebaseAuthService.js @@ -4,7 +4,7 @@ function FirebaseAuthService($firebaseAuth) { return $firebaseAuth(); } - FirebaseAuthService.$inject = ['$firebaseAuth', '$firebaseRef']; + FirebaseAuthService.$inject = ['$firebaseAuth']; angular.module('firebase') .factory('$firebaseAuthService', FirebaseAuthService); diff --git a/tests/protractor/todo/todo.html b/tests/protractor/todo/todo.html index 77ab5d3f..af3a9a27 100644 --- a/tests/protractor/todo/todo.html +++ b/tests/protractor/todo/todo.html @@ -41,7 +41,7 @@
-
+
diff --git a/tests/protractor/todo/todo.spec.js b/tests/protractor/todo/todo.spec.js index 77d4b77b..1ee476b6 100644 --- a/tests/protractor/todo/todo.spec.js +++ b/tests/protractor/todo/todo.spec.js @@ -103,6 +103,8 @@ describe('Todo App', function () { it('updates when a new Todo is added remotely', function (done) { // Simulate a todo being added remotely + + expect(todos.count()).toBe(4); flow.execute(function() { var def = protractor.promise.defer(); firebaseRef.push({ @@ -114,14 +116,19 @@ describe('Todo App', function () { }); return def.promise; }).then(function () { - expect(todos.count()).toBe(6); + browser.wait(function() { + return element(by.id('todo-4')).isPresent() + }, 10000); + }).then(function () { + expect(todos.count()).toBe(5); done(); }); - expect(todos.count()).toBe(5); }) it('updates when an existing Todo is removed remotely', function (done) { // Simulate a todo being removed remotely + + expect(todos.count()).toBe(5); flow.execute(function() { var def = protractor.promise.defer(); var onCallback = firebaseRef.limitToLast(1).on("child_added", function(childSnapshot) { @@ -135,10 +142,16 @@ describe('Todo App', function () { }); return def.promise; }).then(function () { - expect(todos.count()).toBe(3); + browser.wait(function() { + return todos.count(function (count) { + return count == 4; + }); + }, 10000); + }).then(function () { + expect(todos.count()).toBe(4); done(); }); - expect(todos.count()).toBe(4); + }); it('stops updating once the sync array is destroyed', function () { diff --git a/tests/unit/FirebaseArray.spec.js b/tests/unit/FirebaseArray.spec.js index 6a069b1e..acceca7a 100644 --- a/tests/unit/FirebaseArray.spec.js +++ b/tests/unit/FirebaseArray.spec.js @@ -40,13 +40,20 @@ describe('$firebaseArray', function () { $utils = $firebaseUtils; $rootScope = _$rootScope_; $q = _$q_; - tick = function (cb) { + + firebase.database.enableLogging(function () {tick()}); + tick = function () { setTimeout(function() { $q.defer(); $rootScope.$digest(); - cb && cb(); - }, 1000) + try { + $timeout.flush(); + } catch (err) { + // This throws an error when there is nothing to flush... + } + }) }; + arr = stubArray(STUB_DATA); }); }); @@ -86,8 +93,6 @@ describe('$firebaseArray', function () { }); it('should resolve to ref for new record', function(done) { - tick(); - arr.$add({foo: 'bar'}) .then(function (ref) { expect(ref.toString()).toBe(arr.$ref().child(ref.key).toString()) @@ -110,7 +115,6 @@ describe('$firebaseArray', function () { arr = stubArray(null, $firebaseArray.$extend({$$added:addPromise})); expect(arr.length).toBe(0); arr.$add({userId:'1234'}); - //1:flushAll()(arr.$ref()); expect(arr.length).toBe(0); expect(queue.length).toBe(1); queue[0]('James'); @@ -139,7 +143,7 @@ describe('$firebaseArray', function () { called = true; }); ref.set({'-Jwgx':{username:'James', email:'james@internet.com'}}); - //1:ref.flush(); + $timeout.flush(); queue[0]('James'); $timeout.flush(); @@ -157,8 +161,6 @@ describe('$firebaseArray', function () { expect(blackSpy).toHaveBeenCalled(); done(); }); - - tick(); }); it('should work with a primitive value', function(done) { @@ -168,8 +170,6 @@ describe('$firebaseArray', function () { done(); }); }); - - tick(); }); it('should throw error if array is destroyed', function() { @@ -207,8 +207,6 @@ describe('$firebaseArray', function () { done(); }); }); - - tick(); }); it('should work on a query', function() { @@ -216,7 +214,6 @@ describe('$firebaseArray', function () { var query = ref.limitToLast(2); var arr = $firebaseArray(query); addAndProcess(arr, testutils.snap('one', 'b', 1), null); - tick(); expect(arr.length).toBe(1); }); }); @@ -236,8 +233,6 @@ describe('$firebaseArray', function () { done(); }); }); - - tick(); }); it('should accept an item from the array', function(done) { @@ -249,8 +244,6 @@ describe('$firebaseArray', function () { done(); }); }); - - tick(); }); it('should return a promise', function() { @@ -264,8 +257,6 @@ describe('$firebaseArray', function () { done(); }); expect(spy).not.toHaveBeenCalled(); - - tick(); }); it('should reject promise on failure', function(done) { @@ -279,8 +270,6 @@ describe('$firebaseArray', function () { expect(blackSpy).toHaveBeenCalled(); done(); }); - - tick(); }); it('should reject promise on bad index', function(done) { @@ -293,9 +282,7 @@ describe('$firebaseArray', function () { expect(blackSpy.calls.argsFor(0)[0]).toMatch(/invalid/i); done(); - }) - - tick(); + }); }); it('should reject promise on bad object', function(done) { @@ -306,8 +293,6 @@ describe('$firebaseArray', function () { expect(blackSpy.calls.argsFor(0)[0]).toMatch(/invalid/i); done(); }); - - tick(); }); it('should accept a primitive', function() { @@ -319,8 +304,6 @@ describe('$firebaseArray', function () { expect(ss.val()).toBe('happy'); }) }); - - tick(); }); it('should throw error if object is destroyed', function() { @@ -339,8 +322,6 @@ describe('$firebaseArray', function () { expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({event: 'child_changed', key: key})); done() }); - - $rootScope.$digest(); }); it('should work on a query', function(done) { @@ -353,7 +334,8 @@ describe('$firebaseArray', function () { var query = ref.limitToLast(5); var arr = $firebaseArray(query); - tick(function () { + + arr.$loaded().then(function () { var key = arr.$keyAt(1); arr[1].foo = 'watchtest'; @@ -362,9 +344,7 @@ describe('$firebaseArray', function () { expect(blackSpy).not.toHaveBeenCalled(); done(); }); - - tick(); - }); + }) }); }); @@ -381,8 +361,6 @@ describe('$firebaseArray', function () { done(); }); }); - - tick(); }); it('should return a promise', function() { @@ -401,8 +379,6 @@ describe('$firebaseArray', function () { expect(blackSpy).not.toHaveBeenCalled(); done(); }); - - tick(); }); it('should reject promise on failure', function() { @@ -421,8 +397,6 @@ describe('$firebaseArray', function () { expect(blackSpy).toHaveBeenCalledWith(err); done(); }); - - tick(); }); it('should reject promise if bad int', function(done) { @@ -435,8 +409,6 @@ describe('$firebaseArray', function () { expect(blackSpy.calls.argsFor(0)[0]).toMatch(/invalid/i); done(); }); - - tick(); }); it('should reject promise if bad object', function() { @@ -449,7 +421,6 @@ describe('$firebaseArray', function () { expect(blackSpy).toHaveBeenCalled(); expect(blackSpy.calls.argsFor(0)[0]).toMatch(/invalid/i); }); - tick(); }); it('should work on a query', function(done) { @@ -465,9 +436,7 @@ describe('$firebaseArray', function () { arr.$loaded() .then(function () { - var p = arr.$remove(1); - tick(); - return p; + return arr.$remove(1); }) .then(whiteSpy, blackSpy) .then(function () { @@ -475,8 +444,6 @@ describe('$firebaseArray', function () { expect(blackSpy).not.toHaveBeenCalled(); done(); }); - - tick(); }); it('should throw Error if array destroyed', function() { @@ -595,8 +562,6 @@ describe('$firebaseArray', function () { expect(blackSpy).toHaveBeenCalledWith(err); done(); }); - - tick(); }); it('should resolve if function passed directly into $loaded', function(done) { @@ -622,8 +587,6 @@ describe('$firebaseArray', function () { expect(whiteSpy).not.toHaveBeenCalled(); done(); }); - - tick(); }); }); @@ -1072,17 +1035,6 @@ describe('$firebaseArray', function () { }); }); - var flushAll = (function() { - return function flushAll() { - // the order of these flush events is significant - Array.prototype.slice.call(arguments, 0).forEach(function(o) { - o.flush(); - }); - try { $timeout.flush(); } - catch(e) {} - } - })(); - function stubRef() { return firebase.database().ref().push(); } diff --git a/tests/unit/FirebaseAuth.spec.js b/tests/unit/FirebaseAuth.spec.js index 64f5dd23..1ace3203 100644 --- a/tests/unit/FirebaseAuth.spec.js +++ b/tests/unit/FirebaseAuth.spec.js @@ -63,12 +63,17 @@ describe('FirebaseAuth',function(){ authService = $firebaseAuth(auth); $timeout = _$timeout_; - tick = function (cb) { + firebase.database.enableLogging(function () {tick()}); + tick = function () { setTimeout(function() { $q.defer(); $rootScope.$digest(); - cb && cb(); - }, 1000) + try { + $timeout.flush(); + } catch (err) { + // This throws an error when there is nothing to flush... + } + }) }; }); @@ -328,8 +333,6 @@ describe('FirebaseAuth',function(){ }); describe('$onAuthStateChanged()',function(){ - //todo add more testing here after mockfirebase v2 auth is released - it('calls onAuthStateChanged() on the backing auth instance', function() { function cb() {} var ctx = {}; @@ -346,20 +349,18 @@ describe('FirebaseAuth',function(){ describe('$requireSignIn()',function(){ it('will be resolved if user is logged in', function(done){ + var credentials = {provider: 'facebook'}; spyOn(authService._, 'getAuth').and.callFake(function () { - return {provider: 'facebook'}; + return credentials; }); authService.$requireSignIn() .then(function (result) { - expect(result).toEqual({provider:'facebook'}); + expect(result).toEqual(credentials); done(); - }) - .catch(function () { - console.log(arguments); }); - fakePromiseResolve(null); + fakePromiseResolve(credentials); tick(); }); @@ -374,53 +375,40 @@ describe('FirebaseAuth',function(){ done(); }); - fakePromiseResolve(null); + fakePromiseResolve(); tick(); }); }); describe('$waitForSignIn()',function(){ it('will be resolved with authData if user is logged in', function(done){ + var credentials = {provider: 'facebook'}; spyOn(authService._, 'getAuth').and.callFake(function () { - return {provider: 'facebook'}; + return credentials; }); - wrapPromise(authService.$waitForSignIn()); - - fakePromiseResolve({provider: 'facebook'}); - tick(function () { - expect(result).toEqual({provider:'facebook'}); + authService.$waitForSignIn().then(function (result) { + expect(result).toEqual(credentials); done(); }); + + fakePromiseResolve(credentials); + tick(); }); it('will be resolved with null if user is not logged in', function(done){ spyOn(authService._, 'getAuth').and.callFake(function () { - return; + return null; }); - wrapPromise(authService.$waitForSignIn()); - - fakePromiseResolve(); - tick(function () { - expect(result).toEqual(undefined); + authService.$waitForSignIn().then(function (result) { + expect(result).toEqual(null); done(); }); - }); - // TODO: Replace this test - // it('promise resolves with current value if auth state changes after onAuth() completes', function() { - // ref.getAuth.and.returnValue({provider:'facebook'}); - // wrapPromise(auth.$waitForSignIn()); - // callback('onAuth')(); - // $timeout.flush(); - // expect(result).toEqual({provider:'facebook'}); - // - // ref.getAuth.and.returnValue(null); - // wrapPromise(auth.$waitForSignIn()); - // $timeout.flush(); - // expect(result).toBe(null); - // }); + fakePromiseResolve(); + tick(); + }); }); describe('$createUserWithEmailAndPassword()',function(){ diff --git a/tests/unit/FirebaseAuthService.spec.js b/tests/unit/FirebaseAuthService.spec.js index b4760179..306fca17 100644 --- a/tests/unit/FirebaseAuthService.spec.js +++ b/tests/unit/FirebaseAuthService.spec.js @@ -23,5 +23,4 @@ describe('$firebaseAuthService', function () { })); }); - }); diff --git a/tests/unit/FirebaseObject.spec.js b/tests/unit/FirebaseObject.spec.js index 7bbff87a..2c161b73 100644 --- a/tests/unit/FirebaseObject.spec.js +++ b/tests/unit/FirebaseObject.spec.js @@ -32,12 +32,17 @@ describe('$firebaseObject', function() { $q = _$q_; testutils = _testutils_; - tick = function (cb) { + firebase.database.enableLogging(function () {tick()}); + tick = function () { setTimeout(function() { $q.defer(); $rootScope.$digest(); - cb && cb(); - }, 1000) + try { + $timeout.flush(); + } catch (err) { + // This throws an error when there is nothing to flush... + } + }) }; obj = makeObject(FIXTURE_DATA); @@ -68,11 +73,13 @@ describe('$firebaseObject', function() { }); describe('$save', function () { - it('should call $firebase.$set', function () { - spyOn(obj.$ref(), 'set'); + it('should call $firebase.$set', function (done) { + var spy = spyOn(firebase.database.Reference.prototype, 'set').and.callThrough(); obj.foo = 'bar'; - obj.$save(); - expect(obj.$ref().set).toHaveBeenCalled(); + obj.$save().then(function () { + expect(spy).toHaveBeenCalled(); + done(); + }); }); it('should return a promise', function () { @@ -89,7 +96,6 @@ describe('$firebaseObject', function() { expect(blackSpy).not.toHaveBeenCalled(); done(); }); - tick(); }); it('should reject promise on failure', function (done) { @@ -103,8 +109,6 @@ describe('$firebaseObject', function() { expect(whiteSpy).not.toHaveBeenCalled(); done(); }); - - tick(); }); it('should trigger watch event', function(done) { @@ -116,8 +120,6 @@ describe('$firebaseObject', function() { expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({event: 'value', key: obj.$id})); done(); }); - - tick(); }); it('should work on a query', function(done) { @@ -132,8 +134,6 @@ describe('$firebaseObject', function() { expect(spy).toHaveBeenCalledWith({foo: 'bar'}, jasmine.any(Function)); done(); }); - - tick(); }); }); @@ -157,8 +157,6 @@ describe('$firebaseObject', function() { obj.key = "value"; obj.$save(); - - tick(); }); it('should reject if the ready promise is rejected', function (done) { @@ -179,8 +177,6 @@ describe('$firebaseObject', function() { expect(blackSpy).toHaveBeenCalledWith(err); done(); }); - - tick(); }); it('should resolve to the FirebaseObject instance', function (done) { @@ -188,9 +184,7 @@ describe('$firebaseObject', function() { obj.$loaded().then(spy).then(function () { expect(spy).toHaveBeenCalledWith(obj); done() - }) - - tick(); + }); }); it('should contain all data at the time $loaded is called', function (done) { @@ -200,8 +194,6 @@ describe('$firebaseObject', function() { done(); }); obj.$ref().set(FIXTURE_DATA); - - tick(); }); it('should trigger if attached before load completes', function(done) { @@ -211,8 +203,6 @@ describe('$firebaseObject', function() { expect(data).toEqual(jasmine.objectContaining(FIXTURE_DATA)); done(); }); - - tick(); }); it('should trigger if attached after load completes', function(done) { @@ -222,8 +212,6 @@ describe('$firebaseObject', function() { expect(data).toEqual(jasmine.objectContaining(FIXTURE_DATA)); done(); }); - - tick(); }); it('should resolve properly if function passed directly into $loaded', function(done) { @@ -232,9 +220,7 @@ describe('$firebaseObject', function() { obj.$loaded(function (data) { expect(data).toEqual(jasmine.objectContaining(FIXTURE_DATA)); done(); - }) - - tick(); + }); }); it('should reject properly if function passed directly into $loaded', function(done) { @@ -251,8 +237,6 @@ describe('$firebaseObject', function() { expect(whiteSpy).not.toHaveBeenCalled(); done(); }); - - tick(); }); }); @@ -275,8 +259,6 @@ describe('$firebaseObject', function() { expect(off).toBeA('function'); done(); }); - - tick(); }); it('should have data when it resolves', function (done) { @@ -286,8 +268,6 @@ describe('$firebaseObject', function() { expect(obj).toEqual(jasmine.objectContaining(FIXTURE_DATA)); done(); }); - - tick(); }); it('should have data in $scope when resolved', function(done) { @@ -300,28 +280,25 @@ describe('$firebaseObject', function() { expect($scope.test.$id).toBe(obj.$id); done(); }); - - tick(); }); it('should send local changes to $firebase.$set', function (done) { var obj = makeObject(FIXTURE_DATA); var spy = spyOn(firebase.database.Reference.prototype, 'set').and.callThrough(); var $scope = $rootScope.$new(); + var ready = false; obj.$bindTo($scope, 'test') .then(function () { $scope.test.bar = 'baz'; - }) - .then(function () { - tick(function () { - $timeout.flush(); - expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({bar: 'baz'}), jasmine.any(Function)); - done(); - }); + ready = true; }); - tick(); + obj.$ref().on('value', function (snapshot) { + if (!ready) return; + expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({bar: 'baz'}), jasmine.any(Function)); + done(); + }); }); it('should allow data to be set inside promise callback', function (done) { @@ -341,7 +318,6 @@ describe('$firebaseObject', function() { }); ref.set(oldData); - tick(); }); it('should apply server changes to scope variable', function () { @@ -350,7 +326,6 @@ describe('$firebaseObject', function() { $timeout.flush(); obj.$$updated(fakeSnap({foo: 'bar'})); obj.$$notify(); - //1:flushAll(); expect($scope.test).toEqual({foo: 'bar', $id: obj.$id, $priority: obj.$priority}); }); @@ -360,7 +335,6 @@ describe('$firebaseObject', function() { $timeout.flush(); obj.$$updated(fakeSnap({foo: 'bar'})); obj.$$notify(); - //1:flushAll(); var oldTest = $scope.test; obj.$$updated(fakeSnap({foo: 'baz'})); obj.$$notify(); @@ -373,7 +347,6 @@ describe('$firebaseObject', function() { $timeout.flush(); obj.$$updated(fakeSnap({foo: 'bar'})); obj.$$notify(); - //1:flushAll(); var oldTest = $scope.test; obj.$$updated(fakeSnap({foo: 'bar'})); obj.$$notify(); @@ -401,7 +374,6 @@ describe('$firebaseObject', function() { var $scope = $rootScope.$new(); $scope.test = {foo: true}; obj.$bindTo($scope, 'test'); - //1:flushAll(); expect($utils.scopeData(obj)).toEqual(origValue); }); @@ -419,33 +391,28 @@ describe('$firebaseObject', function() { it('should delete $value if set to an object', function (done) { var $scope = $rootScope.$new(); var obj = makeObject(null); + var ready = false; - $timeout.flush(); - // Note: Failing because we're not writing -> reading -> fixing $scope.test obj.$bindTo($scope, 'test') .then(function () { expect($scope.test).toEqual({$value: null, $id: obj.$id, $priority: obj.$priority}); }).then(function () { $scope.test.text = "hello"; - }).then(function () { - // This isn't ideal, but needed to fulfill promises, then trigger timeout created - // by that promise, then fulfil the promise created by that timeout. Yep. - tick(function () { - $timeout.flush(); - tick(function () { - expect($scope.test).toEqual({text: 'hello', $id: obj.$id, $priority: obj.$priority}); - done(); - }) - }); - }); + ready = true; + }) - tick(); + $scope.$watch('test.$value', function (val) { + if (val === null) return; + expect(val).toBe(undefined); + done(); + }); }); it('should update $priority if $priority changed in $scope', function (done) { var $scope = $rootScope.$new(); var ref = stubRef(); var obj = $firebaseObject(ref); + var ready = false; var spy = spyOn(firebase.database.Reference.prototype, 'set').and.callThrough(); obj.$value = 'foo'; @@ -454,16 +421,14 @@ describe('$firebaseObject', function() { }) .then(function () { $scope.test.$priority = 9999; - }) - .then(function () { - tick(function () { - $timeout.flush(); - expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({'.priority': 9999}), jasmine.any(Function)); - done(); - }); + ready = true; }); - tick(); + obj.$ref().on("value", function (snapshot) { + if (!ready) return + expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({'.priority': 9999}), jasmine.any(Function)); + done(); + }); }); it('should update $value if $value changed in $scope', function () { @@ -480,9 +445,7 @@ describe('$firebaseObject', function() { }) .then(function () { expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({'.value': 'bar'}), jasmine.any(Function)); - }) - - tick(); + }); }); it('should only call $$scopeUpdated once if both metaVars and properties change in the same $digest', function(done){ @@ -493,13 +456,13 @@ describe('$firebaseObject', function() { var old$scopeUpdated = obj.$$scopeUpdated; var callCount = 0; + var ready = false; obj.$bindTo($scope, 'test') .then(function () { expect($scope.test).toEqual({text:'hello', $id: obj.$id, $priority: 3}); }) .then(function () { - obj.$$scopeUpdated = function(){ callCount++; done(); @@ -508,16 +471,14 @@ describe('$firebaseObject', function() { $scope.test.text='goodbye'; $scope.test.$priority=4; - }) - .then(function () { - tick(function () { - $timeout.flush(); - expect(callCount).toBe(1); - done(); - }); + ready = true; }); - tick(); + obj.$ref().on("value", function (snapshot) { + if (!ready) return; + expect(callCount).toBe(1); + done(); + }); }); it('should throw error if double bound', function(done) { @@ -536,8 +497,6 @@ describe('$firebaseObject', function() { expect(bReject).toHaveBeenCalled(); done(); }); - - tick(); }); it('should accept another binding after off is called', function(done) { @@ -558,8 +517,6 @@ describe('$firebaseObject', function() { expect(bFail).not.toHaveBeenCalled(); done(); }); - - tick(); }); }); @@ -576,8 +533,6 @@ describe('$firebaseObject', function() { expect(spy).not.toHaveBeenCalled(); done(); }); - - tick(); }); it('additional calls to the deregistration function should be silently ignored',function(done){ @@ -592,8 +547,6 @@ describe('$firebaseObject', function() { expect(spy).not.toHaveBeenCalled(); done(); }); - - tick(); }); }); @@ -626,8 +579,6 @@ describe('$firebaseObject', function() { obj.$remove().then(function () { expect(obj.$value).toBe(null); }); - - tick(); }); it('should trigger a value event for $watch listeners', function(done) { @@ -638,8 +589,6 @@ describe('$firebaseObject', function() { expect(spy).toHaveBeenCalledWith({ event: 'value', key: obj.$id }); done(); }); - - tick(); }); it('should work on a query', function(done) { @@ -651,15 +600,11 @@ describe('$firebaseObject', function() { obj.$loaded().then(function () { expect(obj.foo).toBe('bar'); }).then(function () { - var p = obj.$remove(); - tick(); - return p; + return obj.$remove(); }).then(function () { expect(obj.$value).toBe(null); done(); }); - - tick(); }); }); @@ -681,8 +626,6 @@ describe('$firebaseObject', function() { expect($scope.$watch.$$$offSpy).toHaveBeenCalled(); done(); }); - - tick(); }); it('should unbind if scope is destroyed', function (done) { @@ -695,8 +638,6 @@ describe('$firebaseObject', function() { expect($scope.$watch.$$$offSpy).toHaveBeenCalled(); done(); }); - - tick() }); }); @@ -762,7 +703,7 @@ describe('$firebaseObject', function() { expect(obj).toHaveKey(k); }); obj.$$updated(fakeSnap(null)); - //1:flushAll(); + keys.forEach(function (k) { expect(obj).not.toHaveKey(k); }); @@ -818,19 +759,7 @@ describe('$firebaseObject', function() { expect(obj.$destroy).toHaveBeenCalledWith(error); }); }); - - function flushAll() { - Array.prototype.slice.call(arguments, 0).forEach(function (o) { - angular.isFunction(o.resolve) ? o.resolve() : o.flush(); - }); - try { obj.$ref().flush(); } - catch(e) {} - try { $interval.flush(500); } - catch(e) {} - try { $timeout.flush(); } - catch (e) {} - } - + var pushCounter = 1; function fakeSnap(data, pri) { diff --git a/tests/unit/utils.spec.js b/tests/unit/utils.spec.js index 9e26965e..88510678 100644 --- a/tests/unit/utils.spec.js +++ b/tests/unit/utils.spec.js @@ -39,12 +39,17 @@ describe('$firebaseUtils', function () { $q = _$q_; testutils = _testutils_; - tick = function (cb) { + firebase.database.enableLogging(function () {tick()}); + tick = function () { setTimeout(function() { $q.defer(); $rootScope.$digest(); - cb && cb(); - }, 1000) + try { + $timeout.flush(); + } catch (err) { + // This throws an error when there is nothing to flush... + } + }) }; }); }); @@ -334,8 +339,6 @@ describe('$firebaseUtils', function () { expect(whiteSpy).toHaveBeenCalled(); done(); }); - - tick(); }); it('saves the data', function(done) { @@ -356,8 +359,6 @@ describe('$firebaseUtils', function () { expect(blackSpy).toHaveBeenCalled(); done(); }); - - tick(); }); it('only affects query keys when using a query', function(done) { @@ -365,13 +366,12 @@ describe('$firebaseUtils', function () { var query = ref.limitToLast(1); var spy = spyOn(firebase.database.Reference.prototype, 'update').and.callThrough(); - $utils.doSet(query, {hello: 'world'}); - - tick(function () { - var args = spy.calls.mostRecent().args[0]; - expect(Object.keys(args)).toEqual(['hello', 'fish']); - done(); - }); + $utils.doSet(query, {hello: 'world'}) + .then(function () { + var args = spy.calls.mostRecent().args[0]; + expect(Object.keys(args)).toEqual(['hello', 'fish']); + done(); + }); }); }); @@ -395,13 +395,10 @@ describe('$firebaseUtils', function () { expect(whiteSpy).toHaveBeenCalled(); done(); }); - - tick(); }); it('removes the data', function(done) { return ref.set(MOCK_DATA).then(function() { - tick(); return $utils.doRemove(ref); }).then(function () { return ref.once('value'); @@ -427,13 +424,10 @@ describe('$firebaseUtils', function () { expect(blackSpy).toHaveBeenCalledWith(err); done(); }); - - tick(); }); it('only removes keys in query when query is used', function(done){ return ref.set(MOCK_DATA).then(function() { - tick(); var query = ref.limitToFirst(2); return $utils.doRemove(query); }).then(function() {