Guest User

Untitled

a guest
Jan 29th, 2018
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. jest.mock('services/firebase', () => new Promise(resolve => resolve({
  2. signInWithEmailAndPassword: () => Promise.resolve({ getIdToken: 'abc123' }),
  3. getIdToken: () => jest.fn(),
  4. signOut: () => jest.fn()
  5. })));
  6.  
  7. describe('login actions', () => {
  8. let store;
  9.  
  10. beforeEach(() => {
  11. store = mockStore({});
  12. });
  13.  
  14. it('signIn should call firebase', () => {
  15. const user = {
  16. email: 'first.last@yum.com',
  17. password: 'abd123'
  18. };
  19.  
  20. return store.dispatch(signIn(user.email, user.password))
  21. .then(() => {
  22. console.log('TEST signIn SUCCESS');
  23. expect(mockSignIn).toHaveBeenCalled();
  24. expect(store.getActions()).toEqual({
  25. type: USER_ON_LOGGED_IN
  26. });
  27. })
  28. .catch((err) => {
  29. console.log('TEST signIn ERROR =>', err);
  30. });
  31. });
  32.  
  33. // Sign in action
  34. export const signIn = (email, password, redirectUrl = ROUTEPATH_DEFAULT_PAGE) => (dispatch) => {
  35. dispatch({ type: USER_LOGIN_PENDING });
  36.  
  37. return firebase
  38. .then((auth) => {
  39. console.log('auth =>', auth);
  40. return auth.signInWithEmailAndPassword(email, password);
  41. })
  42. .catch((e) => {
  43. console.error('actions/Login/signIn', e);
  44. // Register a new user
  45. if (e.code === LOGIN_USER_NOT_FOUND) {
  46. dispatch(push(ROUTEPATH_FORBIDDEN));
  47. dispatch(toggleNotification(true, e.message, 'error'));
  48. } else {
  49. dispatch(displayError(true, e.message));
  50. setTimeout(() => {
  51. dispatch(displayError(false, ''));
  52. }, 5000);
  53. throw e;
  54. }
  55. })
  56.  
  57. // I can't seem to mock this correctly
  58. .then(res => res.getIdToken())
  59. .then((idToken) => {
  60. if (!idToken) {
  61. dispatch(displayError(true, 'Sorry, there was an issue with getting your token.'));
  62. }
  63.  
  64. dispatch(onCheckAuth(email));
  65. dispatch(push(redirectUrl));
  66. });
  67. };
Add Comment
Please, Sign In to add comment