Last active
December 28, 2017 10:43
-
-
Save DavidFrahm/0e0403d38c86da77e2d1 to your computer and use it in GitHub Desktop.
Unit test for Module with $ionicPlatform.ready()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Module run with $ionicPlatform.ready() as promise', function () { | |
'use strict'; | |
var ionicPlatform; | |
var readyThenCallbackFn; | |
beforeEach(function () { | |
module('ngViews'); | |
module('swMobileApp', function ($provide) { | |
$provide.value('$ionicPlatform', { | |
// Or like this for ready callback | |
//ready: jasmine.createSpy('$ionicPlatform.ready').and.callFake(function (callbackFn) { | |
// //console.log("$ionicPlatform.ready() spy fake"); | |
// readyThenCallbackFn = callbackFn; | |
//}), | |
ready: jasmine.createSpy('$ionicPlatform.ready').and.callFake(function () { | |
//console.log("$ionicPlatform.ready() spy fake"); | |
return { | |
then: jasmine.createSpy('$ionicPlatform.ready.then').and.callFake(function (callbackFn) { | |
readyThenCallbackFn = callbackFn; | |
}) | |
}; | |
}), | |
registerBackButtonAction: jasmine.createSpy() | |
}); | |
}); | |
inject(function (_$ionicPlatform_, $window, $httpBackend) { | |
ionicPlatform = _$ionicPlatform_; | |
$window.analytics = { | |
startTrackerWithId: jasmine.createSpy('analytics.startTrackerWithIdSpy'), | |
trackView: jasmine.createSpy('analytics.trackView') | |
}; | |
$window.device = {}; | |
$window.navigator = { | |
splashscreen: { | |
hide: jasmine.createSpy('navigator.splashscreen.hide') | |
} | |
}; | |
$window.LowLatencyAudio = { | |
preloadAudio: jasmine.createSpy('LowLatencyAudio.preloadAudio'), | |
unload: jasmine.createSpy('LowLatencyAudio.unload') | |
}; | |
$window.FiksuTrackingManager = { | |
initialize: jasmine.createSpy('FiksuTrackingManager.initialize') | |
}; | |
$window.sessionm = { | |
phonegap: { | |
startSession: jasmine.createSpy('sessionm.phonegap.startSession'), | |
setAutoPresentMode: jasmine.createSpy('sessionm.phonegap.setAutoPresentMode'), | |
listenFailures: jasmine.createSpy('sessionm.phonegap.listenFailures') | |
} | |
}; | |
$window.appAvailability = { | |
check: jasmine.createSpy('appAvailability.check') | |
}; | |
spyOn($window.ionic.Platform, 'isWebView').and.returnValue(true); | |
$httpBackend.when('GET', 'http://sworkitapi.herokuapp.com/workouts?q=featured').respond({}); | |
$httpBackend.when('GET', 'http://sworkitads.herokuapp.com/adsLive/undefined').respond({}); | |
$httpBackend.when('GET', 'http://sworkitads.herokuapp.com/adsLive/EN').respond({}); | |
}); | |
}); | |
it("should initialize Google Analytics SDK", inject(function ($window, $timeout) { | |
expect(ionicPlatform.ready).toHaveBeenCalled(); | |
readyThenCallbackFn(); | |
$timeout.flush(); | |
expect($window.analytics.startTrackerWithId).toHaveBeenCalled(); | |
})); | |
it("should initialize welcome audio", inject(function ($window, $timeout) { | |
expect(ionicPlatform.ready).toHaveBeenCalled(); | |
readyThenCallbackFn(); | |
$timeout.flush(); | |
expect($window.LowLatencyAudio.preloadAudio).toHaveBeenCalledWith('welcome', jasmine.any(String), 1); | |
expect($window.LowLatencyAudio.unload).toHaveBeenCalledWith('welcome'); | |
})); | |
it("should initialize Fiksu SDK", inject(function ($window, $timeout) { | |
expect(ionicPlatform.ready).toHaveBeenCalled(); | |
readyThenCallbackFn(); | |
$timeout.flush(); | |
expect($window.FiksuTrackingManager.initialize).toHaveBeenCalledWith({iTunesApplicationID: '527219710'}); | |
})); | |
it("should initialize SessionM with iOS ID", inject(function ($window, $timeout) { | |
expect(ionicPlatform.ready).toHaveBeenCalled(); | |
spyOn(ionic.Platform, 'isAndroid').and.returnValue(false); | |
readyThenCallbackFn(); | |
$timeout.flush(); | |
expect($window.sessionm.phonegap.startSession).toHaveBeenCalledWith('c46b4d571681af4803890c8a18b71c26ce4ff3d3'); | |
})); | |
it("should initialize SessionM with Android ID", inject(function ($window, $timeout) { | |
expect(ionicPlatform.ready).toHaveBeenCalled(); | |
spyOn(ionic.Platform, 'isAndroid').and.returnValue(true); | |
readyThenCallbackFn(); | |
$timeout.flush(); | |
expect($window.sessionm.phonegap.startSession).toHaveBeenCalled(); | |
})); | |
it("should configure SessionM post-initialization", inject(function ($window, $timeout) { | |
expect(ionicPlatform.ready).toHaveBeenCalled(); | |
readyThenCallbackFn(); | |
$timeout.flush(); | |
expect($window.sessionm.phonegap.setAutoPresentMode).toHaveBeenCalledWith(true); | |
expect($window.sessionm.phonegap.listenFailures).toHaveBeenCalled(); | |
// NB: More to test, just need to mock some other things | |
})); | |
it("should hide splashscreen if device is not android", inject(function ($window, $timeout) { | |
expect(ionicPlatform.ready).toHaveBeenCalled(); | |
$window.device.platform = 'any non-android value'; | |
readyThenCallbackFn(); | |
$timeout.flush(); | |
expect($window.navigator.splashscreen.hide).toHaveBeenCalled(); | |
})); | |
it("should setup appAvailability plugin if device is android", inject(function ($window, $timeout) { | |
expect(ionicPlatform.ready).toHaveBeenCalled(); | |
spyOn(ionic.Platform, 'isAndroid').and.returnValue(true); | |
readyThenCallbackFn(); | |
$timeout.flush(); | |
expect($window.appAvailability.check).toHaveBeenCalled(); | |
// NB: More to test, just need to stub the callbacks | |
})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Correct me if I'm wrong, this should support both
$ionicPlatform.ready(callback)
and$ionicPlatform.ready().then(callback)
. It also adds support for multiple calls to$ionicPlatform.ready
: