Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
Unit test Observable with delay operator

I have a hard time getting my unit test to work together with an Observable with delay operator. The application is built on Angular 2 and the tests are running in karma/jasmine. I've tried the async and fakeAsync helper methods but none of them are working.

Here's a simplified code block (without Angular 2) explaining my issue.

let mouseDownStream = Rx.Observable.fromEvent(document.body, 'mousedown');
let haveBeenCalled = false;
mouseDownStream.delay(200).subscribe(() => haveBeenCalled = true);

describe('mouse down event', () => {
  it('it should emit an event stream after 200ms', (done) => {
    document.body.dispatchEvent(new MouseEvent('mousedown'))
    expect(haveBeenCalled).toBeFalsy();

    // Don't want this setTimeout should use Angular's tick(200) method instead but it's not working.
    setTimeout(() => {
      expect(haveBeenCalled).toBeTruthy();
      done();
    }, 200)
  });
});

JSBin

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Here is an example on how to unit test an Observable with a delay operator in Angular 2+, should anyone still be looking for the answer:

import { fakeAsync, tick } from "@angular/core/testing";

import { fromEvent } from "rxjs";
import { delay } from "rxjs/operators";

describe("mouse down event", () => {
  it("should emit an event stream after 200ms", fakeAsync(() => {
    let mouseDownStream = fromEvent(document.body, "mousedown");
    let haveBeenCalled = false;

    const subscription = mouseDownStream.pipe(delay(200)).subscribe(() => (haveBeenCalled = true));
    document.body.dispatchEvent(new MouseEvent("mousedown"));

    expect(haveBeenCalled).toBeFalsy();

    tick(200);

    expect(haveBeenCalled).toBeTruthy();

    subscription.unsubscribe();
  }));
});

about 4 years ago · Santiago Trujillo Relatório

0

Using rxjs 6 with angular 12, fakeAsync doesn't work for me. Only using the TestScheduler did. Modifying Ritchie's sample:

describe("mouse down event", () => {
    let testScheduler: TestScheduler;
    beforeEach(() => {
      testScheduler = new TestScheduler((act, exp) => expect(exp).toEqual(act) as any);      
    });

    fit("should emit an event stream after 200ms", () => {
      let mouseDownStream = fromEvent(document.body, "mousedown");
      let haveBeenCalled = false;
  
      testScheduler.run(() => {
        const subscription = mouseDownStream.pipe(delay(200)).subscribe(() => (haveBeenCalled = true));
        document.body.dispatchEvent(new MouseEvent("mousedown"));
        expect(haveBeenCalled).toBeFalsy();
        testScheduler.createTime('200|');
        testScheduler.flush();
        expect(haveBeenCalled).toBeTruthy();
        subscription.unsubscribe();
      });
    });
});
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda