Skip to content

Commit

Permalink
Merge pull request #740 from firebase/test-improvements
Browse files Browse the repository at this point in the history
Faster, more consistent tests
  • Loading branch information
katowulf committed Jun 9, 2016
2 parents 14410f0 + 95b12ac commit e36e652
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 249 deletions.
2 changes: 1 addition & 1 deletion src/firebaseAuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
function FirebaseAuthService($firebaseAuth) {
return $firebaseAuth();
}
FirebaseAuthService.$inject = ['$firebaseAuth', '$firebaseRef'];
FirebaseAuthService.$inject = ['$firebaseAuth'];

angular.module('firebase')
.factory('$firebaseAuthService', FirebaseAuthService);
Expand Down
2 changes: 1 addition & 1 deletion tests/protractor/todo/todo.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

<!-- Todo list -->
<div id="todos">
<div class="todo" ng-cloak ng-repeat="(id, todo) in todos">
<div class="todo" ng-cloak ng-repeat="(id, todo) in todos" id="todo-{{$index}}">
<input class="toggle" type="checkbox" ng-model="todo.completed" ng-change="todos.$save(todo)" />
<input class="edit" ng-model="todo.title" ng-change="todos.$save(todo)" />
<button class="removeTodoButton" ng-click="removeTodo(id)">Remove</button>
Expand Down
21 changes: 17 additions & 4 deletions tests/protractor/todo/todo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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) {
Expand All @@ -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 () {
Expand Down
80 changes: 16 additions & 64 deletions tests/unit/FirebaseArray.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down Expand Up @@ -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())
Expand All @@ -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');
Expand Down Expand Up @@ -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();
Expand All @@ -157,8 +161,6 @@ describe('$firebaseArray', function () {
expect(blackSpy).toHaveBeenCalled();
done();
});

tick();
});

it('should work with a primitive value', function(done) {
Expand All @@ -168,8 +170,6 @@ describe('$firebaseArray', function () {
done();
});
});

tick();
});

it('should throw error if array is destroyed', function() {
Expand Down Expand Up @@ -207,16 +207,13 @@ describe('$firebaseArray', function () {
done();
});
});

tick();
});

it('should work on a query', function() {
var ref = stubRef();
var query = ref.limitToLast(2);
var arr = $firebaseArray(query);
addAndProcess(arr, testutils.snap('one', 'b', 1), null);
tick();
expect(arr.length).toBe(1);
});
});
Expand All @@ -236,8 +233,6 @@ describe('$firebaseArray', function () {
done();
});
});

tick();
});

it('should accept an item from the array', function(done) {
Expand All @@ -249,8 +244,6 @@ describe('$firebaseArray', function () {
done();
});
});

tick();
});

it('should return a promise', function() {
Expand All @@ -264,8 +257,6 @@ describe('$firebaseArray', function () {
done();
});
expect(spy).not.toHaveBeenCalled();

tick();
});

it('should reject promise on failure', function(done) {
Expand All @@ -279,8 +270,6 @@ describe('$firebaseArray', function () {
expect(blackSpy).toHaveBeenCalled();
done();
});

tick();
});

it('should reject promise on bad index', function(done) {
Expand All @@ -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) {
Expand All @@ -306,8 +293,6 @@ describe('$firebaseArray', function () {
expect(blackSpy.calls.argsFor(0)[0]).toMatch(/invalid/i);
done();
});

tick();
});

it('should accept a primitive', function() {
Expand All @@ -319,8 +304,6 @@ describe('$firebaseArray', function () {
expect(ss.val()).toBe('happy');
})
});

tick();
});

it('should throw error if object is destroyed', function() {
Expand All @@ -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) {
Expand All @@ -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';

Expand All @@ -362,9 +344,7 @@ describe('$firebaseArray', function () {
expect(blackSpy).not.toHaveBeenCalled();
done();
});

tick();
});
})
});
});

Expand All @@ -381,8 +361,6 @@ describe('$firebaseArray', function () {
done();
});
});

tick();
});

it('should return a promise', function() {
Expand All @@ -401,8 +379,6 @@ describe('$firebaseArray', function () {
expect(blackSpy).not.toHaveBeenCalled();
done();
});

tick();
});

it('should reject promise on failure', function() {
Expand All @@ -421,8 +397,6 @@ describe('$firebaseArray', function () {
expect(blackSpy).toHaveBeenCalledWith(err);
done();
});

tick();
});

it('should reject promise if bad int', function(done) {
Expand All @@ -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() {
Expand All @@ -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) {
Expand All @@ -465,18 +436,14 @@ describe('$firebaseArray', function () {

arr.$loaded()
.then(function () {
var p = arr.$remove(1);
tick();
return p;
return arr.$remove(1);
})
.then(whiteSpy, blackSpy)
.then(function () {
expect(whiteSpy).toHaveBeenCalled();
expect(blackSpy).not.toHaveBeenCalled();
done();
});

tick();
});

it('should throw Error if array destroyed', function() {
Expand Down Expand Up @@ -595,8 +562,6 @@ describe('$firebaseArray', function () {
expect(blackSpy).toHaveBeenCalledWith(err);
done();
});

tick();
});

it('should resolve if function passed directly into $loaded', function(done) {
Expand All @@ -622,8 +587,6 @@ describe('$firebaseArray', function () {
expect(whiteSpy).not.toHaveBeenCalled();
done();
});

tick();
});
});

Expand Down Expand Up @@ -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();
}
Expand Down
Loading

0 comments on commit e36e652

Please sign in to comment.