Open ptomaszi opened 7 years ago
Hello, this shouldn't be any different than testing normal AngularJS components. If you want to test the component template you will need to populate the template cache with the template for that path. Depending on your build system there are several ways you can do that.
https://blog.logentries.com/2015/03/template-caching-in-angular-how-to-avoid-the-storm/
I have found where the issue was. If my component is using the "templateUrl" instead of "template", the render function returns "undefined" for the "ctrl". I have fixed it by modifying "src/testing/utils.js" file and adding "$scope.$digest();" after the $compile:
function renderFactory($compile, $scope) {
return _compileAndDigest;
function _compileAndDigest(Directive, _a) {
var _b = _a === void 0 ? {} : _a, jqHost = _b.jqHost, attrs = _b.attrs, jqChildren = _b.jqChildren;
var ctrlName = provider_1.getInjectableName(Directive);
var selector = primitives_1.StringWrapper.kebabCase(ctrlName);
// is Directive
if (jqHost) {
jqHost.attr(selector, '');
}
else {
// is Component
var hostElement = "<" + selector + "></" + selector + ">";
jqHost = lang_1.global.angular.element(hostElement);
}
// since attributes can be undefined we check them
if (attrs) {
jqHost.attr(attrs);
}
if (jqChildren) {
jqHost.append(jqChildren);
}
// angular api
var compiledElement = $compile(jqHost)($scope);
$scope.$digest();
var ctrl = compiledElement.controller(ctrlName);
$scope.$apply();
return { compiledElement: compiledElement, ctrl: ctrl };
}
}
The component I am testing is using the "templateUrl" to provide a template:
Then I have a corresponding unit test:
When run I am getting the following error:
When I remove templateUrl or replace with template in the component, unit tests run correctly.