Skip to content

Commit

Permalink
fix: detect file type when path contains url parameters
Browse files Browse the repository at this point in the history
Fixes #137
  • Loading branch information
ocombe committed Mar 8, 2015
1 parent 887a67c commit 57e1801
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 104 deletions.
2 changes: 1 addition & 1 deletion src/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
}

if (!file_type) {
if ((m = /[.](css|less|html|htm|js)?$/.exec(path)) !== null) { // Detect file type via file extension
if ((m = /[.](css|less|html|htm|js)?(\?.*)?$/.exec(path)) !== null) { // Detect file type via file extension
file_type = m[1];
} else if(!jsLoader.hasOwnProperty('ocLazyLoadLoader') && jsLoader.hasOwnProperty('load')) { // requirejs
file_type = 'js';
Expand Down
218 changes: 115 additions & 103 deletions tests/unit/specs/ocLazyLoad.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Module: oc.lazyLoad', function() {

// get the $httpBackend from E2E tests (because the one for unit tests sucks)
module('ngMockE2E');
module(function ($provide) {
module(function($provide) {
//retrieve the $httpBackend from module ng and override $delegate from ngMockE2E
angular.injector(['ng'])
.invoke(function($httpBackend) {
Expand All @@ -33,7 +33,7 @@ describe('Module: oc.lazyLoad', function() {

//retrieve the $httpBackend from module ng and override $delegate from ngMockE2E
angular.injector(['ngMockE2E'])
.invoke(['$httpBackend', function(_$httpBackend_){
.invoke(['$httpBackend', function(_$httpBackend_) {
$httpBackend = _$httpBackend_;
}]);

Expand All @@ -55,23 +55,19 @@ describe('Module: oc.lazyLoad', function() {
$httpBackend.when('GET', new RegExp(lazyLoadUrl)).passThrough();
});

// test 1
it('service should be defined', function() {
expect($ocLazyLoad).toBeDefined();
});

// test 2
it('getModules should be working', function() {
expect($ocLazyLoad.getModules).toBeDefined();
expect(angular.isArray($ocLazyLoad.getModules())).toBe(true);
});

// test 3
it('loadedModules should be working', function() {
expect($ocLazyLoad.getModules()).toEqual(['ng', 'app1', 'oc.lazyLoad']);
});

// test 4
it('isLoaded should be working', function() {
expect($ocLazyLoad.isLoaded).toBeDefined();
expect($ocLazyLoad.isLoaded).toThrowError('You need to define the module(s) name(s)');
Expand All @@ -81,23 +77,20 @@ describe('Module: oc.lazyLoad', function() {
expect($ocLazyLoad.isLoaded(['ng', 'noModule'])).toBe(false);
});

// test 5
it('getModuleConfig should be working', function() {
expect($ocLazyLoad.getModuleConfig).toBeDefined();
expect($ocLazyLoad.getModuleConfig).toThrowError('You need to give the name of the module to get');
expect($ocLazyLoad.getModuleConfig('noModule')).toBe(null);
expect($ocLazyLoad.getModuleConfig('test')).toEqual({name: 'test', files: []});
});

// test 6
it('setModuleConfig should be working', function() {
expect($ocLazyLoad.setModuleConfig).toBeDefined();
expect($ocLazyLoad.setModuleConfig).toThrowError('You need to give the module config object to set');
expect($ocLazyLoad.setModuleConfig({name: 'test2'})).toEqual({name: 'test2'});
expect($ocLazyLoad.getModuleConfig('test2')).toEqual({name: 'test2'}); // check if set worked
});

// test 7
it('should be able to lazy load a module', function(done) {
var interval = triggerDigests(),
templateUrl = lazyLoadUrl + 'test.html',
Expand All @@ -110,16 +103,18 @@ describe('Module: oc.lazyLoad', function() {
// create spies for the following tests
window.spy = jasmine.createSpyObj('spy', ['config', 'run', 'ctrl', 'service', 'filter', 'directive']);

$ocLazyLoad.load(testModule).then(function success(res){
$ocLazyLoad.load(testModule).then(function success(res) {
window.clearInterval(interval);

// Test the module loading
expect(res).toEqual(testModule);
expect(function() { angular.module('testModule') }).not.toThrow();
expect(function() {
angular.module('testModule')
}).not.toThrow();
expect(angular.module('testModule')).toBeDefined();

// execute controller
$controller('TestCtrl', { $scope: $rootScope.$new() });
$controller('TestCtrl', {$scope: $rootScope.$new()});

// instantiate service
$injector.get('testService');
Expand All @@ -137,138 +132,155 @@ describe('Module: oc.lazyLoad', function() {
// Test the css loading
var links = document.getElementsByTagName('link');
expect(links.length).toBeGreaterThan(0);
expect(function() { links[links.length - 1].sheet.cssRules; }).not.toThrow(); // this only works if a stylesheet has been loaded
expect(function() {
links[links.length - 1].sheet.cssRules;
}).not.toThrow(); // this only works if a stylesheet has been loaded
expect(links[links.length - 1].sheet.cssRules).toBeDefined();

// because debug is set to false, we shouldn't have any log
$log.assertEmpty();

done();
}, function error(err){
}, function error(err) {
window.clearInterval(interval);
throw err;
});
});

// test 8
it('should be able to lazy load a module when specifying a file type', function(done) {
var interval = triggerDigests(),
testModuleNoExt = [
{type: 'js', path: lazyLoadUrl + 'testModule.fakejs'},
lazyLoadUrl + 'test.css',
'html!' + lazyLoadUrl + 'test.html'
];

// create spies for the following tests
window.spy = jasmine.createSpyObj('spy', ['config', 'run', 'ctrl', 'service', 'filter', 'directive']);

$ocLazyLoad.load(testModuleNoExt).then(function success(res){
window.clearInterval(interval);

// Test the module loading
expect(res).toEqual(testModuleNoExt);
//expect(function() { angular.module('testModuleNoExt') }).not.toThrow();
expect(angular.module('testModuleNoExt')).toBeDefined();

// execute controller
$controller('TestCtrlNoExt', { $scope: $rootScope.$new() });

// instantiate service
$injector.get('testServiceNoExt');

// execute filter
$filter('testFilterNoExt');

// Compile a piece of HTML containing the directive
element = $compile("<test-no-ext></test-no-ext>")($rootScope.$new());

// Test the template loading
var $templateCache = $injector.get('$templateCache');
expect($templateCache.get('/partials/test.html')).toEqual('Test partial content');

// Test the css loading
var links = document.getElementsByTagName('link');
expect(links.length).toBeGreaterThan(0);
expect(function() { links[links.length - 1].sheet.cssRules; }).not.toThrow(); // this only works if a stylesheet has been loaded
expect(links[links.length - 1].sheet.cssRules).toBeDefined();

// because debug is set to false, we shouldn't have any log
$log.assertEmpty();

done();
}, function error(err){
window.clearInterval(interval);
throw err;
});
});

// test 9
it('should be able to execute config blocks', function() {
expect(window.spy.config).toHaveBeenCalledWith('config1');
expect(window.spy.config).toHaveBeenCalledWith('config2');
expect(window.spy.config.calls.count()).toEqual(2);
});

// test 10
it('should be able to execute run blocks', function() {
expect(window.spy.run).toHaveBeenCalledWith('run1');
expect(window.spy.run).toHaveBeenCalledWith('run2');
expect(window.spy.run.calls.count()).toEqual(2);
});

// test 11
it('should be able to define controllers', function() {
expect(window.spy.ctrl).toHaveBeenCalledWith('ctrl');
expect(window.spy.ctrl.calls.count()).toEqual(1);
});

// test 12
it('should be able to define services', function() {
expect(window.spy.service).toHaveBeenCalledWith('service');
expect(window.spy.service.calls.count()).toEqual(1);
});

// test 13
it('should be able to define filters', function() {
expect(window.spy.filter).toHaveBeenCalledWith('filter');
expect(window.spy.filter.calls.count()).toEqual(1);
});

// test 14
it('should be able to define directives', function() {
expect(window.spy.directive).toHaveBeenCalledWith('directive');
expect(window.spy.directive.calls.count()).toEqual(1);
expect(element.html()).toContain("Test template");
});

// test 15
it('should be able to resolve a file name with url parameters', function(done) {
var interval = triggerDigests(),
testModule = [
lazyLoadUrl + 'test.html?v=xy12'
];

$ocLazyLoad.load(testModule).then(function success(res) {
window.clearInterval(interval);

var $templateCache = $injector.get('$templateCache');
expect($templateCache.get('/partials/test.html')).toEqual('Test partial content');

// because debug is set to false, we shouldn't have any log
$log.assertEmpty();

done();
}, function error(err) {
window.clearInterval(interval);
throw err;
});
});

it('should be able to lazy load a module when specifying a file type', function(done) {
var interval = triggerDigests(),
testModuleNoExt = [
{type: 'js', path: lazyLoadUrl + 'testModule.fakejs'},
lazyLoadUrl + 'test.css',
'html!' + lazyLoadUrl + 'test.html'
];

$ocLazyLoad.load(testModuleNoExt).then(function success(res) {
window.clearInterval(interval);

// Test the module loading
expect(res).toEqual(testModuleNoExt);
//expect(function() { angular.module('testModuleNoExt') }).not.toThrow();
expect(angular.module('testModuleNoExt')).toBeDefined();

// execute controller
$controller('TestCtrlNoExt', {$scope: $rootScope.$new()});

// instantiate service
$injector.get('testServiceNoExt');

// execute filter
$filter('testFilterNoExt');

// Compile a piece of HTML containing the directive
element = $compile("<test-no-ext></test-no-ext>")($rootScope.$new());

// Test the template loading
var $templateCache = $injector.get('$templateCache');
expect($templateCache.get('/partials/test.html')).toEqual('Test partial content');

// Test the css loading
var links = document.getElementsByTagName('link');
expect(links.length).toBeGreaterThan(0);
expect(function() {
links[links.length - 1].sheet.cssRules;
}).not.toThrow(); // this only works if a stylesheet has been loaded
expect(links[links.length - 1].sheet.cssRules).toBeDefined();

// because debug is set to false, we shouldn't have any log
$log.assertEmpty();

done();
}, function error(err) {
window.clearInterval(interval);
throw err;
});
});

it('should reject the promise when the jsLoader is unable to load a file', function(done) {
var interval = triggerDigests();

$ocLazyLoad.load(lazyLoadUrl + 'noFile.js').then(function success(res) {}, function error(err) {
expect(err.message).toEqual('Unable to load '+lazyLoadUrl + 'noFile.js');
$ocLazyLoad.load(lazyLoadUrl + 'noFile.js').then(function success(res) {
}, function error(err) {
expect(err.message).toEqual('Unable to load ' + lazyLoadUrl + 'noFile.js');
window.clearInterval(interval);
done();
});
});

// test 16
// test 17
it('should reject the promise when the cssLoader is unable to load a file', function(done) {
var interval = triggerDigests();

$ocLazyLoad.load(lazyLoadUrl + 'noFile.css').then(function success(res) {}, function error(err) {
expect(err.message).toEqual('Unable to load '+lazyLoadUrl + 'noFile.css');
$ocLazyLoad.load(lazyLoadUrl + 'noFile.css').then(function success(res) {
}, function error(err) {
expect(err.message).toEqual('Unable to load ' + lazyLoadUrl + 'noFile.css');
window.clearInterval(interval);
done();
});
});

// test 17
it('should reject the promise when the templateLoader is unable to load a file', function(done) {
var interval = triggerDigests();

$ocLazyLoad.load(lazyLoadUrl + 'noFile.html').then(function success(res) {}, function error(err) {
expect(err.message).toEqual('Unable to load template file "'+lazyLoadUrl + 'noFile.html": NOT FOUND');
$ocLazyLoad.load(lazyLoadUrl + 'noFile.html').then(function success(res) {
}, function error(err) {
expect(err.message).toEqual('Unable to load template file "' + lazyLoadUrl + 'noFile.html": NOT FOUND');
window.clearInterval(interval);
done();
});
Expand All @@ -278,48 +290,48 @@ describe('Module: oc.lazyLoad', function() {

describe('failed configs', function() {

// test 18
it('should throw an error if js loader is not a function', function() {
expect(function() {
angular.module('app2', ['oc.lazyLoad'])
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
jsLoader: ''
});
}]);
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
jsLoader: ''
});
}]);
module('app2');
inject(function(_$ocLazyLoad_) {});
inject(function(_$ocLazyLoad_) {
});
}).toThrowError(/The js loader needs to be a function/);
});

// test 19
// test 20
it('should throw an error if css loader is not a function', function() {
expect(function() {
angular.module('app2', ['oc.lazyLoad'])
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
cssLoader: ''
});
}]);
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
cssLoader: ''
});
}]);
module('app2');
inject(function(_$ocLazyLoad_) {});
inject(function(_$ocLazyLoad_) {
});
}).toThrowError(/The css loader needs to be a function/);
});

// test 20
it('should throw an error if css loader is not a function', function() {
expect(function() {
angular.module('app2', ['oc.lazyLoad'])
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
templatesLoader: ''
});
}]);
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
templatesLoader: ''
});
}]);
module('app2');
inject(function(_$ocLazyLoad_) {});
inject(function(_$ocLazyLoad_) {
});
}).toThrowError(/The template loader needs to be a function/);
});

});

});

0 comments on commit 57e1801

Please sign in to comment.