在玩笑测试期间RxJs的反跳会立即发出事件

时间:2019-03-05 20:58:05

标签: reactjs 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);

});

这是史诗般的

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;

我要避免进行大理石测试,该怎么做?


编辑:

这是我的间谍定义。

    jest.mock('services/moviesService');

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

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

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

0 个答案:

没有答案