Skip to content

Commit

Permalink
fix: compatibility with jasmine
Browse files Browse the repository at this point in the history
Fixes #94
  • Loading branch information
ocombe committed Nov 13, 2014
1 parent 95b1aec commit d4985e1
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 62 deletions.
24 changes: 10 additions & 14 deletions dist/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
// Let's get the list of loaded modules & components
init(angular.element(window.document));

this.$get = ['$timeout', '$log', '$q', '$templateCache', '$http', '$rootElement', '$rootScope', '$cacheFactory', '$interval', function($timeout, $log, $q, $templateCache, $http, $rootElement, $rootScope, $cacheFactory, $interval) {
this.$get = ['$log', '$q', '$templateCache', '$http', '$rootElement', '$rootScope', '$cacheFactory', '$interval', function($log, $q, $templateCache, $http, $rootElement, $rootScope, $cacheFactory, $interval) {
var instanceInjector,
filesCache = $cacheFactory('ocLazyLoad'),
uaCssChecked = false,
Expand All @@ -49,7 +49,7 @@

// Make this lazy because at the moment that $get() is called the instance injector hasn't been assigned to the rootElement yet
providers.getInstanceInjector = function() {
return (instanceInjector) ? instanceInjector : (instanceInjector = $rootElement.data('$injector'));
return (instanceInjector) ? instanceInjector : (instanceInjector = ($rootElement.data('$injector') || angular.injector()));
};

broadcast = function broadcast(eventName, params) {
Expand Down Expand Up @@ -118,12 +118,12 @@
el.async = 1;

var insertBeforeElem = anchor.lastChild;
if(params.insertBefore) {
var element = angular.element(params.insertBefore);
if(element && element.length > 0) {
insertBeforeElem = element[0];
}
}
if(params.insertBefore) {
var element = angular.element(params.insertBefore);
if(element && element.length > 0) {
insertBeforeElem = element[0];
}
}
anchor.insertBefore(el, insertBeforeElem);

/*
Expand Down Expand Up @@ -592,13 +592,9 @@
deferred.reject(e);
return;
}
$timeout(function() {
deferred.resolve(module);
});
deferred.resolve(module);
}, function error(err) {
$timeout(function() {
deferred.reject(err);
});
deferred.reject(err);
});
}
}, function error(err) {
Expand Down
2 changes: 1 addition & 1 deletion dist/ocLazyLoad.min.js

Large diffs are not rendered by default.

24 changes: 10 additions & 14 deletions examples/example1/js/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
// Let's get the list of loaded modules & components
init(angular.element(window.document));

this.$get = ['$timeout', '$log', '$q', '$templateCache', '$http', '$rootElement', '$rootScope', '$cacheFactory', '$interval', function($timeout, $log, $q, $templateCache, $http, $rootElement, $rootScope, $cacheFactory, $interval) {
this.$get = ['$log', '$q', '$templateCache', '$http', '$rootElement', '$rootScope', '$cacheFactory', '$interval', function($log, $q, $templateCache, $http, $rootElement, $rootScope, $cacheFactory, $interval) {
var instanceInjector,
filesCache = $cacheFactory('ocLazyLoad'),
uaCssChecked = false,
Expand All @@ -49,7 +49,7 @@

// Make this lazy because at the moment that $get() is called the instance injector hasn't been assigned to the rootElement yet
providers.getInstanceInjector = function() {
return (instanceInjector) ? instanceInjector : (instanceInjector = $rootElement.data('$injector'));
return (instanceInjector) ? instanceInjector : (instanceInjector = ($rootElement.data('$injector') || angular.injector()));
};

broadcast = function broadcast(eventName, params) {
Expand Down Expand Up @@ -118,12 +118,12 @@
el.async = 1;

var insertBeforeElem = anchor.lastChild;
if(params.insertBefore) {
var element = angular.element(params.insertBefore);
if(element && element.length > 0) {
insertBeforeElem = element[0];
}
}
if(params.insertBefore) {
var element = angular.element(params.insertBefore);
if(element && element.length > 0) {
insertBeforeElem = element[0];
}
}
anchor.insertBefore(el, insertBeforeElem);

/*
Expand Down Expand Up @@ -592,13 +592,9 @@
deferred.reject(e);
return;
}
$timeout(function() {
deferred.resolve(module);
});
deferred.resolve(module);
}, function error(err) {
$timeout(function() {
deferred.reject(err);
});
deferred.reject(err);
});
}
}, function error(err) {
Expand Down
24 changes: 10 additions & 14 deletions examples/example2/js/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
// Let's get the list of loaded modules & components
init(angular.element(window.document));

this.$get = ['$timeout', '$log', '$q', '$templateCache', '$http', '$rootElement', '$rootScope', '$cacheFactory', '$interval', function($timeout, $log, $q, $templateCache, $http, $rootElement, $rootScope, $cacheFactory, $interval) {
this.$get = ['$log', '$q', '$templateCache', '$http', '$rootElement', '$rootScope', '$cacheFactory', '$interval', function($log, $q, $templateCache, $http, $rootElement, $rootScope, $cacheFactory, $interval) {
var instanceInjector,
filesCache = $cacheFactory('ocLazyLoad'),
uaCssChecked = false,
Expand All @@ -49,7 +49,7 @@

// Make this lazy because at the moment that $get() is called the instance injector hasn't been assigned to the rootElement yet
providers.getInstanceInjector = function() {
return (instanceInjector) ? instanceInjector : (instanceInjector = $rootElement.data('$injector'));
return (instanceInjector) ? instanceInjector : (instanceInjector = ($rootElement.data('$injector') || angular.injector()));
};

broadcast = function broadcast(eventName, params) {
Expand Down Expand Up @@ -118,12 +118,12 @@
el.async = 1;

var insertBeforeElem = anchor.lastChild;
if(params.insertBefore) {
var element = angular.element(params.insertBefore);
if(element && element.length > 0) {
insertBeforeElem = element[0];
}
}
if(params.insertBefore) {
var element = angular.element(params.insertBefore);
if(element && element.length > 0) {
insertBeforeElem = element[0];
}
}
anchor.insertBefore(el, insertBeforeElem);

/*
Expand Down Expand Up @@ -592,13 +592,9 @@
deferred.reject(e);
return;
}
$timeout(function() {
deferred.resolve(module);
});
deferred.resolve(module);
}, function error(err) {
$timeout(function() {
deferred.reject(err);
});
deferred.reject(err);
});
}
}, function error(err) {
Expand Down
3 changes: 2 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module.exports.conf = {
'bower_components/angular-mocks/angular-mocks.js',
'src/**/*.js',
'tests/unit/**/*.spec.js',
'tests/unit/**/*.mock.js'
'tests/unit/**/*.mock.js',
{pattern: 'tests/unit/lazyLoad/**/*.js', included: false}
],


Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"example": "examples"
},
"scripts": {
"test": "karma start tests/unit/karma.conf.js",
"test": "karma start karma.conf.js",
"build": "gulp build"
},
"repository": {
Expand Down Expand Up @@ -42,4 +42,4 @@
"semver": "^2.3.1",
"streamqueue": "^0.1.1"
}
}
}
24 changes: 10 additions & 14 deletions src/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// Let's get the list of loaded modules & components
init(angular.element(window.document));

this.$get = ['$timeout', '$log', '$q', '$templateCache', '$http', '$rootElement', '$rootScope', '$cacheFactory', '$interval', function($timeout, $log, $q, $templateCache, $http, $rootElement, $rootScope, $cacheFactory, $interval) {
this.$get = ['$log', '$q', '$templateCache', '$http', '$rootElement', '$rootScope', '$cacheFactory', '$interval', function($log, $q, $templateCache, $http, $rootElement, $rootScope, $cacheFactory, $interval) {
var instanceInjector,
filesCache = $cacheFactory('ocLazyLoad'),
uaCssChecked = false,
Expand All @@ -42,7 +42,7 @@

// Make this lazy because at the moment that $get() is called the instance injector hasn't been assigned to the rootElement yet
providers.getInstanceInjector = function() {
return (instanceInjector) ? instanceInjector : (instanceInjector = $rootElement.data('$injector'));
return (instanceInjector) ? instanceInjector : (instanceInjector = ($rootElement.data('$injector') || angular.injector()));
};

broadcast = function broadcast(eventName, params) {
Expand Down Expand Up @@ -111,12 +111,12 @@
el.async = 1;

var insertBeforeElem = anchor.lastChild;
if(params.insertBefore) {
var element = angular.element(params.insertBefore);
if(element && element.length > 0) {
insertBeforeElem = element[0];
}
}
if(params.insertBefore) {
var element = angular.element(params.insertBefore);
if(element && element.length > 0) {
insertBeforeElem = element[0];
}
}
anchor.insertBefore(el, insertBeforeElem);

/*
Expand Down Expand Up @@ -585,13 +585,9 @@
deferred.reject(e);
return;
}
$timeout(function() {
deferred.resolve(module);
});
deferred.resolve(module);
}, function error(err) {
$timeout(function() {
deferred.reject(err);
});
deferred.reject(err);
});
}
}, function error(err) {
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/lazyLoad/testModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// ngGrid is also lazy loaded by $ocLazyLoad thanks to the module dependency injection !
angular.module('testModule', []).controller('TestCtrl', function($scope) {
console.info('------- test module ctrl');
}).config(function() {
console.info('config testModule');
}).config(function() {
console.info('config 2 testModule');
}).run(function() {
console.info('run testModule');
});
20 changes: 18 additions & 2 deletions tests/unit/specs/ocLazyLoad.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
describe('Module: oc.lazyLoad', function() {
'use strict';

var $ocLazyLoad;
var $ocLazyLoad,
$rootScope;

describe('with app1', function() {

beforeEach(function() {
module('app1');

inject(function(_$ocLazyLoad_) {
inject(function(_$ocLazyLoad_, _$rootScope_) {
$ocLazyLoad = _$ocLazyLoad_;
$rootScope = _$rootScope_;
});
});

Expand Down Expand Up @@ -50,6 +52,20 @@ describe('Module: oc.lazyLoad', function() {
expect($ocLazyLoad.getModuleConfig('test2')).toEqual({name: 'test2'}); // check if set worked
});

it('should be able to lazy load a module', function(done) {
$ocLazyLoad.load({
name:'testModule',
files:['/base/tests/unit/lazyLoad/testModule.js']
}).then(function(){
done();
}, function(err){
throw err;
});
setInterval(function() {
$rootScope.$digest();
}, 10);
})

});

describe('failed configs', function() {
Expand Down

0 comments on commit d4985e1

Please sign in to comment.