0

I am trying to call .next on a simple subject submitTask$.

My pipeline is this:

export const submitTask$ = new Subject();

function epic() {
  return submitTask$.pipe(
    map(taskId => {
        console.log('here');
        return Boolean(taskId)
    })
  )
}

I am trying to use TestScheduler to test this but cannot figure out how. I tried this:

const testScheduler = new TestScheduler((actual, expected) => {
  expect(actual).toStrictEqual(expected);
});

testScheduler.run(({ hot, expectObservable }) => {

const actions$ = hot('-', [() => submitTask$.next(task.id)]);

const output$ = epic();

expectObservable(output$).toBe('0', [true]);

However it is not working, I am never seeing the console.log('here')

Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • The RxJS `Subject` is a special type of `Observable`. If there is no subscription to an `Observer` the pipe of it won't be evaluated. So I think a `subscribe` method is needed for the `output$` in the test. (I did not use `TestScheduler` yet, that's why I'm sure if it changes something on the default behaviour.) Here is what I tested on stackblitz without `TestScheduler`: https://stackblitz.com/edit/angular-my-demo-starter-77tf72?file=src%2Fapp%2Fapp.component.ts – Milan Tenk Apr 27 '20 at 18:33

1 Answers1

2

It seems to me that you're not subscribing actions$. Also, I'm not sure [() => submitTask$.next(task.id)] will do anything.

You could try this:


testScheduler.run(({ /* ... */ }) => {
  const s = new Subject();

  const src = hot('    --a', { a: 1 /* task.id */ });
  const expectedSrc = '--1';

  const epicObs = cold('-s').pipe(mergeMapTo(epic())); // Subscribe to the subject, before the value is emitted
  const expectedEpic = '--v';
  const epicValues = { v: true };

  expectObservable(src.pipe(tap(id => s.next(id)))).toBe(expectedSrc);
  expectObservable(epicObs).toBe(expectedEpic, epicValues);
});
Andrei Gătej
  • 11,116
  • 1
  • 14
  • 31