Skip to content

Commit

Permalink
test(platform): fix unit tests for setting platform to null
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoslin committed Jul 21, 2014
1 parent 56fbf3b commit 7e20424
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
10 changes: 7 additions & 3 deletions js/utils/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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] || '';
}
},

Expand Down Expand Up @@ -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] );
}
}
Expand Down
19 changes: 5 additions & 14 deletions test/unit/angular/service/platform.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand All @@ -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() {
Expand Down Expand Up @@ -109,20 +100,20 @@ 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);
ionic.Platform.setVersion(undefined);
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);
Expand Down

0 comments on commit 7e20424

Please sign in to comment.