Redux-observable测试叉加入大理石图

时间:2020-04-17 12:55:20

标签: rxjs redux-observable

在使用forkJoin时,我在某种程度上很难拿出大理石图。我的输出总是以某种方式表明订阅为空。

伪代码:

// epic
const epic = (action$) => action$.pipe(
  ofType('SAVE'),
  mergeMap(() => {
    const requests = [
      getJSON('/a'),
      getJSON('/b'),
      getJSON('/c'),
    ];
    return forkJoin(
      ...requests
    ).pipe(
      mergeMap(() => [
        { type: 'SAVE_DONE'},
        { type: 'LOAD'},
      ])
    );
  })
);

// mock
const dependencies = {
  getJSON: url => cold('g', {
    g: { url }
  })
};

// input
hot('i', {
  i: { type: 'SAVE' }
} 
// output??

forkJoin是并行的,但是在大理石图中它是顺序的? ggg?如果我们看一看整个流程,那就是afaik,它是igggxy,其中x和y是SAVE_DONELOAD动作。还是我完全误解了这一点?

1 个答案:

答案 0 :(得分:0)

如果对epic进行测试,您应该期望SAVE_DONELOAD连续发出2次发射,因为这需要括号。

然后该图应如下所示:

cold('(ab)', {
    a: { type: 'SAVE_DONE'},
    b: { type: 'LOAD'},
  })
};

由于getJSON的依赖性由您决定,您实际上可以在其中模拟错误或负载延迟,最长的是获胜者。

const dependencies = {
  getJSON: url =>
    url === '/a'
      ? cold('--a|', {
        a: { url }
      }) :
    url === '/b'
      ? cold('---a|', {
        a: { url }
      }) :
    url === '/c'
      ? cold('----a|', {
        a: { url }
      }) :
    cold('|', {}),
};

并且因为我们有40ms的延迟,所以主图应该像

cold('----(ab)', {
    a: { type: 'SAVE_DONE'},
    b: { type: 'LOAD'},
  })
};