简体   繁体   English

使用@ContentChildren对查询列表进行单元测试

[英]Unit test for querylist using @ContentChildren

I am trying to write a unit test for a component which has child components and are injected in using ng-content . 我正在尝试为具有子组件并使用ng-content注入的组件编写单元测试。 I am trying to get the queryList of components in my test so I can test this function below but can't seem to populate the QueryList variable in my unit test. 我正在尝试在测试中获取组件的queryList,因此可以在下面测试此功能,但似乎无法在单元测试中填充QueryList变量。

  @ContentChildren(TabsContentComponent, { descendants: true, read: TabsContentComponent }) tabContent: QueryList<TabsContentComponent>;
onTabChange($event) {
this.tabContent.toArray().forEach((tab, index) => {
  tab.isActive = false;
  if ($event.value === tab.value) {
    tab.isActive = true;
  }
});

}

Here is my components: I have a component called TabsContentContainer which holds another component called Tabs. 这是我的组件:我有一个名为TabsContentContainer的组件,其中包含另一个名为Tabs的组件。 Which looks like this: 看起来像这样:

<div class="flex flex-direction-column tabs-container">
<app-tabs (tabChange)='onTabChange($event)' [tabs]="tabs"></app-tabs>
<ng-content></ng-content>

How I use the TabsContentContainer component I do this (This is just a mock version of it minus the app-tabs component): 我是如何使用TabsContentContainer组件的(这只是它的模拟版本减去app-tabs组件):

<app-tabs-container>
<app-tabs-content>content goes here</app-tabs-content>
<app-tabs-content>content goes here</app-tabs-content>
<app-tabs-content>content goes here</app-tabs-content>

I have tried following another answer to figure out what to do as it is fairly old answer but I can't seem to get my mock component to return my QueryList of app-tabs-content . 我已经尝试了另一个答案以弄清楚该怎么做,因为它是一个相当老的答案,但是我似乎无法让我的模拟组件返回app-tabs-content QueryList

Here is my spec file: 这是我的规格文件:

    @Component({
  selector: 'app-tabs-container-mock-component',
  template: `<app-tabs-container>
    <app-tabs-content></app-tabs-content>
    <app-tabs-content></app-tabs-content>
    <app-tabs-content></app-tabs-content>
  </app-tabs-container>`
})
export class TabsContainerMockComponent { }

fdescribe('TabsContainerComponent', () => {
  let component: TabsContainerComponent;
  let component2: TabsContainerMockComponent;
  console.log(component2);
  let fixture: ComponentFixture<TabsContainerComponent>;
  let fixture2: ComponentFixture<TabsContainerMockComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [TabsContainerComponent, TabsContainerMockComponent],
      schemas: [CUSTOM_ELEMENTS_SCHEMA]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(TabsContainerComponent);
    fixture2 = TestBed.createComponent(TabsContainerMockComponent);
    console.log(fixture2);
    component = fixture.componentInstance;
    component2 = fixture2.componentInstance;
    fixture.detectChanges();
    fixture2.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
    expect(component2).toBeTruthy();
  });

  it('should show the correct content when changing tabs', () => {
    const container = fixture.debugElement.children[0].componentInstance;
    debugger;
    console.log(container);
  });

What I have tried : 我尝试过的

I have tried mocking my component and trying to access it but that didn't seem to work as I get an error complaining Component 'TabsContainerMockComponent' is not included in a module and will not be available inside a template. Consider adding it to a NgModule declaration 我已经尝试过模拟我的组件并尝试访问它,但是似乎无法正常工作,因为我收到一个错误消息,提示Component 'TabsContainerMockComponent' is not included in a module and will not be available inside a template. Consider adding it to a NgModule declaration Component 'TabsContainerMockComponent' is not included in a module and will not be available inside a template. Consider adding it to a NgModule declaration

That led me to adding a mock NgModule but that also didn't work and didn't seem right. 那导致我添加了一个模拟的NgModule但是那也不起作用,而且看起来也不对。

I am lost for what to try as I cant seem to get component.tabContent to equal a QueryList of app-tabs-content . 我迷失了尝试的方法,因为我似乎无法使component.tabContent等于app-tabs-contentQueryList

The issues is that right now your TabsContainerMockComponent is actually a wrapper on top of your TabsContainerComponent . 问题在于,现在您的TabsContainerMockComponent实际上是TabsContainerComponent顶部的包装器。 In order to access your TabsContainerComponent inside of your TabsContainerMockComponent (which should probably be renamed to something like TestHostTabsContainerComponent ), you'll need to get a hold of the DebugElement and one of it's children (probably the first child) will be your TabsContainerComponent . 为了访问TabsContainerComponent您的内部TabsContainerMockComponent (这也许应该改名为类似TestHostTabsContainerComponent ),你需要得到的保持DebugElement和它的孩子中的一个(可能是第一个孩子)将是你TabsContainerComponent

You'll need a variable at the top to hold your TabsContainerComponent : 您需要在顶部使用一个变量来保存TabsContainerComponent

let tabsContainerComponent: TabsContainerComponent;

Do this in your beforeEach : 在您的beforeEach执行此beforeEach

tabsContainerComponent = fixture2.debugElement.children[0].componentInstance;

Then you can access your QueryList as it is a public property on the TabsContainerComponent : 然后你就可以访问你的QueryList ,因为它是在一个公共财产TabsContainerComponent

tabsContainerComponent.tabContent

Read more about DebugElement here: https://angular.io/api/core/DebugElement 在此处阅读有关DebugElement更多信息: https : DebugElement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM