如何测试位于saga文件(使用jest文件)中的生成器函数中的yield delay(some_delay)?

时间:2020-08-19 20:56:32

标签: jestjs redux-saga redux-saga-test-plan

saga文件中的

。生成器函数如下:

function* handleCall(){
   yield delay(1000);
   yield put(act.call());
}

我想测试test.js文件中的delay(1000)

1 个答案:

答案 0 :(得分:0)

遵循 Unit Testing 文档。我们可以使用 testSaga 函数创建一个模拟 saga 供您断言效果。

例如

saga.ts

import { delay, put } from 'redux-saga/effects';
import * as act from './actionCreators';

export function* handleCall() {
  yield delay(1000);
  yield put(act.call());
}

actionCreators.ts

export function call() {
  return {
    type: 'HANDLE_CALL',
  };
}

saga.test.ts

import { testSaga } from 'redux-saga-test-plan';
import { handleCall } from './saga';
import * as act from './actionCreators';

describe('63494870', () => {
  it('should pass', () => {
    testSaga(handleCall).next().delay(1000).next().put(act.call()).next().isDone();
  });
});

测试结果:

 PASS  src/stackoverflow/63494870/saga.test.ts
  63494870
    ✓ should pass (3 ms)

-------------------|---------|----------|---------|---------|-------------------
File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-------------------|---------|----------|---------|---------|-------------------
All files          |     100 |      100 |     100 |     100 |                   
 actionCreators.ts |     100 |      100 |     100 |     100 |                   
 saga.ts           |     100 |      100 |     100 |     100 |                   
-------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.689 s