0

如何对使用另一个组件的组件进行单元测试,该组件使用 ng-template 的内容投影?由于它是一个单元测试,我想省略其他组件的声明。

这是一个例子:

Component({
  selector: "my-confirmation",
  template: `
    <ng-container *ngFor="let checkboxTemplate of checkboxTemplates">
      <ng-container *ngTemplateOutlet="checkboxTemplate"></ng-container>
    </ng-container>
`,
})
export class ConfirmationComponent {  @Output() public close = new EventEmitter<boolean>();
  @ContentChildren("checkboxTemplate") public checkboxTemplates: QueryList<TemplateRef<void>>;
}

@Component({
  template: `
    <my-confirmation>
      <ng-template #checkboxTemplate>
        <mat-checkbox id="dont-ask">
          Do not show this again.
        </mat-checkbox>
      </ng-template>
    </my-confirmation>
`
})
export class UnderTestComponent {}



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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        UnderTestComponent,
        // This is a unit test, I do not want to include other components
        // ConfirmationComponent
      ]
    });
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(UnderTestComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(fixture.debugElement.query(By.css("#dont-ask")).toBeTruthy();
  });
});
4

0 回答 0