Skip to content

Instantly share code, notes, and snippets.

@DavidFrahm
Last active December 28, 2017 10:43
Show Gist options
  • Save DavidFrahm/0e0403d38c86da77e2d1 to your computer and use it in GitHub Desktop.
Save DavidFrahm/0e0403d38c86da77e2d1 to your computer and use it in GitHub Desktop.
Unit test for Module with $ionicPlatform.ready()
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
}));
});
@miqmago
Copy link

miqmago commented Dec 28, 2017

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:

describe('Module run with $ionicPlatform.ready() as promise', function () {
    'use strict';
    var ionicPlatformIsReadyFns = [];
    var ionicPlatformIsReady = function () {
        for (var i = 0, il = ionicPlatformIsReadyFns.length; i < il; i += 1) {
            ionicPlatformIsReadyFns[i]();
        }
    };

    beforeEach(function () {
        module('ngViews');
        module('swMobileApp', function ($provide) {
            $provide.value('$ionicPlatform', {
                ready: jasmine.createSpy('$ionicPlatform.ready').and.callFake(function (callbackFn) {
                    // console.log("$ionicPlatform.ready() spy fake");
                    if (callbackFn) {
                        ionicPlatformIsRedayFns.push(callbackFn);
                    }
                    return {
                        then: jasmine.createSpy('$ionicPlatform.ready.then').and.callFake(function (thenCallbackFn) {
                            ionicPlatformIsRedayFns.push(thenCallbackFn);
                        }),
                    }; 
                }),
                registerBackButtonAction: jasmine.createSpy(),
            });
        });
        // ...
    });

    it("should initialize Google Analytics SDK", inject(function ($window, $timeout) {
        expect(ionicPlatform.ready).toHaveBeenCalled();

        ionicPlatformIsReady();
        $timeout.flush();

        expect($window.analytics.startTrackerWithId).toHaveBeenCalled();
    }));

    // ...

});

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment