vendredi 20 mars 2020

How can I test a function when I have a local variable in Angular?

I want to do something when I resize my window in that case push item1

export class MyComponent {
  defaultArray = [];

  @HostListener('window:resize')
  handleResize() {
    clearTimeout(this.resizeId);
    this.resizeId = setTimeout(() => {
      this. handleItemsWidth();
    }, 500);
  }

And after I want to test if defaultArray.length was changed

   handleItemsWidth() {
    const block = document ? document.getElementById('myBlock') : null;
    if (block) {
      defaultArray.push('item1');
    }
  }
}

There is my test case, I can't perform the action because I don't have access to local variables

it('should test if defaultArray length was changed', function () {
  component.defaultArray = [];
  component.handleItemsWidth();
  fixture.detectChanges();
  expect(component.defaultArray.length).toBe(1);
});

Aucun commentaire:

Enregistrer un commentaire