I have a standalone javascript file, cacheBustingInterceptor.js in a node.js app. It is a service in a factory pattern that is invoked by the app.js when the app holds.
/**
* Intercept the http request
* If the config.url is from upload or templates and is a html file
* append the cacheBusting Param
* If the template url has query param exisitng
* append &_dt=epochtime else append ?_dt=epochtime
*/
var cacheBustingInterceptor = {
CacheBustingService: CacheBustingServiceFactory
};
function CacheBustingServiceFactory() {
function CacheBustingService() {}
var possibleHtmlPaths = ['templates', 'upload'];
CacheBustingService.prototype.appendCacheBustingParam = function(templateUrl) {
for (var index = 0; index != possibleHtmlPaths.length; index++) {
// check if the url has is .html and from upload and templates
var addCacheBusting = templateUrl.indexOf(possibleHtmlPaths[index]) != - 1 &&
templateUrl.indexOf('.html') != - 1;
var hasQueryParams = templateUrl.indexOf('?') != -1;
if (addCacheBusting) {
if (hasQueryParams) {
return templateUrl + window.appendCacheBustingParam;
} else {
return templateUrl + window.cacheBustingParam;
}
}
}
return templateUrl;
};
CacheBustingService.prototype.interceptRequest = function() {
var _this = this;
var cacheBuster = {
request: function (config) {
config.url = _this.appendCacheBustingParam(config.url);
return config;
}
}
return cacheBuster;
}
return CacheBustingService;
}
the way we invoke this is by adding a injector to the app.js in the config and push the factory to the configs.
like so,
app. config([''$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('templateCacheBustingInjector');
app.factory('templateCacheBustingInjector', ['$injector', function
($injector) {
var CacheBustingService =
$injector.invoke(cacheBustingInterceptor.CacheBustingService);
var cacheBustingService = new CacheBustingService();
return cacheBustingService.interceptRequest();
}]);
Now this all works fine but I want to unit test the method 'appendCacheBustingParam' in the cacheBustingInterceptor.js and running out of ideas to invoke it from the jasmine unit test
Things tired: 1. invoking the way I invoked in the app.js, but I get service injection error or provider error 2. load the js file using require but require is not supported and I tried using browsify, however, that too doesnot help.
require('../main/webapp/media/scripts/cacheBustingInterceptor.js');
fdescribe('Cache Busting Service', function() {
var cacheBustingService;
var $injector;
beforeEach((inject(function (_$injector_) {
$injector = _$injector_;
$injector.get(cacheBustingInterceptor.CacheBustingService);
// var CacheBustingService = $injector.invoke(cacheBustingInterceptor.CacheBustingService);
})));
it('Test appendCacheBustingParam', function() {
cacheBustingService = new CacheBustingService();
spyOn(cacheBustingService.prototype, 'appendCacheBustingParam');
expect(cacheBustingService.prototype).toHaveBeenCalled();
});
});
from Test standalone JS file in Jasmine/Node.js
No comments:
Post a Comment