Search code examples
angularkarma-jasmineag-gridag-grid-angular

How to test angular component with ag-grid


I have an angular component which is using ag-grid. I created an empty test case which is failing with error can not read property includes of undefined.

Inside my component ts file I am using includes keyword only in once place. which is inside column definition object. below is that code.

cellClass: function (params) {
    return params.value.includes('Fail') ? 'status-fail' : null;
  }

Following is my unit test setup.

 beforeEach(async(() => {  
    TestBed.configureTestingModule({
      declarations: [BannerComponent,CheckComponent,LinkComponent],
      imports: [
        RouterTestingModule, 
        FormsModule,
        HttpClientModule,  
        BsDatepickerModule.forRoot(),
        DatepickerModule.forRoot(),
        AgGridModule.withComponents([CheckComponent,LinkComponent])],
      providers: [
          { provide: BannerService, useClass: MockBannerService },         
        { provide: ActivatedRoute, useValue: {paramMap:of(convertToParamMap({Id: 1}))} }
    ],
      schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]
    })
    .compileComponents();
    fixture = TestBed.createComponent(BannerComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
}));
  fit('should create', () => {
    fixture.detectChanges();
    expect(component).toBeTruthy();
  });

I am using mock service which is returning mock object list to display in grid. Inside ngOnInit method of component service is being called. mock list has all value for every property.

Not sure why params is undefined. Any suggestion?


Solution

  • I am not sure how but removing fixture.detectchanges() from describe fixed my issue.