Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. const encoder = credentials => Object.keys(credentials).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(credentials[key])}`).join('&')
  2.  
  3. const postLogin = credentials => {
  4. credentials.grant_type = 'password'
  5. const payload = {
  6. method: 'post',
  7. headers: config.LOGIN_HEADERS,
  8. data: encoder(credentials),
  9. url: `${config.IDENTITY_URL}/Token`
  10. }
  11. return axios(payload)
  12. }
  13.  
  14. function * loginRequest (action) {
  15. try {
  16. const res = yield call(postLogin, action.credentials)
  17. utils.storeSessionData(res.data)
  18. yield put({ type: types.LOGIN_SUCCESS, data: res.data })
  19. } catch (err) {
  20. yield put({ type: types.LOGIN_FAILURE, err })
  21. }
  22. }
  23.  
  24. function * loginSaga () {
  25. yield takeLatest(types.LOGIN_REQUEST, loginRequest)
  26. }
  27.  
  28. export default loginSaga
  29.  
  30. const loginReply = {
  31. isAuthenticating: false,
  32. isAuthenticated: true,
  33. email: 'foo@yahoo.com',
  34. token: 'access-token',
  35. userId: '1234F56',
  36. name: 'Jane Doe',
  37. title: 'Tester',
  38. phoneNumber: '123-456-7890',
  39. picture: 'pic-url',
  40. marketIds: [1, 2, 3]
  41. }
  42.  
  43. describe('login-saga', () => {
  44. it('login identity user', async (done) => {
  45. // Setup Nock
  46. nock(config.IDENTITY_URL)
  47. .post('/Token', { userName: 'xxx@xxx.com', password: 'xxxxx' })
  48. .reply(200, loginReply)
  49.  
  50. // Start up the saga tester
  51. const sagaTester = new SagaTester({})
  52.  
  53. sagaTester.start(loginSaga)
  54.  
  55. // Dispatch the event to start the saga
  56. sagaTester.dispatch({type: types.LOGIN_REQUEST})
  57.  
  58. // Hook into the success action
  59. await sagaTester.waitFor(types.LOGIN_SUCCESS)
  60.  
  61. // Check the resulting action
  62. expect(sagaTester.getLatestCalledAction()).to.deep.equal({
  63. type: types.LOGIN_SUCCESS,
  64. payload: loginReply
  65. })
  66. })
  67. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement