TestScheduler for subject

345 Views Asked by At

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')

1

There are 1 best solutions below

0
Andrei Gătej On

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);
});