jeudi 21 mars 2019

Testing ag-grid row selection with karma in Angular

I've got some decent tests going, but I can't seem to get a few API calls to give me what I expect. Specifically, I want to select a row, and test that it's actually selected. For starters anyway... perhaps once that's done I can move on to test how I'm really manipulating the data.

I'm sure there's something under the sheets going on that I'm missing, although I thought fixture.detectChanges() would take care of it. It fails on the very last test of the snippet, btw. Gets 0 instead of the expected 1.

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [BrowserAnimationsModule, HttpClientTestingModule, AgGridModule.withComponents([])],
      declarations: [ MyComponent ],
      schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
      providers: [MyComponent, {provide: MyService, useClass: MockMyService} ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;

    spyOn(component, 'getMessages').and.callThrough();

    fixture.detectChanges();

    // To test whether we're storing GUI changes
    let store = {};
    const mockStorage = {
      getItem: (key: string): string => {
        return key in store ? store[key] : null;
      },
      setItem: (key: string, value: string) => {
        store[key] = `${value}`;
      },
      removeItem: (key: string) => {
        delete store[key];
      },
      clear: () => {
        store = {};
      }
    };

  });

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

  it('should handle reselecting data', () => {

  expect(component.gridApi).not.toBeTruthy();
  fixture.detectChanges();
  expect(component.gridOptions.api).toBeTruthy();

  expect(component.volumeDataWrapper).toBeDefined();
  expect(component.volumeDataWrapper.volume_data.length).toEqual(2);
  expect(component.getMessages).toHaveBeenCalled();

  expect(component.gridOptions.api.getDisplayedRowCount()).toEqual(2);

  component.gridOptions.api.forEachNode( node => {
    if (node.symbol === 'SPY') {
      node.setSelected(true);
    }
  });

  fixture.detectChanges();

  const selectedRows = component.gridOptions.api.getSelectedRows();
  expect(selectedRows.length).toEqual(1); // this is where we fail

});
});

Aucun commentaire:

Enregistrer un commentaire