javascript - Writing test for angular directive with dynamic template -
javascript - Writing test for angular directive with dynamic template -
here directive:
app.directive('templates',function() { homecoming { restrict:'e', templateurl: function(s,e) { switch (e.template) { case 'temp1': homecoming 'temp1.html'; case 'temp2': homecoming 'temp1.htm2'; default: // nothing... ; } } }; });
i can compile in test i'm not sure how test if right templates beingness called
there not much test here. sense test can load template cache , test if specific element has been rendered or not sense test.
example:-
describe('templates', function () { beforeeach(inject(function ($rootscope, $templatecache, $compile) { // set arbitrary template test $templatecache.put('temp1.html', '<div class="test">hello</div>'); element = angular.element("<templates template='temp1'></templates>"); $compile(element)(scope); $rootscope.$digest(); })); it('should load template', function () { expect(element.find('.test').length).toequal(1); //test if element has loaded template expect(element.find('.test').text()).toequal("hello"); });
demo
on different note directive can break if there no template
provided, required homecoming template templateurl function. can create simple directive more generic well.
.directive('templates',function() { homecoming { restrict:'e', templateurl: function(e,attr) { homecoming attr.template + ".html" }; });
how ever, there nil tested here because end testing angular's templateurl function evaluation.
javascript angularjs unit-testing jasmine
Comments
Post a Comment