Is it possible to unit test a structural directive the way we test an attribute directive in angular 2

半世苍凉 提交于 2020-03-01 01:54:30

问题


I have both attribute and structural directives in my project. I am able to test the attribute directive by creating a test component and using the attribute directive in it's template.

@Component({
    template: `<input [myAttrDir]="{prop1: val1, prop2: val2}"/>`
})
export class TestComponent {
}

@Directive({
    selector: '[myAttrDir]'
})
export class MyAttrDirective {
    @Input('myAttrDir') testProp;
}

The testing module looks like:

TestBed.configureTestingModule({
    declarations: [MyAttrDirective, TestComponent]
})

I get hold of directive this way:

fixture = TestBed.createComponent(TestComponent)
directive = fixture.debugElement.query(By.directive(MyAttrDirective))

I am able to get the instance of attribute directive. However when I try this same way to test structural directive, I get null value of directive. I have checked out official documentation as well and only found the unit testing of attribute directive. The structural directive testing methodology is not given anywhere.

@Component({
    template: `<input *myStrucDir="{prop1: val1, prop2: val2}"/>`
})
export class TestComponent {
}
@Directive({
    selector: '[myStrucDir]'
})
export class MyStrucDirective {
    @Input set myStrucDir(data);
    constructor(
        private templateRef: TemplateRef<any>,
        private vcr: ViewContainerRef,
        private cfr: ComponentFactoryResolver,
        private el: ElementRef) {

    }
}
TestBed.configureTestingModule({
    declarations: [MyStrucDirective, TestComponent]
})
fixture = TestBed.createComponent(TestComponent)
directive = fixture.debugElement.query(By.directive(MyStrucDirective))

Is it possible to test structural directive in any way?


回答1:


I had the same issue, but I figured out why debugElement.query(By.directive(MyStrucDirective)) does NOT work for structural directives.

Structural directives are applied to templates (<ng-template>) instead of elements. This means, that they are not bound to any DebugElement, but rather to a DebugNode. That is a small difference, but explains why it is not found.

To find the instance, you have to query differently:

# Angular 8.1 or below
debugElement.queryAllNodes(debugNode => debugNode.providerTokens.includes(MyStrucDirective))[0];

# Angular 8.2
debugElement.queryAllNodes(By.directive(MyStrucDirective))[0];


来源:https://stackoverflow.com/questions/51839072/is-it-possible-to-unit-test-a-structural-directive-the-way-we-test-an-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!