cartant / rxjs-marbles

An RxJS marble testing library for any test framework
https://cartant.github.io/rxjs-marbles/
MIT License
301 stars 18 forks source link

Testing async operators such as `delay()` #39

Closed Shyam-Chen closed 6 years ago

Shyam-Chen commented 6 years ago
// decrementAsync.js
import { of } from 'rxjs';
import { delay, filter } from 'rxjs/operators';

export const decrementAsync = ({ dispatch }) => {
  of(null)
    .pipe(delay(1000))
    .subscribe(() => dispatch('decrement'));  // It can not be tested
};
// decrementAsync.spec.js
import { marbles } from 'rxjs-marbles/jest';

import { decrementAsync } from './decrementAsync';

describe('decrementAsync', () => {
  it('should handle decrementAsync', marbles((m) => {
    const dispatch = (type) => {
      const source =  m.hot('--^-a-----|', { a: null });
      const subs =            '^-------!';
      const expected =        '---a----|';

      const destination = source.pipe(delay(m.time('-|')));

      m.expect(destination).toBeObservable(expected);
      m.expect(source).toHaveSubscriptions(subs);

      m.expect(type).toBe('decrement');
    };

    decrementAsync({ dispatch });
  }));
});
cartant commented 6 years ago

Please read this part of the README.