Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular unit test doesn't initialize ag-Grid

I'm trying to unit test a component that contains an ag-Grid, but the onGridReady function never gets called, so all tests involving ag-Grid fail. How can I make onGridReady actually get called before my tests run?

spec file:

describe('MyComponent', () => {
  let spectator: SpectatorHost<MyComponent>;
  let fixture: ComponentFixture<MyComponent>;

  const createHost = createHostFactory({
    component: MyComponent,
    entryComponents: [MyComponent],
    imports: [
      TranslateModule.forRoot(),
      AgGridModule.withComponents([]),
      MatTooltipModule,
      InlineSVGModule.forRoot(),
      MaterialModule,
      HMSharedModule,
    ],
    providers: [
      {
        provide: MatDialogRef,
        useValue: [],
      },
      {
        provide: MAT_DIALOG_DATA,
        useValue: [],
      },
      HttpClient,
      HttpHandler,
    ],
  });

  beforeEach(() => {
    spectator = createHost(`<MyComponentSelector></MyComponentSelector>`, {
      hostProps: {},
    });
    fixture = spectator.fixture;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(spectator.component).toBeTruthy();
  });

  it('should detect changes', () => {
    spectator.fixture.detectChanges();

  });

"should create" works, but "should detect changes" fails with this error:

TypeError: Cannot read property 'setColumnDefs' of undefined

in the html, here's my ag-grid:

          <ag-grid-angular
            style="height: 157px"
            #agGrid1
            class="ag-grid ag-theme-balham"
            [rowSelection]="rowSelection"
            [columnDefs]="columnDefs1"
            [gridOptions]="gridOptions"
            (gridReady)="onGridReady($event)"
            (gridSizeChanged)="resizeColumns($event)"
            (cellValueChanged)="updateBottomGrid('agGrid1', false)"
            [singleClickEdit]="true"
          >
          </ag-grid-angular>

and here's the onGridReady:

  onGridReady(params: any): void {
    console.log('onGridReady called successfully');
    if (this.agGrid1) {
      this.agGrid1.api = params.api;
    }
  }

The console statement is never printed, so onGridReady is somehow never being called. How can I get it to be called before the unit test?

Edit: Someone mentioned that I need to provide data for columnDefs, but I already have initializeColumnDefs() which defines all columns, and I have confirmed that this does get called during the unit test, so this is not the problem.

Also, I don't think this is relevant, but this component is a MatDialog.

like image 318
Matt123 Avatar asked Nov 02 '25 14:11

Matt123


1 Answers

I was able to get rid of this error by adding:

import { ClientSideRowModelModule } from '@ag-grid-community/client-side-row-model';

modules: Module[] = [ClientSideRowModelModule];

and adding the module in the grid

          <ag-grid-angular
            style="height: 157px"
            #agGrid1
            class="ag-grid ag-theme-balham"
            [rowSelection]="rowSelection"
            [columnDefs]="columnDefs1"
            [gridOptions]="gridOptions"
            (gridReady)="onGridReady($event)"
            (gridSizeChanged)="resizeColumns($event)"
            (cellValueChanged)="updateBottomGrid('agGrid1', false)"
            [singleClickEdit]="true"
            [modules]="modules"
          >
          </ag-grid-angular>

I have no idea why that fixed it.

like image 53
Matt123 Avatar answered Nov 04 '25 05:11

Matt123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!