3

Gostaria de testar o tap() dentro do meu Pipe para poder cobrir este trecho de código, só falta ele para que eu atinja 100% de cobertura de testes.

fromEvent(this.input.nativeElement, 'keyup')
      .pipe(
        debounceTime(150),
        distinctUntilChanged(),
        **tap(() => {
          if (this.input.nativeElement.value.length > 2 || this.input.nativeElement.value.length == 0) {
            this.paginator.pageIndex = 0;
            this.loadAdmissionPage();
          }
        })**
      )
      .subscribe();
    this.paginator.page
      .pipe(
        **tap(() => this.loadAdmissionPage())**
      )
      .subscribe();

Tentei de várias formas testar mas não tive sucesso.

1
  • 1
    Posta o teste unitario que vc tentou 8/05/2019 às 12:27

2 Respostas 2

1

normalmente o tap se testa só dando um subscribe no observable.

mas como no seu caso é de um evento é um pouco mais complicado.

Eu faria assim:

const spy = jest.spyOn(SeuComponent, 'loadAdmissionPage');
const input = fixture.debugElement.query(
        By.css('algum filtro pra pegar esse seu this .input'),
 );
input.nativeElement.value='testing'
input.dispatchEvent(
  new KeyboardEvent('keyup', { key: 'y' })
);
expect(spy).toHaveBeenCalled();

// para testar o segundo é mais facil

const spy = jest.spyOn(SeuComponent, 'loadAdmissionPage');
component.paginator=of({})

funcaoQueEnglobaEsseSeuCodigo();

component.paginator.next({})
expect(spy).toHaveBeenCalled();
0

Consegui executar testes em cima do TAP com o seguinte trecho de código.

it('it should verify the nativeElement keyup if length = 0', ()=>{
    component.input.nativeElement.value = '';
    component.input.nativeElement.dispatchEvent(new Event('keyup'));
});

it('it should verify the nativeElement keyup if length = 1', ()=>{
        component.input.nativeElement.value = 'a';
        component.input.nativeElement.dispatchEvent(new Event('keyup'));
});

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .