• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

280
Vistas
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 3 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda