From 4f597f07230a0a7d878806cb7525dc45614b0ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Thomas?= Date: Mon, 23 Nov 2015 20:33:45 +0100 Subject: [PATCH] Purchases: Retrieve the purchase updated from the API when cancelation 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. --- client/lib/purchases/store.js | 5 +- client/lib/purchases/test/store-test.js | 58 ++++++++++++++++++++++++ client/lib/upgrades/actions/purchases.js | 2 +- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/client/lib/purchases/store.js b/client/lib/purchases/store.js index 6e42667e5e11b..884967eab60e6 100644 --- a/client/lib/purchases/store.js +++ b/client/lib/purchases/store.js @@ -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, { diff --git a/client/lib/purchases/test/store-test.js b/client/lib/purchases/test/store-test.js index 8f74d77382a70..7f719993dccbf 100644 --- a/client/lib/purchases/test/store-test.js +++ b/client/lib/purchases/test/store-test.js @@ -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 + } ); + } ); } ); diff --git a/client/lib/upgrades/actions/purchases.js b/client/lib/upgrades/actions/purchases.js index 733f6cad0ecf5..c35bec32cf674 100644 --- a/client/lib/upgrades/actions/purchases.js +++ b/client/lib/upgrades/actions/purchases.js @@ -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( {