Unit test gridApi.forEachNode from Ag grid using karma jasmine

Viewed 560

I am writing the unit test case for Ag-grid in Angular where I have to update a cell data on a method call. my code from service.ts file is

updateGridNode(event, gridApi) {
    gridApi.forEachNode((rowNode) => {
      if (rowNode.data.id === event.user_id) {
        rowNode.data.state = event.state_id;
        rowNode.data.status = event.status;
        gridApi.redrawRows();
      }
    });
  }

I tried following in my service.spec.ts file

import { GridApi } from '@ag-grid-community/core';
.
.
it('should update grid data when  called', () => {
    const gridApi = new GridApi();
    const rowNode = [{
      data: {
        id: '1'
      }
    }];
    const event= {
      user_id: 1,
      state_id: '1',
      status:'SomeStatus'
    };

    spyOn(gridApi, 'forEachNode');
    service.updateGridStateAsPerEvent(wsEvent, gridApi, acdUserSkills);
    spyOn(gridApi, 'redrawRows');
    expect(gridApi.redrawRows).toHaveBeenCalled();
}

but it fails.

How can I mock rowNode and get inside forEachNode function and check if the values are updated?

Versions I'm using:

Angular: 12.2.1
AG-Grid:25.3.0
Karma: 6.3.4
karma-jasmine: 4.0.0
0 Answers
Related