2017-09-12 19 views
0

: 나는 pollingTask 로그 아웃에 취소해야하기 때문에단위 테스트 REDUX-사가 작업을 취소 할 사람이 다음 REDUX-사가 로그인/로그 아웃 플로우 단위 테스트하는 방법에 대한 팁이 있다면 궁금

let pollingTask = null 

function * handleLogin() { 
    try { 
    const token = yield call(loginHandler) 
    pollingTask = yield fork(handlePolls, token) 
    yield put('LOGIN_SUCCSES') 
    } catch (e) { 
    yield put('LOGIN_FAILURE') 
    } 
} 

function * handlePolls (token) { 
    while (true) { 
    try { 
     yield call(pollHandler, token) 
     yield put('POLL_SUCCESS') 
    } catch (e) { 
     yield put('POLL_FAILURE') 
    } finally { 
     if (yield cancelled()) { 
     yield call(pollCancelled) 
     } 
    } 
    } 
} 

function * handleLogout() { 
    try { 
    yield call(logoutHandler) 
    yield cancel(pollingTask) 
    yield put('LOGOUT_SUCCESS') 
    } catch (e) { 
    yield put('LOGOUT_FAILURE') 
    } 
} 

을 , 내 테스트에서 createMockTask()을 사용해 보았지만 항상 handleLogin()이 항상 시작되고 pollingTask을 초기화한다는 것을 알고 있지만 handleLogout() saga를 호출하면 항상 undefined으로 그 값을 얻습니다.

도움이 될 것입니다.

답변

0

이 기능을 yield()로 설정하려면 이터레이터에서 .return()을 호출하십시오.

예 - https://github.com/redux-saga/redux-saga/issues/266#issuecomment-216087030

찍은

//assuming this saga 
function* saga() { 
    try { 
    const resp = yield call(someApi) 
    yield put(action(resp)) 
    } finally { 
    if(yield cancelled()) { 
     // handle logic specific to cancellation. For example 
     yield <some effect> 
    } 
    } 
} 

// test 
const gen = saga() 
expect(gen.next().value).toEqual(call(someApi)) 
// simulates cancellation 
// gen asking for cancel status 
expect(gen.return().value).toEqual(cancelled()) 
// answer yes, we've been cancelled 
expect(gen.next(true).value).toEqual(<some effect>)