redux-observable测试forkJoin大理石图

问题描述 投票:1回答:1

使用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动作。还是我完全误解了?

rxjs redux-observable
1个回答
0
投票

[如果测试epic,您应该期望2个SAVE_DONELOAD发射一个接一个,因为需要括号。

然后,该图应该看起来像:

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'},
  })
};
© www.soinside.com 2019 - 2024. All rights reserved.