epicTester.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { Epic, ActionsObservable, StateObservable } from 'redux-observable';
  2. import { Subject } from 'rxjs';
  3. import { WebSocketSubject } from 'rxjs/webSocket';
  4. import { ActionOf } from 'app/core/redux/actionCreatorFactory';
  5. import { StoreState } from 'app/types/store';
  6. import { EpicDependencies } from 'app/store/configureStore';
  7. export const epicTester = (
  8. epic: Epic<ActionOf<any>, ActionOf<any>, StoreState, EpicDependencies>,
  9. state?: StoreState
  10. ) => {
  11. const resultingActions: Array<ActionOf<any>> = [];
  12. const action$ = new Subject<ActionOf<any>>();
  13. const state$ = new Subject<StoreState>();
  14. const actionObservable$ = new ActionsObservable(action$);
  15. const stateObservable$ = new StateObservable(state$, state || ({} as StoreState));
  16. const websockets$: Array<Subject<any>> = [];
  17. const dependencies: EpicDependencies = {
  18. getWebSocket: () => {
  19. const webSocket$ = new Subject<any>();
  20. websockets$.push(webSocket$);
  21. return webSocket$ as WebSocketSubject<any>;
  22. },
  23. };
  24. epic(actionObservable$, stateObservable$, dependencies).subscribe({ next: action => resultingActions.push(action) });
  25. const whenActionIsDispatched = (action: ActionOf<any>) => {
  26. action$.next(action);
  27. return instance;
  28. };
  29. const whenWebSocketReceivesData = (data: any) => {
  30. websockets$.forEach(websocket$ => websocket$.next(data));
  31. return instance;
  32. };
  33. const thenResultingActionsEqual = (...actions: Array<ActionOf<any>>) => {
  34. expect(resultingActions).toEqual(actions);
  35. return instance;
  36. };
  37. const thenNoActionsWhereDispatched = () => {
  38. expect(resultingActions).toEqual([]);
  39. return instance;
  40. };
  41. const instance = {
  42. whenActionIsDispatched,
  43. whenWebSocketReceivesData,
  44. thenResultingActionsEqual,
  45. thenNoActionsWhereDispatched,
  46. };
  47. return instance;
  48. };