Child component's template does not change, wh

2019-09-11 01:48发布

问题:

This doesn't work for me. Please make the Plunkr below work.

describe("trying a test", () => {
  beforeEach(() => {
    TestBed.initTestEnvironment(BrowserDynamicTestingModule,  platformBrowserDynamicTesting());
    TestBed.configureTestingModule({
      declarations: [myCmp, ChildCmp]
    });
  });

  it("test should work", () => {
    const fixture = TestBed.createComponent(myCmp);
    const div = fixture.debugElement.children[0];
    const childCmp = div.queryAll(By.directive(ChildCmp));

    const divEl = div.queryAll(By.css('div'));

    divEl[0].triggerEventHandler('click', <Event>{});
    fixture.detectChanges();

    expect(childCmp[0].nativeElement.textContent).toBe("updated  value");

  });

}); 

https://plnkr.co/edit/wWJMDi3ZFC6RTSvCw4HH?p=preview

回答1:

This is no longer an issue for me. For the most part (with one exception I will outline below), both my parent components and child components get updated in my real app code, though I haven't updated the Plunkr.

I just use .and.callThrough() on the spyed on parent component method that is called, when something is clicked on the template. Rather than a simple spy.

I do have an issue with ngFor rendered child components not updating. This is my other question: Angular 2 unit testing: Make ngFor rendered, child components' templates change?