简体   繁体   English

检查父组件规范中子组件的属性

[英]Check property of child component in parent component's spec

I have a parent component and a child component.我有一个父组件和一个子组件。 From the parent's spec file I'd like to test weather a property in the child component has been set.我想从父级的规范文件中测试是否设置了子组件中的属性。

In the ngOnInit() of ChildComponent I can console.log the value of myFormGroup.disabled and see the value I expect, but I'd like to confirm this in the spec of the parent.在 ChildComponent 的ChildComponent ngOnInit()中,我可以 console.log myFormGroup.disabled的值并查看我期望的值,但我想在父级的规范中确认这一点。

describe('ParentComponent', () => {
  let component: ParentComponent;
  let fixture: ComponentFixture<ParentComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ParentComponent, ChildComponent],
      imports: [IonicModule.forRoot()],
    }).compileComponents();

    fixture = TestBed.createComponent(ParentComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  }));

  it('should disable the myFormGroup in child component', () => {
    expect(ChildComponent.prototype.myFormGroup.disabled).toBeTrue();
    // Cannot read property 'disabled' of undefined
  });
});

You can use the By.directive selector to select the child element.您可以使用By.directive选择器来选择 select 子元素。

Try this (follow comments with:!):试试这个(跟随评论:!):

describe('ParentComponent', () => {
  let component: ParentComponent;
  let fixture: ComponentFixture<ParentComponent>;
  // !! have a handle on ChildComponent
  let childComponent: ChildComponent;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ParentComponent, ChildComponent],
      imports: [IonicModule.forRoot()],
    }).compileComponents();

    fixture = TestBed.createComponent(ParentComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    // !! get a handle on childComponent
    childComponent = fixture.debugElement.query(By.directive(ChildComponent)).componentInstance;
  }));

  it('should disable the myFormGroup in child component', () => {
    // !! change assertion
    expect(childComponent.myFormGroup.disabled).toBeTrue();
  });
});

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

相关问题 从子组件以角度形式获取父代的属性 - Get parent's property from child component in angular form 父组件中角子组件的视图 - Angular child component's view in parent component 如何访问或检查超级父级组件中子组件的模板驱动表单的验证 - How can I access or check validation of the child component's template driven form in the super parent's component 如何从 Angular 中的子组件修改父组件的 css 属性 - How to modify parent component's css property from child component in Angular 如何使用 Angular 9 中的“延迟加载”使用子组件的事件属性绑定父组件 - How to bind parent component using child component's event property using `lazy loading` in Angular 9 角度2:父子组件属性绑定 - Angular 2: Parent-Child Component Property Binding Angular5-从子组件的输入的[[ngModel)]绑定更改父级的属性 - Angular5 - Change parent's property from child component's input's [(ngModel)] binding 父组件的角度5设置子组件属性 - angular 5 setting child component property from parent component angular 父组件属性更新不运行子组件中的方法 - angular parent component property update does not run methods in child component 父组件的angular 2访问子组件属性 - angular 2 access child component property from parent component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM