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

Commit 0174a8f

Browse files
committed
Add test coverage.
1 parent b512baa commit 0174a8f

File tree

3 files changed

+142
-3
lines changed

3 files changed

+142
-3
lines changed

tests/automatic_karma.conf.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ module.exports = function(config) {
1616
coverageReporter: {
1717
reporters: [
1818
{
19-
type: "lcovonly",
19+
// Nice HTML reports on developer machines, but not on Travis
20+
type: process.env.TRAVIS ? "lcovonly" : "lcov",
2021
dir: "coverage",
2122
subdir: "."
2223
},

tests/unit/FirebaseArray.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,15 @@ describe('$FirebaseArray', function () {
391391
arr.$$notify('child_removed', 'removedkey123', 'prevkey456');
392392
expect(spy).not.toHaveBeenCalled();
393393
});
394+
395+
it('calling the deregistration function twice should be silently ignored', function(){
396+
var spy = jasmine.createSpy('$watch');
397+
var off = arr.$watch(spy);
398+
off();
399+
off();
400+
arr.$$notify('child_removed', 'removedkey123', 'prevkey456');
401+
expect(spy).not.toHaveBeenCalled();
402+
});
394403
});
395404

396405
describe('$destroy', function() {
@@ -399,6 +408,14 @@ describe('$FirebaseArray', function () {
399408
expect(arr.$$$destroyFn).toHaveBeenCalled();
400409
});
401410

411+
it('should only call destroyFn the first time it is called', function() {
412+
arr.$destroy();
413+
expect(arr.$$$destroyFn).toHaveBeenCalled();
414+
arr.$$$destroyFn.calls.reset();
415+
arr.$destroy();
416+
expect(arr.$$$destroyFn).not.toHaveBeenCalled();
417+
});
418+
402419
it('should empty the array', function() {
403420
expect(arr.length).toBeGreaterThan(0);
404421
arr.$destroy();
@@ -602,6 +619,16 @@ describe('$FirebaseArray', function () {
602619
expect(spy).toHaveBeenCalled();
603620
});
604621

622+
it('"child_added" should not invoke $$notify if it already exists after prevChild', function() {
623+
var spy = jasmine.createSpy('$$notify');
624+
var arr = stubArray(STUB_DATA, $FirebaseArray.$extendFactory({ $$notify: spy }));
625+
var index = arr.$indexFor('e');
626+
var prevChild = arr.$$getKey(arr[index -1]);
627+
spy.calls.reset();
628+
arr.$$process('child_added', arr.$getRecord('e'), prevChild);
629+
expect(spy).not.toHaveBeenCalled();
630+
});
631+
605632
///////////////// UPDATE
606633

607634
it('should invoke $$notify with "child_changed" event', function() {
@@ -648,6 +675,16 @@ describe('$FirebaseArray', function () {
648675
expect(spy).toHaveBeenCalled();
649676
});
650677

678+
it('"child_moved" should not trigger $$notify if prevChild is already the previous element' , function() {
679+
var spy = jasmine.createSpy('$$notify');
680+
var arr = stubArray(STUB_DATA, $FirebaseArray.$extendFactory({ $$notify: spy }));
681+
var index = arr.$indexFor('e');
682+
var prevChild = arr.$$getKey(arr[index - 1]);
683+
spy.calls.reset();
684+
arr.$$process('child_moved', arr.$getRecord('e'), prevChild);
685+
expect(spy).not.toHaveBeenCalled();
686+
});
687+
651688
///////////////// REMOVE
652689
it('should remove from local array', function() {
653690
var len = arr.length;
@@ -666,6 +703,23 @@ describe('$FirebaseArray', function () {
666703
arr.$$process('child_removed', arr.$getRecord('e'));
667704
expect(spy).toHaveBeenCalled();
668705
});
706+
707+
it('"child_removed" should not trigger $$notify if the record is not in the array' , function() {
708+
var spy = jasmine.createSpy('$$notify');
709+
var arr = stubArray(STUB_DATA, $FirebaseArray.$extendFactory({ $$notify: spy }));
710+
spy.calls.reset();
711+
arr.$$process('child_removed', {$id:'f'});
712+
expect(spy).not.toHaveBeenCalled();
713+
});
714+
715+
//////////////// OTHER
716+
it('should throw an error for an unknown event type',function(){
717+
var arr = stubArray(STUB_DATA);
718+
expect(function(){
719+
arr.$$process('unknown_event', arr.$getRecord('e'));
720+
}).toThrow();
721+
});
722+
669723
});
670724

671725
describe('$extendFactory', function() {

tests/unit/FirebaseObject.spec.js

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
describe('$FirebaseObject', function() {
22
'use strict';
3-
var $firebase, $FirebaseObject, $utils, $rootScope, $timeout, obj, $fb, testutils, $interval;
3+
var $firebase, $FirebaseObject, $utils, $rootScope, $timeout, obj, $fb, testutils, $interval, log;
44

55
var DEFAULT_ID = 'recc';
66
var FIXTURE_DATA = {
@@ -11,9 +11,19 @@ describe('$FirebaseObject', function() {
1111
};
1212

1313
beforeEach(function () {
14+
log = {
15+
error:[]
16+
};
17+
1418
module('mock.firebase');
1519
module('firebase');
16-
module('testutils');
20+
module('testutils',function($provide){
21+
$provide.value('$log',{
22+
error:function(){
23+
log.error.push(Array.prototype.slice.call(arguments));
24+
}
25+
})
26+
});
1727
inject(function (_$firebase_, _$interval_, _$FirebaseObject_, _$timeout_, $firebaseUtils, _$rootScope_, _testutils_) {
1828
$firebase = _$firebase_;
1929
$FirebaseObject = _$FirebaseObject_;
@@ -253,6 +263,34 @@ describe('$FirebaseObject', function() {
253263
expect($scope.test).toEqual({foo: 'bar', $id: obj.$id, $priority: obj.$priority});
254264
});
255265

266+
it('will replace the object on scope if new server value is not deeply equal', function () {
267+
var $scope = $rootScope.$new();
268+
obj.$bindTo($scope, 'test');
269+
$timeout.flush();
270+
$fb.$set.calls.reset();
271+
obj.$$updated(fakeSnap({foo: 'bar'}));
272+
obj.$$notify();
273+
flushAll();
274+
var oldTest = $scope.test;
275+
obj.$$updated(fakeSnap({foo: 'baz'}));
276+
obj.$$notify();
277+
expect($scope.test === oldTest).toBe(false);
278+
});
279+
280+
it('will leave the scope value alone if new server value is deeply equal', function () {
281+
var $scope = $rootScope.$new();
282+
obj.$bindTo($scope, 'test');
283+
$timeout.flush();
284+
$fb.$set.calls.reset();
285+
obj.$$updated(fakeSnap({foo: 'bar'}));
286+
obj.$$notify();
287+
flushAll();
288+
var oldTest = $scope.test;
289+
obj.$$updated(fakeSnap({foo: 'bar'}));
290+
obj.$$notify();
291+
expect($scope.test === oldTest).toBe(true);
292+
});
293+
256294
it('should stop binding when off function is called', function () {
257295
var origData = $utils.scopeData(obj);
258296
var $scope = $rootScope.$new();
@@ -347,6 +385,31 @@ describe('$FirebaseObject', function() {
347385
});
348386
});
349387

388+
describe('$watch', function(){
389+
it('should return a deregistration function',function(){
390+
var spy = jasmine.createSpy('$watch');
391+
var off = obj.$watch(spy);
392+
obj.foo = 'watchtest';
393+
obj.$save();
394+
flushAll();
395+
expect(spy).toHaveBeenCalled();
396+
spy.calls.reset();
397+
off();
398+
expect(spy).not.toHaveBeenCalled();
399+
});
400+
401+
it('additional calls to the deregistration function should be silently ignored',function(){
402+
var spy = jasmine.createSpy('$watch');
403+
var off = obj.$watch(spy);
404+
off();
405+
off();
406+
obj.foo = 'watchtest';
407+
obj.$save();
408+
flushAll();
409+
expect(spy).not.toHaveBeenCalled();
410+
});
411+
});
412+
350413
describe('$remove', function() {
351414
it('should return a promise', function() {
352415
expect(obj.$remove()).toBeAPromise();
@@ -391,6 +454,13 @@ describe('$FirebaseObject', function() {
391454
expect(obj.$$$destroyFn).toHaveBeenCalled();
392455
});
393456

457+
it('should NOT invoke destroyFn if it is invoked a second time', function () {
458+
obj.$destroy();
459+
obj.$$$destroyFn.calls.reset();
460+
obj.$destroy();
461+
expect(obj.$$$destroyFn).not.toHaveBeenCalled();
462+
});
463+
394464
it('should dispose of any bound instance', function () {
395465
var $scope = $rootScope.$new();
396466
spyOnWatch($scope);
@@ -508,6 +578,20 @@ describe('$FirebaseObject', function() {
508578
});
509579
});
510580

581+
describe('$$error',function(){
582+
it('will log an error',function(){
583+
obj.$$error(new Error());
584+
expect(log.error).toHaveLength(1);
585+
});
586+
587+
it('will call $destroy',function(){
588+
obj.$destroy = jasmine.createSpy('$destroy');
589+
var error = new Error();
590+
obj.$$error(error);
591+
expect(obj.$destroy).toHaveBeenCalledWith(error);
592+
});
593+
});
594+
511595
function flushAll() {
512596
Array.prototype.slice.call(arguments, 0).forEach(function (o) {
513597
angular.isFunction(o.resolve) ? o.resolve() : o.flush();

0 commit comments

Comments
 (0)