How to unit test AngularJs directive requiring parent controller with Jasmine?

I have an AngularJs accordionPanel directive that require controller of the parent accordion directive. I need to check the accordionPanel directive to see if the model has changed when I call the foldUnfold function. How to write unit test to see if the model changed when calling foldUnfold . Thats a simplified version of my directives and the test I have received so far is below:

  .directive("accordion", [ function() { return { templateUrl: "otherurl", transclude: true, replace: true, scope: { }, controller: ["$scope",function($scope) { this.isOneOpenOnly = function() { return $scope.oneOpenOnly; } }], link: function(scope, elem, attrs, ctrl, linker) { // some code } } } ]) .directive("accordionPanel", [ function() { return { templateUrl: "urlblah", transclude: true, replace: true, require: "^accordion", scope: {}, link: function(scope, elem, attrs, ctrl, linker) { scope.foldUnfold = function() { // some logic here then scope.changeThisModel=ctrl.isOneOpenOnly(); } } } } ]) 

This is my test:

 it('Should return unfolded as true', function() { var scope=$rootScope.$new(), element=$compile("<div accordion><div accordion-panel></div></div>")(scope); scope.$digest(); scope.foldUnfold(); // this is fails as scope refers to accordion but I need to access accordionPanel expect(scope.changeThisModel).toBe(true); }); 

The problem is that I cannot access the accordionPanel area where foldUnfold sits. I think it is possible to access it through $$childHead and the like, but even if it is possible, this does not seem to be the right way. How can I check it then?

+5
source share

All Articles