From d0b47d879ff87fc14c059ba62edc6e939702ac87 Mon Sep 17 00:00:00 2001 From: Andy Joslin Date: Tue, 8 Apr 2014 14:59:04 -0600 Subject: [PATCH] test($ionicLoading): re-add missing unit tests --- js/ext/angular/src/service/ionicLoading.js | 8 +- .../angular/test/service/ionicLoading.unit.js | 107 ++++++++++++++++++ 2 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 js/ext/angular/test/service/ionicLoading.unit.js diff --git a/js/ext/angular/src/service/ionicLoading.js b/js/ext/angular/src/service/ionicLoading.js index 1d72f83e4b..a393de6f51 100644 --- a/js/ext/angular/src/service/ionicLoading.js +++ b/js/ext/angular/src/service/ionicLoading.js @@ -89,8 +89,10 @@ function($animate, $document, $ionicTemplateLoader, $ionicBackdrop, $timeout, $q if (options.duration) { $timeout.cancel(this.durationTimeout); - this.durationTimeout = $timeout(angular.bind(this, this.hide), - +options.duration); + this.durationTimeout = $timeout( + angular.bind(this, this.hide), + +options.duration + ); } if (options.content) { this.scope.html = options.content; @@ -100,7 +102,7 @@ function($animate, $document, $ionicTemplateLoader, $ionicBackdrop, $timeout, $q var scope = this.scope; ionic.requestAnimationFrame(function() { $animate.removeClass(el, 'ng-hide'); - //Fix for ios: if we center the element twice, it always gets + //Fix for ios: if we center the element twice, it always gets //position right. Otherwise, it doesn't ionic.DomUtil.centerElementByMargin(el[0]); //One frame after it's visible, position it diff --git a/js/ext/angular/test/service/ionicLoading.unit.js b/js/ext/angular/test/service/ionicLoading.unit.js new file mode 100644 index 0000000000..f3de66b807 --- /dev/null +++ b/js/ext/angular/test/service/ionicLoading.unit.js @@ -0,0 +1,107 @@ +describe('$ionicLoading service', function() { + beforeEach(module('ionic')); + it('should reuse loader instance for getLoader', inject(function($ionicLoading) { + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + var loader2 = TestUtil.unwrapPromise($ionicLoading._getLoader()); + expect(loader).toBe(loader2); + })); + + describe('loader instance', function() { + + describe('.show()', function() { + + it('should retain backdrop if !noBackdrop and !isShown', inject(function($ionicLoading, $ionicBackdrop) { + spyOn($ionicBackdrop, 'retain'); + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + loader.show({}) + expect($ionicBackdrop.retain).toHaveBeenCalled(); + })); + it('should not retain backdrop if noBackdrop', inject(function($ionicLoading, $ionicBackdrop) { + spyOn($ionicBackdrop, 'retain'); + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + loader.show({ noBackdrop: true }) + expect($ionicBackdrop.retain).not.toHaveBeenCalled(); + })); + it('should not retain backdrop if isShown', inject(function($ionicLoading, $ionicBackdrop) { + spyOn($ionicBackdrop, 'retain'); + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + loader.isShown = true; + loader.show({}) + expect($ionicBackdrop.retain).not.toHaveBeenCalled(); + })); + + it('should not timeout if no duration', inject(function($ionicLoading, $timeout) { + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + loader.show({}); + expect(loader.durationTimeout).toBeFalsy(); + })); + it('should timeout if duration', inject(function($ionicLoading, $timeout) { + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + loader.show({ duration: 1000 }); + expect(loader.durationTimeout).toBeTruthy(); + expect(loader.durationTimeout.$$timeoutId).toBeTruthy(); + })); + it('should remove ng-hide after raf', inject(function($ionicLoading) { + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + ionic.requestAnimationFrame = function(cb) { cb(); }; + expect(loader.element.hasClass('ng-hide')).toBe(true); + loader.show({}); + expect(loader.element.hasClass('ng-hide')).toBe(false); + })); + it('should isShown = true', inject(function($ionicLoading) { + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + expect(loader.isShown).toBeFalsy(); + loader.show({}); + expect(loader.isShown).toBe(true); + })); + + }); + + describe('.hide()', function() { + + it('should release backdrop if hasBackdrop and isShown', inject(function($ionicLoading, $ionicBackdrop) { + spyOn($ionicBackdrop, 'release'); + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + loader.isShown = true; + loader.hasBackdrop = true; + loader.hide(); + expect($ionicBackdrop.release).toHaveBeenCalled(); + })); + it('should not release backdrop if !hasBackdrop', inject(function($ionicLoading, $ionicBackdrop) { + spyOn($ionicBackdrop, 'release'); + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + loader.isShown = true; + loader.hide(); + expect($ionicBackdrop.release).not.toHaveBeenCalled(); + })); + it('should cancel durationTimeout and set isShown to false', inject(function($ionicLoading, $timeout) { + spyOn($timeout, 'cancel'); + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + loader.durationTimeout = {}; + loader.isShown = true; + loader.hide({}); + expect($timeout.cancel).toHaveBeenCalledWith(loader.durationTimeout); + expect(loader.isShown).toBe(false); + })); + + }); + + it('should show with options', inject(function($ionicLoading, $timeout) { + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + spyOn(loader, 'show'); + var options = {}; + $ionicLoading.show(options); + $timeout.flush(); + expect(loader.show).toHaveBeenCalledWith(options); + })); + + it('should hide', inject(function($ionicLoading, $rootScope) { + var loader = TestUtil.unwrapPromise($ionicLoading._getLoader()); + spyOn(loader, 'hide'); + $ionicLoading.hide(); + $rootScope.$apply(); + expect(loader.hide).toHaveBeenCalled(); + })); + + }); +});