Angular 2 unit tests: Child component template not updating when child component bound to css class with "." selector?

时间:2016-10-20 19:07:20

标签: unit-testing angular

Please make my Plunkr test pass - https://plnkr.co/edit/dH5UVuqQOhBrSbqjZekN?p=preview. The child component must be bound to the class property of tr, and this type of *ngFor is necessary too.

This is my parent component:

@Component({
selector: 'my-cmp',
template: `<div (click)="updateMe()"></div><tr class="child-cmp"  *ngFor="let _testVar of _testVars" [_testVar]="_testVar">`
})

export class myCmp {
  _testVars = ["initial value"];
   private updateMe(): void {
      this._testVars = ["updated value"];
   }
}

I just want this test to work:

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

  it("test should work", () => {
    const fixture = TestBed.createComponent(myCmp);
    fixture.detectChanges();
    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");
    expect(true).toBe(true);
  });

}); 

0 个答案:

没有答案