Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { call, put } from 'redux-saga/effects';
- import * as loginSaga from './loginSaga';
- import { LoginManager, AccessToken } from 'react-native-fbsdk';
- import { AuthActions } from '../../state/auth/types';
- import { authActionsCreators } from '../../state/auth/actions';
- import navigationService from '../../services/NavigationService';
- describe('loginSaga', () => {
- describe('facebookLoginFlow', () => {
- let facebookFlowGen: IterableIterator<any>;
- describe('Error has occured durning authorization', () => {
- beforeEach(() => {
- facebookFlowGen = loginSaga.facebookLoginFlow();
- facebookFlowGen.next();
- });
- it(`should put ${AuthActions.REQUEST_FAIL} with custom error message`, () => {
- expect(facebookFlowGen.throw({}).value)
- .toEqual(
- put(authActionsCreators.requestFail('Error when loggin in to facebook.'))
- );
- });
- it(`should put ${AuthActions.REQUEST_FAIL} with given error message`, () => {
- const error = {
- message: 'Given error.'
- };
- expect(facebookFlowGen.throw(error).value)
- .toEqual(
- put(authActionsCreators.requestFail(error.message))
- );
- });
- });
- describe('User has cancelled authentication', () => {
- beforeAll(() => {
- facebookFlowGen = loginSaga.facebookLoginFlow();
- });
- it('should call logInWithReadPermissions function with success', () => {
- expect(facebookFlowGen.next().value)
- .toEqual(
- call(
- [LoginManager, LoginManager.logInWithReadPermissions],
- ['public_profile', 'email']
- )
- );
- });
- it('generator should be done, after cancellation', () => {
- const response = {
- isCancelled: true
- };
- expect(facebookFlowGen.next(response).done)
- .toBeTruthy();
- });
- });
- describe('User has been authorized', () => {
- const accessTokenResponse = {
- accessToken: 'myToken'
- };
- beforeAll(() => {
- facebookFlowGen = loginSaga.facebookLoginFlow();
- });
- it('should call logInWithReadPermissions function with success', () => {
- expect(facebookFlowGen.next().value)
- .toEqual(
- call(
- [LoginManager, LoginManager.logInWithReadPermissions],
- ['public_profile', 'email']
- )
- );
- });
- it('should call getCurrentAccessToken function and map AccessTokenToString with success', () => {
- const withReadPermissionResponse = {
- isCancelled: false
- };
- const keyToStringSpy = jest.spyOn(loginSaga, 'keyToString');
- const getAccessTokenGen = facebookFlowGen.next(withReadPermissionResponse);
- const accessTokenResultGen = facebookFlowGen.next(accessTokenResponse);
- expect(getAccessTokenGen.done)
- .toBeFalsy();
- expect(getAccessTokenGen.value)
- .toEqual(
- call(
- [AccessToken, AccessToken.getCurrentAccessToken]
- )
- );
- expect(loginSaga.keyToString)
- .toHaveBeenCalledWith(accessTokenResponse, 'accessToken');
- });
- it('should call getFacebookData function with success', () => {
- expect(facebookFlowGen.next(accessTokenResponse.accessToken).value)
- .toEqual(
- call(loginSaga.getFacebookData)
- );
- });
- it('should call reducer and setToken to state', () => {
- expect(facebookFlowGen.next().value)
- .toEqual(
- put(authActionsCreators.setToken(accessTokenResponse.accessToken))
- );
- });
- it('should redirect to App route after successful authorization', () => {
- navigationService.navigate = jest.fn((route) => ({ }));
- const navigateGen = facebookFlowGen.next().value;
- expect(navigateGen)
- .toEqual({});
- expect(navigationService.navigate)
- .toHaveBeenCalledWith('App');
- expect(facebookFlowGen.next().done)
- .toBeTruthy();
- });
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement