Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. function* authFlow() {
  2. while (true) {
  3. const initialAction = yield take (['LOGIN', 'LOGOUT']);
  4. if (initialAction.type === 'LOGIN') {
  5. const { username, password } = initialAction.payload;
  6. const authTask = yield fork(authorizeWithRemoteServer, {username: username, password: password});
  7. const action = yield take(['LOGOUT', 'LOGIN_FAIL']);
  8. if (action.type === 'LOGOUT') {
  9. yield cancel(authTask);
  10. yield call (unauthorizeWithRemoteServer)
  11. }
  12. } else {
  13. yield call (unauthorizeWithRemoteServer)
  14. }
  15. }
  16. }
  17.  
  18. it ('authFlow() should work with successful login and then successful logout', () => {
  19. const mockCredentials = {username: 'User', password: 'goodpassword'};
  20. testSaga( stateAuth.watcher )
  21. //this should test the first 'yield', which is waiting for LOGIN or LOGOUT. It works
  22. .next().take(['LOGIN', 'LOGOUT'])
  23.  
  24. // This should test 'authorizeWithRemoteServer', and appears to do that properly
  25. .next({type: 'LOGIN', payload: mockCredentials}).fork( stateAuth.authorizeWithRemoteServer, mockCredentials)
  26.  
  27. // This should reflect 'yield take' after the 'yield fork', and does so
  28. .next().take(['LOGOUT', 'LOGIN_FAIL'])
  29.  
  30. /* 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:
  31. .next({type: 'LOGOUT'}).cancel(createMockTask())
  32.  
  33. ...but that results in the following, perhaps predictable, error:
  34.  
  35. cancel(task): argument task is undefined
  36.  
  37. What I found does make the test not fail is the following line, but
  38. I do not understand why it works. The fact that it matches
  39. "take(['LOGIN', 'LOGOUT'])" indicates that it has
  40. looped back to the top of the generator
  41. */
  42. .next(createMockTask()).take(['LOGIN', 'LOGOUT'])
  43. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement