i am trying to write unit test for sigma angular directive but i am getting this error. the string "Container not found." was thrown, throw an Error :)
Source code
angular
.module('test')
.directive('sigmagraph', directive);
function directive() {
var divId = 'sigma-container-' + Math.floor(Math.random() * 999999999999);
return{
restrict: 'E',
transclude: true,
template: '<div id="' + divId + '" style="width: 100%;height: 100%;" class="sigmaContainer">' +
'</div>',
scope: {
graph: '=',
isVisible: '='
},
link: function (scope, element, attrs, ctrl) {
// Create a sigma instance.
var sigmaInstance = new sigma({
settings: _newSigmaSettings,
renderer: {
container: divId,
type: 'canvas'
}
});
//other code..
}
}
}
Test code
describe('sigmagraph', function() {
var scope,element;
beforeEach(function () {
module('ui.router');
module('tresata.orion');
module('core');
inject(function ($compile, $rootScope, $templateCache) {
// $templateCache.put('<div class="sigmaContainer"></div>'); // also tried this but its not working
scope = $rootScope.$new();
element = angular.element('<sigmagraph graph="graph" is-visible="view.graphView">');
scope.graph = mockGraphData;
$compile(element)(scope);
$rootScope.$digest();
});
});
it('should apply template', function() {
expect(element.html()).to.not.be.empty;
});
i also tried to solve this problem using $templateCache but still its not working. I'm beginner in testing so may be i missed something. please help me to solve this problem.
I have solved a problem just the same as yours.
Because your directive has a link function which will be executed after compiling, and at this time, there's no element appended on the DOM.
So if you use the
$compile
service to compile your directive dynamically, you'b better append your element of directive before compile it. Modify the code like below may help you: