Skip to content

Commit

Permalink
Update account-recovery lookup to use wpcom-http
Browse files Browse the repository at this point in the history
Refactoring patch that doesn't change any behavior observable to the user.
  • Loading branch information
jsnajdr committed Sep 15, 2017
1 parent eca55d0 commit af39508
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 37 deletions.
48 changes: 36 additions & 12 deletions client/state/data-layer/wpcom/account-recovery/lookup/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @format */
/**
* External dependencies
*/
Expand All @@ -6,15 +7,17 @@ import { isString, tap } from 'lodash';
/**
* Internal dependencies
*/
import wpcom from 'lib/wp';
import { http } from 'state/data-layer/wpcom-http/actions';
import { dispatchRequest } from 'state/data-layer/wpcom-http/utils';
import { noRetry } from 'state/data-layer/wpcom-http/pipeline/retry-on-failure/policies';
import { ACCOUNT_RECOVERY_RESET_OPTIONS_REQUEST } from 'state/action-types';
import {
fetchResetOptionsSuccess,
fetchResetOptionsError,
updatePasswordResetUserData,
} from 'state/account-recovery/reset/actions';

export const fromApi = data => ( [
export const fromApi = data => [
{
email: data.primary_email,
sms: data.primary_sms,
Expand All @@ -25,28 +28,49 @@ export const fromApi = data => ( [
sms: data.secondary_sms,
name: 'secondary',
},
] );
];

export const validate = ( { primary_email, primary_sms, secondary_email, secondary_sms } ) => {
if ( ! [ primary_email, primary_sms, secondary_email, secondary_sms ].every( isString ) ) {
throw Error( 'Unexpected response format from /account-recovery/lookup' );
}
};

export const handleRequestResetOptions = ( { dispatch }, action ) => {
export const requestResetOptions = ( { dispatch }, action ) => {
const { userData } = action;

wpcom.req.get( {
body: userData,
apiNamespace: 'wpcom/v2',
path: '/account-recovery/lookup',
} ).then( data => {
dispatch(
http(
{
method: 'GET',
path: '/account-recovery/lookup',
apiNamespace: 'wpcom/v2',
query: userData,
retryPolicy: noRetry(),
},
action
)
);
};

export const requestResetOptionsError = ( { dispatch }, action, error ) => {
dispatch( fetchResetOptionsError( error ) );
};

export const requestResetOptionsSuccess = ( store, action, data ) => {
const { dispatch } = store;
const { userData } = action;

try {
dispatch( fetchResetOptionsSuccess( fromApi( tap( data, validate ) ) ) );
dispatch( updatePasswordResetUserData( userData ) );
} )
.catch( error => dispatch( fetchResetOptionsError( error ) ) );
} catch ( error ) {
requestResetOptionsError( store, action, error );
}
};

export default {
[ ACCOUNT_RECOVERY_RESET_OPTIONS_REQUEST ]: [ handleRequestResetOptions ],
[ ACCOUNT_RECOVERY_RESET_OPTIONS_REQUEST ]: [
dispatchRequest( requestResetOptions, requestResetOptionsSuccess, requestResetOptionsError ),
],
};
47 changes: 22 additions & 25 deletions client/state/data-layer/wpcom/account-recovery/lookup/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
import { assert } from 'chai';
import sinon from 'sinon';

/**
* Internal dependencies
*/
import useNock from 'test/helpers/use-nock';

import {
fromApi,
validate,
handleRequestResetOptions,
requestResetOptionsSuccess,
requestResetOptionsError
} from '../';

import {
Expand Down Expand Up @@ -50,21 +46,11 @@ describe( 'validate()', () => {
} );

describe( 'handleRequestResetOptions()', () => {
const apiBaseUrl = 'https://public-api.wordpress.com:443';
const endpoint = '/wpcom/v2/account-recovery/lookup';

const userData = {
user: 'foo',
};

describe( 'success', () => {
useNock( nock => (
nock( apiBaseUrl )
.persist()
.get( endpoint )
.reply( 200, validResponse )
) );

it( 'should dispatch RECEIVE action on success', ( done ) => {
const dispatch = sinon.spy( ( action ) => {
if ( action.type === ACCOUNT_RECOVERY_RESET_OPTIONS_RECEIVE ) {
Expand All @@ -77,7 +63,7 @@ describe( 'handleRequestResetOptions()', () => {
}
} );

handleRequestResetOptions( { dispatch }, { userData } );
requestResetOptionsSuccess( { dispatch }, { userData }, validResponse );
} );

it( 'should dispatch UPDATE_USER_DATA action on success', ( done ) => {
Expand All @@ -92,7 +78,7 @@ describe( 'handleRequestResetOptions()', () => {
}
} );

handleRequestResetOptions( { dispatch }, { userData } );
requestResetOptionsSuccess( { dispatch }, { userData }, validResponse );
} );
} );

Expand All @@ -102,12 +88,6 @@ describe( 'handleRequestResetOptions()', () => {
message: 'Something wrong!',
};

useNock( nock => (
nock( apiBaseUrl )
.get( endpoint )
.reply( errorResponse.status, errorResponse )
) );

it( 'should dispatch ERROR action on failure', ( done ) => {
const dispatch = sinon.spy( () => {
assert.isTrue( dispatch.calledWithMatch( {
Expand All @@ -118,7 +98,24 @@ describe( 'handleRequestResetOptions()', () => {
done();
} );

handleRequestResetOptions( { dispatch }, { userData } );
requestResetOptionsError( { dispatch }, { userData }, errorResponse );
} );

it( 'should dispatch ERROR action on validation failure', ( done ) => {
const invalidResponse = {
primary_email: '[email protected]',
};

const dispatch = sinon.spy( () => {
assert.isTrue( dispatch.calledWithMatch( {
type: ACCOUNT_RECOVERY_RESET_OPTIONS_ERROR,
error: { message: 'Unexpected response format from /account-recovery/lookup' }
} ) );

done();
} );

requestResetOptionsSuccess( { dispatch }, { userData }, invalidResponse );
} );
} );
} );

0 comments on commit af39508

Please sign in to comment.