如何测试RxJ的反跳史诗?

时间:2019-03-19 11:39:34

标签: rxjs jestjs redux-observable

我正在尝试将测试函数写入可观察到的Redux史诗。史诗在应用程序内部运行良好,我可以检查它是否正确消除动作的抖动,并在发射前等待300ms。但是出于某种原因,当我试图以开玩笑的方式对其进行测试时,反跳操作符会立即触发。因此,我的确保去抖的测试用例失败了。

这是测试用例

it('shall not invoke the movies service neither dispatch stuff if the we invoke before 300ms', done => {
    const $action = ActionsObservable.of(moviesActions.loadMovies('rambo'));

    loadMoviesEpic($action).subscribe(actual => {
        throw new Error('Should not have been invoked');
    });

    setTimeout(() => {
        expect(spy).toHaveBeenCalledTimes(0);
        done();
    }, 200);

});

这是我的间谍定义。

jest.mock('services/moviesService');

const spy = jest.spyOn(moviesService, 'searchMovies');

beforeEach(() => {
    moviesService.searchMovies.mockImplementation(keyword => {
        return Promise.resolve(moviesResult);
    });
    spy.mockClear();
});

这是史诗般的

import { Observable, interval } from 'rxjs';
import { combineEpics } from 'redux-observable';

import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounce';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/from';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/interval';
import 'rxjs/add/observable/concat';

import actionTypes from 'actions/actionTypes';
import * as moviesService from 'services/moviesService';
import * as moviesActions from 'actions/moviesActions';

const DEBOUNCE_INTERVAL_IN_MS = 300;
const MIN_MOVIES_SEARCH_LENGTH = 3;

export function loadMoviesEpic($action) {
  return $action
    .ofType(actionTypes.MOVIES.LOAD_MOVIES)
    .debounce(() => Observable.interval(DEBOUNCE_INTERVAL_IN_MS))
    .filter(({ payload }) => payload.length >= MIN_MOVIES_SEARCH_LENGTH)
    .switchMap(({ payload }) => {
      const loadingAction = Observable.of(moviesActions.loadingMovies());

      const moviesResultAction = Observable.from(
        moviesService.searchMovies(payload)
      )
        .map(moviesResultList => moviesActions.moviesLoaded(moviesResultList))
        .catch(err => Observable.of(moviesActions.loadError(err)));

      return Observable.concat(loadingAction, moviesResultAction);
    });
}

const rootEpic = combineEpics(loadMoviesEpic);

export default rootEpic;

因此基本上不应该调用此东西,因为反跳时间为300毫秒,而我试图在200毫秒后检查间谍。但是10毫秒之后,间谍被调用。

如何正确测试此史诗?我接受任何建议,但最好还是避免大理石测试,而只依赖计时器和假计时器。

谢谢:D

2 个答案:

答案 0 :(得分:2)

问题是debouncedebounceTime到达可观察到的反跳时会立即发出

因此,由于您使用of进行发射,因此到达了可观察的终点,debounce允许立即发出最后一个发射的值。

这是一个演示行为的简单测试:

import { of } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

test('of', () => {
  const spy = jest.fn();
  of(1, 2, 3)
    .pipe(debounceTime(1000000))
    .subscribe(v => spy(v));
  expect(spy).toHaveBeenCalledWith(3);  // 3 is emitted immediately
})

要有效地测试debouncedebounceTime,您需要使用可观察的,持续发射且不会以interval之类的东西结束的事物:

import { interval } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

test('interval', done => {
  const spy = jest.fn();
  interval(100)
    .pipe(debounceTime(500))
    .subscribe(v => spy(v));
  setTimeout(() => {
    expect(spy).not.toHaveBeenCalled();  // Success!
    done();
  }, 1000);
});

答案 1 :(得分:1)

尝试使用advanceTimersByTime

代替setTimeout