11describe ( '$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