I have a directive that dynamically adds a loading indicator to html element. Here is the directive:
return {
restrict: 'EA',
scope: true,
replace: true,
transclude: true,
template: '<div ng-transclude></div>',
link: function(scope, element, attr) {
var loadingExpression = attr["loadingIndicator"] || attr["loadingState"];
scope.$watch(loadingExpression, function (isLoading) {
scope["isLoading"] = isLoading;
});
$templateRequest('/templates/loading-indicator/indicator.html').then(function(template) {
$compile(template)(scope).appendTo(element);
});
// Apply position:relative to parent element if necessary
var position = element.css('position');
if (!position || position === 'static') {
element.css('position','relative');
}
}
};
Works as expected.
Now I want to write a simple tests for that directive to see if the loading spinner (part of the indicator.html template) is added to the DOM:
describe('loadingIndicatorDirective', function () {
var element, $scope;
beforeEach(function () {
module('loadingIndicator');
inject(function ($rootScope, $compile) {
element = angular.element(
'<div>' +
'<div loading-indicator="true">' +
'<p>Lorem Ipsum ...</p>' +
'<p>TEST TEST TEST</p>' +
'</div>' +
'</div>'
);
$scope = $rootScope;
$compile(element)($scope);
$scope.$digest();
})
});
it('should create a loading spinner', function () {
var spinner = element.find('.loading-indicator');
expect(spinner.length).toBe(1);
});
});
When running the test, I get the error: "Error: Unexpected request: GET /templates/loading-indicator/indicator.html". I want to have a test, that uses the actual template that I am appending in the directive. Isn't that possible?