1

in saga file. generator function looks like:

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

I would like to test delay(1000) in test.js file

Lin Du
  • 88,126
  • 95
  • 281
  • 483

1 Answers1

0

Followling the Unit Testing docs. We can use testSaga function creates a mock saga for you to assert effects on.

E.g.

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();
  });
});

test result:

 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
Lin Du
  • 88,126
  • 95
  • 281
  • 483