I am writing unit test for html div having a *ngIf
condition.
<div *ngIf="clientSearchResults$ | async as searchResults" class = 'fgf' #datalist id="mydata" >
<app-client-list id="clientDataTable1" class="clientDataTable dataTable" [clients]='searchResults'></app-client-list>
</div>
This ngIf
condition gets true, when I recieved the data from ngrx store. Below is the component code, which filled this data.
searchData(client: Client) {
//// some conditions
this._clientService.getClientList()
.subscribe(data => {
const filteredData = this.filterData(data, client);
this.isDataFound = filteredData !== null && filteredData.length > 0;
this.testbool = true;
/// In this line, my div got the data and using async condition, we
/// fill the div element.
this.store.dispatch(new SetClientSearchResultsAction(filteredData));
});
}
Now, when writing the unit test case for this.
it('should search the data with valid client passed from UI', async(() => {
let debugFixture: DebugElement = fixture.debugElement;
let htmlElement: HTMLElement = debugFixture.nativeElement;
let clientListGrid = htmlElement.getElementsByClassName('fgf');
let testbool= htmlElement.getElementsByClassName('testbool');
spyOn(component, 'searchData').and.callThrough();
spyOn(component, 'filterData').and.returnValue(CLIENT_OBJECT);
spyOn(clientService, 'getClientList').and.callThrough();
console.log("=========before======="+ clientListGrid.length);
component.searchData(validClient);
component.clientSearchResults$ = store.select('searchResults');
fixture.detectChanges();
debugFixture = fixture.debugElement;
htmlElement = debugFixture.nativeElement;
clientListGrid = htmlElement.getElementsByClassName('fgf');
console.log("=========after ======="+ clientListGrid.length);
expect(component.searchData).toHaveBeenCalled();
}));
Problem is, in console, before calling the function, I am getting the length as 0 and after calling the function also, I am getting the length as 0. It should be 1, When We have received the data from store.
And It is just because of this *ngif condition, *ngIf="clientSearchResults$ | async as searchResults"
Data is getting load in DIV, but still, in unit test I am not able to test this thing ?
query(By.css('.fgf'))
orquery(By.css('.testbool'))
? are you sure thatclientListGrid
ortestbool
have the right content ? – HeerlenclientListGrid = htmlElement.getElementsByClassName('fgf'); console.log("=========after ======="+ clientListGrid.length);
this should give some correct data. length as 1 when data has been loaded – Pneumatology