I an trying to write the unit test using jasmine for ag-grid.
HTMl
<ag-grid-angular style="width: 500px; height: 400px;" class="ag-theme-alpine" [rowData]="rowData" [columnDefs]="columnDefs" rowSelection='single' (gridReady)="onGridReady($event)" > </ag-grid-angular>
.ts
import { Component, Input, OnInit } from '@angular/core'; import { ColDef } from 'ag-grid-community'; import { DataGrid } from 'src/app/shared/models/datagrid.model'; @Component({ selector: 'app-simple-view', templateUrl: './simple-view.component.html', styleUrls: ['./simple-view.component.scss'] }) export class SimpleViewComponent implements OnInit { @Input('data') data: any | undefined; columnDefs: ColDef[] = [ { field: 'Geography', sortable: true }, { field: 'Category', sortable: true }, { field: 'DataType', sortable: true } ]; rowData: DataGrid[] | undefined; public gridApi: any; constructor() { } ngOnInit(): void { this.rowData = this.data; } onGridReady(params: any) { this.gridApi = params.api; } }
spec.ts
it('grid API is available after `detectChanges`', () => { fixture.detectChanges(); expect(component.gridOptions.api).toBeTruthy(); });
this test method is failing with the below error
SimpleViewComponent > grid API is available after detectChanges
Expected undefined to be truthy.
Error: Expected undefined to be truthy.
at
at UserContext. (http://localhost:9876/karma_webpack/webpack:/src/app/features/overview/components/simple-view/simple-view.component.spec.ts:45:31)
at Generator.next ()
at asyncGeneratorStep (http://localhost:9876/karma_webpack/webpack:/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:3:1)