jeudi 28 décembre 2017

Angular async detect changes

How can I "detect changes" in testing Angular without testbed?

With a component, I will have this:

it('should work', async(() => {
  const fixture = TestBed.createComponent(TestComponent);
  // do some async call
  fixture.detectChanges();   // <=== This is what I want
  expect(something).toEqual(somethingElse);
});

With a service, I don't always have a testbed. How to tell angular to wait before asserting? Example:

it('should work', async(() => {
  const service = new MyService();
  const sub = service.emitter$.subscribe((val) => {
    // expect(val).toEqual(otherVal);
  });
  service.doAsyncWork();
});

How to get these connected? What if I have two phases to check (e.g. once before that async work, once after, but all after some initial period)?

Aucun commentaire:

Enregistrer un commentaire