lundi 16 septembre 2019

ag-grid-angular testing grid changes with mocha

Trying to run some tests w/ ag-grid. I have a function that's bound to cellValueChanged. The function is tested fine, but I'd like to make sure that it's actually called by editing a cell, in the test. I tried a couple of different methods to no avail. (The aysnc one was kind of a stab, tbh).

Perhaps such things should be limited to e2e?

// Component

  onCellValueChanged(event: any) {
    this.hasValidationErrors = true;
  }

// HTML

  <ag-grid-angular #agGrid class="ag-theme-balham"
    [gridOptions]="gridOptions"
    (cellValueChanged)="onCellValueChanged($event)"
    >
  </ag-grid-angular>

// Test 1

  it('should have validation errors on load and cell change', () => {
    component.hasValidationErrors = false;
    expect(component.hasValidationErrors).toBe(false);
    fixture.detectChanges();
    expect(component.gridOptions.api).toBeTruthy();

    component.gridOptions.api.forEachNodeAfterFilterAndSort( (rowNode, index) => {
      rowNode.setDataValue('someColumn', 'Anything');
    });

    fixture.detectChanges();
    expect(component.hasValidationErrors).toBe(true); // This simple fails as onCellValueChanged() is never called
  });

// Test 2

  it('should have validation errors on load and cell change async', async () => {
    expect(component.gridOptions.api).toBeTruthy();
    component.gridOptions.api.forEachNodeAfterFilterAndSort( (rowNode, index) => {
      rowNode.setDataValue('someColumn', 'Anything');
    });

    fixture.whenStable().then( () => {
      fixture.detectChanges();
      // This block is never executed
      console.log(`Checking when false`);
      expect(component.hasValidationErrors).toBe(true);
    })
  });

Aucun commentaire:

Enregistrer un commentaire