Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

361
Views
Angular unit test case- Cannot read properties of undefined (reading 'forEachNode')

I am trying to write unit test cases for Grid, for this I need to loop through gridapi data but it is giving me Cannot read undefined forEachNode error.

myComponent.component.ts.

          import { Component, OnInit, } from '@angular/core';
          import { GridOptions, GridApi, } from "ag-grid-community";

          @Component({
                selector: 'my-component',
                templateUrl: './mycomponent.component.html',
                styleUrls: ['./mycomponent.component.scss']
          })
          export class MyComponent implements OnInit {

             grid: GridOptions;
             data;
             constructor() {}
             getData() {
                this.grid.api.forEachNode((node) =>
                   this.data.push(node.data.id);
                );
             }   
          }

mycomponent.component.spec.ts:

          import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
          import { Mycomponent} from './mycomponent.component';
          
          describe('Mycomponent', () => {
                    let component: Mycomponent;
                    let fixture: ComponentFixture<Mycomponent>;

          beforeEach(
             waitForAsync(() => {
               TestBed.configureTestingModule({
                 declarations: [Mycomponent],
                 }).compileComponents();
                })
           );

          beforeEach(() => {
              fixture = TestBed.createComponent(Mycomponent);
              component = fixture.componentInstance;
              const gridObj = jasmine.createSpyObj('grid', [
                    'api',
              ]);
              gridObj.grid.api.and.returnValue({
                   context: {}
                   data : { id:1,name: 'columName'}
             });
             component.grid = gridObj;
             fixture.detectChanges();
          });

           it(`should call getData method.`, () => {
            component.getData();
            expect(component.data).toEqual('[1]');
           });
          });

When I try to run above test case, TypeError: Cannot read properties of undefined (reading 'forEachNode'). Can any one tell me where I am doing wrong.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!