简体   繁体   English

如何使用@viewChildren 在 Angular 中以 QueryList 的形式访问规范文件中的子组件属性

[英]How to Access child component property in spec file as QueryList in Angular using @viewChildren

I have the below code in the parent.component.ts file, where I'm accessing the child grid as QueryList using @ViewChildren.我在 parent.component.ts 文件中有以下代码,我在其中使用 @ViewChildren 作为 QueryList 访问子网格。

@ViewChildren(ChildComponent) childComponent: QueryList<any>;

I have this below function in the component where I accessed the childComponent property like this.我在访问 childComponent 属性的组件中具有以下功能。

checkProperty() { 
 let isNewRow = this.childComponent[_results].some(
   item => item.newRowCreated === true
 )
 if(isNewRow) { 
  this.newRowEnabled = true;
 }
}

While writing jasmine test for checkProperty() method the mock data which I created for this is throwing error and I'm unable to test this method.在为 checkProperty() 方法编写 jasmine 测试时,我为此创建的模拟数据抛出错误,我无法测试此方法。 Please find the test case code.请找到测试用例代码。

it('test checkProperty() method', () => {
  component.childComponent = {
    changes: {},
    first: {},
    last: {},
    length: 1,
    dirty: false,
    _results: [{newRowCreated: true}]
  }
  fixture.detectChanges();
  component.checkProperty();
  expect(component.newRowEnabled).toBe(true);
});
   

This code is throwing error in place where the childComponent is mocked in the spec file.此代码在规范文件中模拟 childComponent 的位置抛出错误。 Im getting the below error.我收到以下错误。

Type 'changes: {},first: {},last: {},length: 1,dirty: false,_results: [{newRowCreated: true}' is missing the following properties form type QueryList<any>: map,filter, find, reduce and 7 more.

Is this the right way to mock the childComponent as QueryList?这是将 childComponent 模拟为 QueryList 的正确方法吗? Please help to fix this.请帮助解决这个问题。

To mock things you could use ng-mocks .要模拟事物,您可以使用ng-mocks

It supports mock child components, @ViewChildren : https://ng-mocks.sudo.eu/guides/view-child , etc.它支持模拟子组件, @ViewChildrenhttps : @ViewChildren等。

So you may try something like that:所以你可以尝试这样的事情:

beforeEach(() => TestBed.configureTestingModule({
  declarations: [
    ParentComponent,
    // Mocking the child component
    MockComponent(ChildComponent),
  ],
}));

it('test checkProperty() method', () => {
  // changing properties of the child component
  ngMocks.findInstance(ChildComponent).newRowCreated = true;

  // triggers and assertions
  fixture.detectChanges();
  component.checkProperty();
  expect(component.newRowEnabled).toBe(true);
});

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

相关问题 访问 ViewChildren 查询列表的第 n 个子项(角度) - Access nth child of ViewChildren Querylist (Angular) 访问 Angular @ViewChildren 的子组件参考 - Access to child component reference for Angular @ViewChildren Angular 如何使用 ViewChildren 循环 QueryList? - Angular how to loop throught QueryList with ViewChildren? Angular 动态组件:@ViewChildren 为 QueryList 中的每个组件获取 ViewContainerRef - Angular Dynamic Components: @ViewChildren get ViewContainerRef for every component in QueryList angular 9 使用查询列表(contentChildren 或 ViewChildren)对同级 dom 重新排序/排序 - angular 9 reorder/sort siblings dom by using querylist(contentChildren or ViewChildren) Angular 如何从 viewChildren queryList 项中获取索引? - Angular how to get index from viewChildren queryList items? angular 4 如何访问ViewChildren_results - angular 4 how to access ViewChildren _results 如何将焦点设置在 ViewChildren QueryList 的第一个元素上? - How to set focus on the first element of a ViewChildren QueryList? 如何从规范文件 angular 7 中的组件访问 window object - How to access window object from component in spec file angular 7 如何在过滤属性时将 ViewChildren QueryList 中的特定项目作为 ElementRef 获取? - How to get specific item in ViewChildren QueryList as ElementRef while filtering on property(ies)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM