angularjs - Access directive attribute value in the jasmine test -
i have example angularjs directive <div some-dir="5" />
how access directive attribute value of 5
inside test?
describe("some-dir", function() { var element, scope; beforeeach(module('app')); beforeeach(inject(function($rootscope, $compile) { scope = $rootscope; element = angular.element('<div><div id="el1" some-dir="5" /></div>'); $compile(element)(scope); scope.$digest(); })); it('should able attribute value', function(){ // attr value of some-dir }); });
you can check scope values of element using isolatescope method. won't work when pass value right next directive attribute, values not copied isolated scope.
in case, it's possible , test value using element.attributes method.
first compile directive html:
var element; beforeeach(inject(function (_$compile_, _$rootscope_) { var $compile = _$compile_, $scope = _$rootscope_; element = $compile('<div my-directive="4" some-value="5"></div>')($scope); $scope.$digest(); }));
then can expect element's isolatescope return object somevalue property.
it('should expect some-value 5', function () { inject(function ($injector) { // check attribute values using isolatescope expect(element.isolatescope().somevalue).toequal(5); // check value right after directive attribute expect(element.attr('my-directive')).toequal('4'); }); });
here example plunker.
Comments
Post a Comment