htmlangularangular-materialjasminekarma-jasmine

unit test anchor link in Mat table cell


Trying to unit test, whether a cell is having hyperlink or not.

Not able to find the tag a via testHarness api.

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { MatTableHarness } from '@angular/material/table/testing';
import { HarnessLoader, parallel } from '@angular/cdk/testing';
import { TableHarnessExample } from './table-harness-example';

describe('TableHarnessExample', () => {
  let fixture: ComponentFixture<TableHarnessExample>;
  let loader: HarnessLoader;

  beforeEach(() => {
    fixture = TestBed.createComponent(TableHarnessExample);
    fixture.detectChanges();
    loader = TestbedHarnessEnvironment.loader(fixture);
  });

  fit('should show link in weight column', async () => {
    const table = await loader.getHarness(MatTableHarness);
    const firstRow = (await table.getRows())[0];
    const cells = await firstRow.getCells({ columnName: 'weight' });

    expect(await (await cells[0].host()).getProperty('a')).toBeDefined();
  });
});

Here is the stack blitz


Solution

  • Use the Harness method, when you want to access the properties/methods available inside the harness (click an element, select an option, check for a class, etc.).

    For your scenario, you want to access a child element (a) inside the cell, which does not itself have a Harness to access it.

    So you should try the other method using fixture.debugElement.query to get that particular cell and access the a tag.

    fit('should show link in weight column', async () => {
      let tableRows = fixture.nativeElement.querySelectorAll('tr');
      const firstRowWithData = tableRows[1];
      console.log(tableRows, firstRowWithData);
      expect(
        firstRowWithData.querySelector('.mat-column-weight > a').href
      ).toContain('dummy.com');
      expect(
        firstRowWithData.querySelector('.mat-column-weight > a').text.trim()
      ).toBe('1.0079');
    });
    

    Full Code:

    import { ComponentFixture, TestBed } from '@angular/core/testing';
    import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
    import { MatTableHarness } from '@angular/material/table/testing';
    import { HarnessLoader, parallel } from '@angular/cdk/testing';
    import { TableHarnessExample } from './table-harness-example';
    
    describe('TableHarnessExample', () => {
      let fixture: ComponentFixture<TableHarnessExample>;
      let loader: HarnessLoader;
    
      beforeEach(() => {
        fixture = TestBed.createComponent(TableHarnessExample);
        fixture.detectChanges();
        loader = TestbedHarnessEnvironment.loader(fixture);
      });
    
      fit('should show link in weight column', async () => {
        let tableRows = fixture.nativeElement.querySelectorAll('tr');
        const firstRowWithData = tableRows[1];
        console.log(tableRows, firstRowWithData);
        expect(
          firstRowWithData.querySelector('.mat-column-weight > a').href
        ).toContain('dummy.com');
        expect(
          firstRowWithData.querySelector('.mat-column-weight > a').text.trim()
        ).toBe('1.0079');
      });
    });
    

    Stackblitz Demo