Modifying a component defined in TestBed.overrideComponent
Asked Answered
T

1

16

We are using

TestBed.overrideComponent(CoolComponent, {
    set: {
      template: '<div id="fake-component">i am the fake component</div>',
      selector: 'our-cool-component',
      inputs: [ 'model' ]
    }
  })

to override a component.

The component has a ViewChild which we configure in our ngOnInit method

@Component({
  selector: 'our-cool-component',
  templateUrl: 'cool.component.html'
})
export class CoolComponent implements OnInit, OnDestroy {
  @Input() model: SomeModel
  @ViewChild(CoolChildComponent) coolChildComponent;

  ngOnInit() {
    this.coolChildComponent.doStuff();
  }
}

The CoolComponent in turn lives in a Wrapper component.

When we call fixture.detectChanges() on the Wrapper fixture, this attempts to construct the CoolComponent, but it immediately dies when it calls doStuff() because CoolChildComponent is undefined.

Is there a way to get at the CoolComponent to stub its CoolChildComponent? It doesn't seem like we can get it off the Wrapper because it's only referenced through the template, not as a property of the component.

Tide answered 2/9, 2016 at 16:15 Comment(2)
I just added a bounty, apparently you aren't informed (meta.https://mcmap.net/q/751454/-filling-datagrid-and-sql-query/3001761)Hypochondriasis
Has this ever been working? Does it work when you're running it in the browser (or whatever you're targeting)? Is this a testing only problem?Cauterize
D
8
ngOnInit() {
  this.coolChildComponent.doStuff();
}

should be

ngAfterViewInit() {
  this.coolChildComponent.doStuff();
}

because

View child is set before the ngAfterViewInit callback is called.

Duvetyn answered 5/9, 2016 at 8:7 Comment(1)
Nope, this does not make a difference for me.Amphibolite

© 2022 - 2024 — McMap. All rights reserved.