From 7e20424a87ff8cd49903304189eed7b5854efe83 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 21 Jul 2014 08:22:07 -0600 Subject: [PATCH] test(platform): fix unit tests for setting platform to null --- js/utils/platform.js | 10 +++++++--- test/unit/angular/service/platform.unit.js | 19 +++++-------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/js/utils/platform.js b/js/utils/platform.js index c23c700487..ee0b9eb911 100644 --- a/js/utils/platform.js +++ b/js/utils/platform.js @@ -11,6 +11,10 @@ */ ionic.Platform = { + // Put navigator on platform so it can be mocked and set + // the browser does not allow window.navigator to be set + navigator: window.navigator, + /** * @ngdoc property * @name ionic.Platform#isReady @@ -157,7 +161,7 @@ * @returns {boolean} Whether we are running on iPad. */ isIPad: function() { - if( /iPad/i.test(window.navigator.platform) ) { + if( /iPad/i.test(ionic.Platform.navigator.platform) ) { return true; } return /iPad/i.test(this.ua); @@ -211,7 +215,7 @@ } else if(this.ua.indexOf('Windows Phone') > -1) { platformName = WINDOWS_PHONE; } else { - platformName = window.navigator.platform && navigator.platform.toLowerCase().split(' ')[0] || ''; + platformName = ionic.Platform.navigator.platform && navigator.platform.toLowerCase().split(' ')[0] || ''; } }, @@ -250,7 +254,7 @@ }; if(versionMatch[pName]) { v = this.ua.match( versionMatch[pName] ); - if(v.length > 2) { + if(v && v.length > 2) { platformVersion = parseFloat( v[1] + '.' + v[2] ); } } diff --git a/test/unit/angular/service/platform.unit.js b/test/unit/angular/service/platform.unit.js index f35499fa08..1f731e62a0 100644 --- a/test/unit/angular/service/platform.unit.js +++ b/test/unit/angular/service/platform.unit.js @@ -5,7 +5,7 @@ describe('Ionic Platform Service', function() { beforeEach(inject(function($window, $ionicPlatform, $rootScope) { window = $window; - window.navigator = { + ionic.Platform.navigator = { platform: '' }; ionic.Platform.ua = ''; @@ -20,15 +20,6 @@ describe('Ionic Platform Service', function() { ionic.Platform.setPlatform('iOS'); expect(ionic.Platform.platform()).toEqual('ios'); - - ionic.Platform.setPlatform(''); - expect(ionic.Platform.platform()).toEqual(''); - - ionic.Platform.setPlatform(null); - expect(ionic.Platform.platform()).toEqual(''); - - ionic.Platform.setPlatform(); - expect(ionic.Platform.platform()).toEqual(''); }); it('set version with device', function() { @@ -109,10 +100,10 @@ describe('Ionic Platform Service', function() { expect(ionic.Platform.isIPad()).toEqual(true); }); - it('should be iPad from iPad in window.navigator.platform and webview, but iPhone in user agent', function() { + it('should be iPad from iPad in ionic.Platform.navigator.platform and webview, but iPhone in user agent', function() { window.cordova = {}; ionic.Platform.ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25'; - window.navigator = { + ionic.Platform.navigator = { platform: 'iPad Simulator' }; ionic.Platform.setPlatform(undefined); @@ -120,9 +111,9 @@ describe('Ionic Platform Service', function() { expect(ionic.Platform.isIPad()).toEqual(true); }); - it('should not be iPad from no in window.navigator.platform, and iPhone in user agent', function() { + it('should not be iPad from no in ionic.Platform.navigator.platform, and iPhone in user agent', function() { ionic.Platform.ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25'; - window.navigator = {}; + ionic.Platform.navigator = {}; ionic.Platform.setPlatform(undefined); ionic.Platform.setVersion(undefined); expect(ionic.Platform.isIPad()).toEqual(false);