1

我无法在表中使用 lazayload 测试 virtualScroll

  <p-table class="basicTable" [value]="cars" [scrollable]="true" [rows]="100" scrollHeight="500px"
             (onLazyLoad)="load($event)" [rows]="100" lazy="true"
             [virtualScroll]="true">
  const tableEl = fixture.debugElement.query(By.css('.p-datatable-virtual-scrollable-body'));
    fixture.detectChanges();
    const bodyRows = tableEl.query(By.css('.p-datatable-tbody')).queryAll(By.css('tr'));
    console.log(bodyRows)

bodyRows 为空

谢谢

4

1 回答 1

0

Angular 团队的单元测试为我提供了答案。CDK Virtual Scroll 显示数据之前有许多异步步骤,因此我们必须模拟这些步骤。取而代之的是fixture.detectChanges(),将以下函数添加到.spec.ts文件顶部并在传递fixture参数时调用它:

const finishInit = (fixture: ComponentFixture<any>) => {
  // On the first cycle we render and measure the viewport.
  fixture.detectChanges();
  flush();

  // On the second cycle we render the items.
  fixture.detectChanges();
  flush();

  // Flush the initial fake scroll event.
  tick(500);
  flush();
  fixture.detectChanges();
};

it还需要使用fakeAsync

it('should display virtual scroll data', fakeAsync(() => {


}));
于 2021-04-29T16:58:40.967 回答