Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function* authFlow() {
- while (true) {
- const initialAction = yield take (['LOGIN', 'LOGOUT']);
- if (initialAction.type === 'LOGIN') {
- const { username, password } = initialAction.payload;
- const authTask = yield fork(authorizeWithRemoteServer, {username: username, password: password});
- const action = yield take(['LOGOUT', 'LOGIN_FAIL']);
- if (action.type === 'LOGOUT') {
- yield cancel(authTask);
- yield call (unauthorizeWithRemoteServer)
- }
- } else {
- yield call (unauthorizeWithRemoteServer)
- }
- }
- }
- it ('authFlow() should work with successful login and then successful logout', () => {
- const mockCredentials = {username: 'User', password: 'goodpassword'};
- testSaga( stateAuth.watcher )
- //this should test the first 'yield', which is waiting for LOGIN or LOGOUT. It works
- .next().take(['LOGIN', 'LOGOUT'])
- // This should test 'authorizeWithRemoteServer', and appears to do that properly
- .next({type: 'LOGIN', payload: mockCredentials}).fork( stateAuth.authorizeWithRemoteServer, mockCredentials)
- // This should reflect 'yield take' after the 'yield fork', and does so
- .next().take(['LOGOUT', 'LOGIN_FAIL'])
- /* this is where I don't understand what's happening. What I would think I should do is something like this, if I want to test the logout path:
- .next({type: 'LOGOUT'}).cancel(createMockTask())
- ...but that results in the following, perhaps predictable, error:
- cancel(task): argument task is undefined
- What I found does make the test not fail is the following line, but
- I do not understand why it works. The fact that it matches
- "take(['LOGIN', 'LOGOUT'])" indicates that it has
- looped back to the top of the generator
- */
- .next(createMockTask()).take(['LOGIN', 'LOGOUT'])
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement