【问题标题】:Testing Angular Material table is rendering correct data测试 Angular Material 表正在呈现正确的数据
【发布时间】:2019-06-12 13:25:00
【问题描述】:

我正在使用 Angular Material 表来显示用户列表,包括电子邮件、创建日期和角色。我的模板如下所示:

<table mat-table [dataSource]="dataSource" multiTemplateDataRows matSort matSortActive="email" matSortDirection="asc">
  <ng-container matColumnDef="email">
    <th mat-header-cell *matHeaderCellDef mat-sort-header>Email</th>
    <td mat-cell *matCellDef="let user" class="user-email"><a [routerLink]="['/users', user.id]">{{user.email}}</a></td>
  </ng-container>

  <ng-container matColumnDef="created">
    <th mat-header-cell *matHeaderCellDef mat-sort-header>Created</th>
    <td mat-cell *matCellDef="let user">{{user.created | date: 'medium'}}</td>
  </ng-container>

  <ng-container matColumnDef="roles">
    <th mat-header-cell *matHeaderCellDef mat-sort-header>Roles</th>
    <td mat-cell *matCellDef="let user">{{user.roles.join(', ')}}</td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let user; columns: displayedColumns;"
      class="user-row">
  </tr>
</table>

我有单元测试来确定数据是否加载到组件中,但是我也想测试数据是否在模板中正确呈现。为此,我尝试使用以下测试代码:

it('should display the users email', () => {
  expect(component.users).toBe(testUsers);

  fixture.detectChanges();
  console.log(fixture.isStable()); // displays false
  fixture.whenRenderingDone().then(() => {
    console.log(fixture.isStable()); // displays true
    const emailElement = fixture.debugElement.query(By.css('.user-email'));
    console.log(emailElement);
  });
});

我希望emailElement 是具有user-email 类的每个td 元素的列表,但它始终为空。

有没有办法测试表格是否包含正确的值?

【问题讨论】:

    标签: angular unit-testing angular-material


    【解决方案1】:

    在您的代码示例中,您实际上从未分配过 .user-email 类。您只是将类 .user-row 分配给元素 tr。如果要按类获取元素,可以使用 Angular Material 分配的类。每个td 元素都有一个mat-column-{columnName} 格式的类,例如mat-column-email 在你的情况下。

    这是我将如何测试它:

    it('should test the table ', (done) => {
      expect(component.users).toEqual(testUsers);
    
      fixture.detectChanges();
      fixture.whenStable().then(() => {
        fixture.detectChanges();
    
        let tableRows = fixture.nativeElement.querySelectorAll('tr');
        expect(tableRows.length).toBe(4);
    
        // Header row
        let headerRow = tableRows[0];
        expect(headerRow.cells[0].innerHTML).toBe('Email');
        expect(headerRow.cells[1].innerHTML).toBe('Created');
        expect(headerRow.cells[2].innerHTML).toBe('Roles');
    
        // Data rows
        let row1 = tableRows[1];
        expect(row1.cells[0].innerHTML).toBe('dummy@mail.com');
        expect(row1.cells[1].innerHTML).toBe('01-01-2020');
        expect(row1.cells[2].innerHTML).toBe('admin,standard');
    
        // Test more rows here..
    
        done();
      });
    });
    

    can find a working StackBlitz here

    【讨论】:

    • 抱歉,上面的代码应该已经包含了这个类。我现在已经对其进行了编辑以添加它。无论哪种方式,您的方法都有效,我认为这是由于使用nativeElement.querySelectorAll() 而不是fixture.debugElement.query()。谢谢!
    • 我发现这个解决方案是 OTT。我错过的部分是导入 MatTableModule。我不需要任何检测更改,因为我的表是通过 init 上的 observable 填充的。
    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2021-06-24
    相关资源
    最近更新 更多