Angular Ошибка Jasmine TypeError: Невозможно прочитать свойство 'scroll', равное нулю - PullRequest
0 голосов
/ 28 мая 2020

Я получаю ошибку ниже при запуске модульного теста jasmine в angular

Ошибка:

TypeError: не удается прочитать свойство 'scroll', имеющее значение null в BoxComponent.ngAfterViewInit ( http://localhost: 9876 / _karma_webpack_ / src / app / components / box / box.component.ts: 289: 14 )

// component 

ngAfterViewInit() {
    document.querySelector('.form-div .form-box').scroll(0, 0);
  }
  
//unit test

it('should call ngAfterViewInit', () => {
    component.ngAfterViewInit();
    spyOn(document.querySelector('.form-div .form-box'), 'scroll').withArgs(0, 0).and.callThrough();
    expect(document.querySelector('.form-div .form-box').scroll).toHaveBeenCalledWith(0, 0);
    expect(component.ngAfterViewInit).toBeTruthy();
  });
  
  
// Error

TypeError: Cannot read property 'scroll' of null
    at <Jasmine>
    at BoxComponent.ngAfterViewInit (http://localhost:9876/_karma_webpack_/src/app/components/box/box.component.ts:289:14)

Ответы [ 2 ]

0 голосов
/ 28 мая 2020

Я пробовал код ниже, и он работал

el: ElementRef

 ngAfterViewInit() {
    if (this.el && this.el.nativeElement && this.el.nativeElement.parentElement) {
      this.el.nativeElement.parentElement.scrollTop = 0;
    }
  }
0 голосов
/ 28 мая 2020

Предполагается, что вы должны использовать $ document вместо document, тогда вы можете имитировать его в своих тестах. вы можете сослаться на: Angular js тестовый документ

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...