I have a scenario when I try to download HTML5 Audio for mobile devices, and this can only be achieved through user interaction (like ontouchstart). I implemented this logic at the Angular stage to ensure it as soon as possible. I cannot attach this in the configuration phase, as it has dependencies on other Angular factories:
angular.module('MyModule')
.run(['Device', 'ResourceManager', 'ExceptionFactory', function (Device, ResourceManager, ExceptionFactory) {
if (!Device.browser.features.webaudio) {
var onFirstUserInteraction = function () {
var sounds = ResourceManager.getSounds();
if (sounds.length > 1) {
throw ExceptionFactory.create('Html5AudioLimitReachedException', 'Html5 Audio Devices can only load one sound resource');
}
if (sounds.length === 1) {
sounds[0].createBrowserAudioObject();
}
document.documentElement.removeEventListener(Device.browser.is.IE ? 'click' : 'touchstart', onFirstUserInteraction, true);
};
document.documentElement.addEventListener(Device.browser.is.IE ? 'click' : 'touchstart', onFirstUserInteraction, true);
}
}]);
I have the following unit test crashing because the event handler above was not logged in time:
beforeEach(function () {
angular.module('Html5SoundLoaderApp', [])
.run(['Device', 'ResourceManager', function (Device, ResourceManager) {
Device.browser.features.webaudio = false;
ResourceManager.addSound('testOne', 'test/url/testOne.mp3', {});
ResourceManager.addSound('testTwo', 'test/url/testTwo.mp3', {});
}]);
module('Html5SoundLoaderApp');
});
it('should only be able to load one sound resource', inject(['ExceptionFactory', function (ExceptionFactory) {
var spy = sinon.spy(ExceptionFactory, 'create');
expect(function () {
angular.mock.ui.trigger(document.documentElement, 'touchstart')
}).to.throw();
spy.should.have.been.calledOnce;
spy.should.have.been.calledWith('Html5AudioLimitReachedException', 'Html5 Audio Devices can only load one sound resource');
}]));
Did I expect the run () block to complete execution before the test starts? Am I really mistaken in this assumption? If so, what is the best way to deal with this situation?
thanks