Skip to content

Commit

Permalink
Purchases: Retrieve the purchase updated from the API when cancelatio…
Browse files Browse the repository at this point in the history
…n of private registration is successful

This allows to update the Purchases store as soon as possible with up-to-date information (such as the correct price of the domain registration purchase) without resorting to fetching the list of updates.
  • Loading branch information
stephanethomas committed Nov 23, 2015
1 parent 47c7eb5 commit b0dece3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
5 changes: 1 addition & 4 deletions client/lib/purchases/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ const PurchasesStore = createReducerStore( ( state, payload ) => {
return assign( {}, state, { error: action.error } );

case ActionTypes.PURCHASES_PRIVATE_REGISTRATION_CANCEL_COMPLETED:
return updatePurchaseById( state, action.purchaseId, {
error: null,
hasPrivateRegistration: false
} );
return updatePurchaseById( state, action.purchase.id, action.purchase );

case ActionTypes.PURCHASES_PRIVATE_REGISTRATION_CANCEL_FAILED:
return updatePurchaseById( state, action.purchaseId, {
Expand Down
58 changes: 58 additions & 0 deletions client/lib/purchases/test/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,62 @@ describe( 'Purchases Store', () => {
done();
} );
} );

it( 'should return an object with original purchase when cancelation of private registration is triggered', () => {
Dispatcher.handleServerAction( {
type: actionTypes.PURCHASES_PRIVATE_REGISTRATION_CANCEL,
purchaseId: 2
} );

expect( PurchasesStore.getByPurchaseId( 2 ) ).to.be.eql( {
data: {
id: 2
},
error: null,
hasLoadedFromServer: true,
isFetching: false
} );
} );

it( 'should return an object with original purchase and error message when cancelation of private registration failed', () => {
Dispatcher.handleServerAction( {
type: actionTypes.PURCHASES_PRIVATE_REGISTRATION_CANCEL_FAILED,
error: 'Unable to fetch stored cards',
purchaseId: 2
} );

expect( PurchasesStore.getByPurchaseId( 2 ) ).to.be.eql( {
data: {
id: 2,
error: 'Unable to fetch stored cards'
},
error: null,
hasLoadedFromServer: true,
isFetching: false
} );
} );

it( 'should return an object with updated purchase when cancelation of private registration completed', () => {
Dispatcher.handleServerAction( {
type: actionTypes.PURCHASES_PRIVATE_REGISTRATION_CANCEL_COMPLETED,
purchase: {
amount: 2200,
error: null,
hasPrivateRegistration: false,
id: 2
}
} );

expect( PurchasesStore.getByPurchaseId( 2 ) ).to.be.eql( {
data: {
amount: 2200,
error: null,
hasPrivateRegistration: false,
id: 2
},
error: null,
hasLoadedFromServer: true,
isFetching: false
} );
} );
} );
2 changes: 1 addition & 1 deletion client/lib/upgrades/actions/purchases.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function cancelPrivateRegistration( purchaseId, onComplete ) {
if ( success ) {
Dispatcher.handleServerAction( {
type: ActionTypes.PURCHASES_PRIVATE_REGISTRATION_CANCEL_COMPLETED,
purchaseId
purchase: purchasesAssembler.createPurchaseObject( data.upgrade )
} );
} else {
Dispatcher.handleServerAction( {
Expand Down

0 comments on commit b0dece3

Please sign in to comment.