Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { call, put } from 'redux-saga/effects';
  2. import * as loginSaga from './loginSaga';
  3. import { LoginManager, AccessToken } from 'react-native-fbsdk';
  4.  
  5. import { AuthActions } from '../../state/auth/types';
  6. import { authActionsCreators } from '../../state/auth/actions';
  7. import navigationService from '../../services/NavigationService';
  8.  
  9. describe('loginSaga', () => {
  10.    
  11.     describe('facebookLoginFlow', () => {
  12.         let facebookFlowGen: IterableIterator<any>;
  13.  
  14.        
  15.         describe('Error has occured durning authorization', () => {
  16.             beforeEach(() => {
  17.                 facebookFlowGen = loginSaga.facebookLoginFlow();
  18.                 facebookFlowGen.next();
  19.             });
  20.            
  21.             it(`should put ${AuthActions.REQUEST_FAIL} with custom error message`, () => {
  22.                 expect(facebookFlowGen.throw({}).value)
  23.                     .toEqual(
  24.                         put(authActionsCreators.requestFail('Error when loggin in to facebook.'))
  25.                     );
  26.             });
  27.  
  28.             it(`should put ${AuthActions.REQUEST_FAIL} with given error message`, () => {
  29.                 const error = {
  30.                     message: 'Given error.'
  31.                 };  
  32.                 expect(facebookFlowGen.throw(error).value)
  33.                     .toEqual(
  34.                         put(authActionsCreators.requestFail(error.message))
  35.                     );
  36.             });
  37.         });
  38.  
  39.         describe('User has cancelled authentication', () => {
  40.             beforeAll(() => {
  41.                 facebookFlowGen = loginSaga.facebookLoginFlow();
  42.             });
  43.  
  44.             it('should call logInWithReadPermissions function with success', () => {
  45.                 expect(facebookFlowGen.next().value)
  46.                     .toEqual(
  47.                         call(
  48.                             [LoginManager, LoginManager.logInWithReadPermissions],
  49.                             ['public_profile', 'email']
  50.                         )
  51.                     );
  52.             });
  53.  
  54.             it('generator should be done, after cancellation', () => {
  55.                 const response = {
  56.                     isCancelled: true
  57.                 };
  58.  
  59.                 expect(facebookFlowGen.next(response).done)
  60.                     .toBeTruthy();
  61.             });
  62.         });
  63.  
  64.         describe('User has been authorized', () => {
  65.             const accessTokenResponse = {
  66.                 accessToken: 'myToken'
  67.             };
  68.  
  69.             beforeAll(() => {
  70.                 facebookFlowGen = loginSaga.facebookLoginFlow();
  71.             });
  72.  
  73.             it('should call logInWithReadPermissions function with success', () => {
  74.                 expect(facebookFlowGen.next().value)
  75.                     .toEqual(
  76.                         call(
  77.                             [LoginManager, LoginManager.logInWithReadPermissions],
  78.                             ['public_profile', 'email']
  79.                         )
  80.                     );
  81.             });
  82.  
  83.             it('should call getCurrentAccessToken function and map AccessTokenToString with success', () => {
  84.                 const withReadPermissionResponse = {
  85.                     isCancelled: false
  86.                 };
  87.  
  88.                 const keyToStringSpy = jest.spyOn(loginSaga, 'keyToString');
  89.  
  90.                 const getAccessTokenGen = facebookFlowGen.next(withReadPermissionResponse);
  91.                 const accessTokenResultGen = facebookFlowGen.next(accessTokenResponse);
  92.  
  93.                 expect(getAccessTokenGen.done)
  94.                     .toBeFalsy();
  95.                 expect(getAccessTokenGen.value)
  96.                     .toEqual(
  97.                         call(
  98.                             [AccessToken, AccessToken.getCurrentAccessToken]
  99.                         )
  100.                     );
  101.                 expect(loginSaga.keyToString)
  102.                         .toHaveBeenCalledWith(accessTokenResponse, 'accessToken');
  103.             });
  104.  
  105.             it('should call getFacebookData function with success', () => {
  106.                 expect(facebookFlowGen.next(accessTokenResponse.accessToken).value)
  107.                     .toEqual(
  108.                         call(loginSaga.getFacebookData)
  109.                     );
  110.             });
  111.  
  112.             it('should call reducer and setToken to state', () => {
  113.                 expect(facebookFlowGen.next().value)
  114.                     .toEqual(
  115.                         put(authActionsCreators.setToken(accessTokenResponse.accessToken))
  116.                     );
  117.             });
  118.  
  119.             it('should redirect to App route after successful authorization', () => {
  120.                 navigationService.navigate = jest.fn((route) => ({ }));
  121.  
  122.                 const navigateGen = facebookFlowGen.next().value;
  123.  
  124.                 expect(navigateGen)
  125.                     .toEqual({});
  126.                 expect(navigationService.navigate)
  127.                     .toHaveBeenCalledWith('App');
  128.                 expect(facebookFlowGen.next().done)
  129.                     .toBeTruthy();
  130.             });
  131.         });
  132.     });
  133.  
  134. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement