Skip to content

Commit

Permalink
add unit tests for the template loader
Browse files Browse the repository at this point in the history
  • Loading branch information
fredkingham committed Jan 20, 2017
1 parent f159c1f commit 26e91d1
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 24 deletions.
46 changes: 22 additions & 24 deletions pathway/static/js/pathway/services/pathway_template_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,28 @@ angular.module('opal.services').service('PathwayTemplateLoader', function(
};

PathwayTemplateLoader.prototype = {
getTemplatePromise: function(templateUrl){
return $templateRequest(templateUrl);
},
getStepTemplates: function(){
_getStepTemplates: function(){
return _.map(this.steps, function(step){
return this.getTemplatePromise(step.template_url);
return $templateRequest(step.template_url);
}, this);
},
populateTemplateCache: function(){
_populateTemplateCache: function(){
var self = this;
var promises = [
self.getTemplatePromise(self.pathway_template_url),
self.getTemplatePromise(self.step_wrapper_template_url),
$templateRequest(self.pathway_template_url),
$templateRequest(self.step_wrapper_template_url),
];
promises = promises.concat(self.getStepTemplates());
promises = promises.concat(self._getStepTemplates());
return $q.all(promises).then(function(data){
self.cachedTemplates.baseTemplate = data[0];
self.cachedTemplates.stepWrapper = data[1];
self.cachedTemplates.stepTemplates = data.splice(2, data.length);
});
},
loadInSteps: function(pathwayTemplate){
_loadInSteps: function(pathwayTemplate){
/*
* load in the template wrapper (should already be cached)
* load in steps
* inject the step into the template wrapper
* compile the output of this with the steps scope
* aggregate all the step templates and inject them into the pathway template
* wrap the steps in the stepTemplateWrapper
* inject them into the pathway template
*/

var stepTemplateWrapper = this.cachedTemplates.stepWrapper;
Expand All @@ -62,27 +56,31 @@ angular.module('opal.services').service('PathwayTemplateLoader', function(
wrappedTemplate.find(this.step_template_node).replaceWith(stepTemplate);
return $compile(wrappedTemplate)(step.scope);
}, this);
$(pathwayTemplate).find(".to_append").append(allSteps);
pathwayTemplate.find(".to_append").append(allSteps);
},
injectSteps: function(loadedHtml){
_loadInPathway: function(){
/*
* injects the pathway base templates into the document, then the step templates
*/
var baseTemplate = this.cachedTemplates.baseTemplate;
var result = $compile(baseTemplate)(this.newScope);
var pathwayTemplate = $(this.pathway_insert);

pathwayTemplate.append(result);

if(!$(this.pathway_insert).size()){
if(!pathwayTemplate.size()){
throw "Unable to find base template to append to";
}
this.loadInSteps(pathwayTemplate)

pathwayTemplate.append(result);

this._loadInSteps(pathwayTemplate)
},
load: function(newScope, pathway_insert, step_wrapper_template_url, pathway_template_url, steps){
load: function(){
/*
* the public method that triggers the load
*/
var self = this;
this.populateTemplateCache().then(function(loadedHtml){
self.injectSteps(loadedHtml);
return this._populateTemplateCache().then(function(){
self._loadInPathway();
});
}
};
Expand Down
137 changes: 137 additions & 0 deletions pathway/static/js/pathwaytest/pathway_template_loader_service.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,140 @@
describe('pathwayTemplateLoader', function() {
var iterator, PathwayTemplateLoader, pathway_template_loader;
var $rootScope, scope, pathway_insert, step_wrapper_template_url;
var compileSpy, templateRequest;

beforeEach(function(){
iterator = 0;
compileSpy = jasmine.createSpy('compileSpy').and.returnValue('compiled');
templateRequest = jasmine.createSpy('templateRequest').and.returnValue({
then: function(fn){
iterator += 1;
fn(String(iterator));
}
});

module('opal.services', function($provide){
$provide.service('$templateRequest', function(){
return templateRequest;
});
$provide.service('$compile', function(){
return function(){
return compileSpy;
};
});
});


inject(function($injector) {
PathwayTemplateLoader = $injector.get('PathwayTemplateLoader');
$rootScope = $injector.get('$rootScope');
});

scope = $rootScope.$new();
pathway_insert = ".pathway-insert";
step_wrapper_template_url = "templates/template_wrapper_url";

pathway_template_loader = new PathwayTemplateLoader(
scope,
".pathway-insert",
'.something',
step_wrapper_template_url,
[
{
template_url: 'step_1_template_url',
scope: "scope1"
},
{
template_url: 'step_2_template_url',
scope: "scope2"
}
]
);
});

describe('_getStepTemplates', function(){
it('should request promises for all the step templates and return their promises', function(){
pathway_template_loader._getStepTemplates();
expect(templateRequest).toHaveBeenCalled();
expect(templateRequest.calls.argsFor(0)).toEqual(['step_1_template_url']);
expect(templateRequest.calls.argsFor(1)).toEqual(['step_2_template_url']);
});
});

describe('_populateTemplateCache', function(){
it('should load in all the templates', function(){
pathway_template_loader._populateTemplateCache();
expect(templateRequest.calls.count()).toBe(4);
$rootScope.$apply();
expect(pathway_template_loader.cachedTemplates.baseTemplate).toBe("1");
expect(pathway_template_loader.cachedTemplates.stepWrapper).toBe("2");
expect(pathway_template_loader.cachedTemplates.stepTemplates).toEqual(["3", "4"]);
});
});

describe('loadInSteps', function(){
it('should load the steps templates into the template', function(){
find = jasmine.createSpy('find spy');
append = jasmine.createSpy('append spy');

var pathwayTemplateSpy = {
find: find.and.returnValue({
append: append,
})
};

pathway_template_loader.cachedTemplates.baseTemplate = "1";
pathway_template_loader.cachedTemplates.stepWrapper = '<div><div class="something"></div></div>';
pathway_template_loader.cachedTemplates.stepTemplates = ["3", "4"];
pathway_template_loader._loadInSteps(pathwayTemplateSpy);
expect(compileSpy.calls.count()).toBe(2);
expect(compileSpy.calls.argsFor(0)).toEqual(['scope1']);
expect(compileSpy.calls.argsFor(1)).toEqual(['scope2']);
expect(append.calls.argsFor(0)).toEqual([['compiled', 'compiled']]);
});
});

describe('loadInPathway', function(){
it('should load in the pathway', function(){
spyOn(pathway_template_loader, "_loadInSteps");
append = jasmine.createSpy('append');
spyOn(window, "$").and.returnValue({
append: append,
size: function(){ return 1; }
});
pathway_template_loader._loadInPathway();
expect(append).toHaveBeenCalled()
expect(pathway_template_loader._loadInSteps).toHaveBeenCalled();
});

it('should raise an error if its unable to find the pathway insert', function(){
spyOn(pathway_template_loader, "_loadInSteps");
append = jasmine.createSpy('append');
spyOn(window, "$").and.returnValue({
append: append,
size: function(){ return 0; }
});
try{
pathway_template_loader._loadInPathway();
}
catch(err){
expect(err).toEqual("Unable to find base template to append to");
}

expect(append).not.toHaveBeenCalled();
expect(pathway_template_loader._loadInSteps).not.toHaveBeenCalled();
});
});

describe('load', function(){
it('should call through', function(){
spyOn(pathway_template_loader, "_populateTemplateCache").and.returnValue({
then: function(fn){fn();}
});
spyOn(pathway_template_loader, "_loadInPathway");
pathway_template_loader.load();
expect(pathway_template_loader._populateTemplateCache).toHaveBeenCalled();
expect(pathway_template_loader._loadInPathway).toHaveBeenCalled();
});
});
});

0 comments on commit 26e91d1

Please sign in to comment.