I have a component that render a table using Angular Material mat-table
. It is working fine, but in my spec file the table does not render the elements of the body (td
s and tr
s), so the test fails.
It seems that I initialize the text correct, I import the MatTableModule
, and also initialize the datasource for the mat-table
.
The component get input which goes to the datasource, it not async, and I call fixture.detectChanges()
after initializing the input.
The table
and body
tags are rendered but the body
tag is empty.
Any suggestions why the data of the table
is not rendered?
Thanks
here is a sample of the test code:
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
const dataSource: DataSource[] = [{id: 1, name: 'a'}];
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations:[MyComponent],
imports: [MatTableModule],
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement
});
it('should display results', () => {
component.myDataSource = dataSource;
fixture.detectChanges();
console.log(debugElement.nativeElement); //here I see that the body is empty
const elements = debugElement.queryAll(By.css('td'));
expect(elements.length).toBe(2); // I get 0
});