Bug 1429180 - Change selectedStateKey to an array that defines the path within the state object that should be updated. r=MattN
authorJared Wein <jwein@mozilla.com>
Mon, 21 May 2018 22:05:47 -0400
changeset 419311 88f302c4a1eb30e05f6de319bb755a94f6e0343d
parent 419310 02cc06146058af4f5332d288fd169edd1f2c77a3
child 419312 cbb0b27b6e59f4ef16376d964c4598b87fa96e6d
push id34036
push userdluca@mozilla.com
push dateWed, 23 May 2018 09:50:27 +0000
treeherdermozilla-central@bdb0b4d7712d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN
bugs1429180
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1429180 - Change selectedStateKey to an array that defines the path within the state object that should be updated. r=MattN MozReview-Commit-ID: IjJfmUtvjdz
browser/components/payments/content/paymentDialogWrapper.js
browser/components/payments/res/containers/address-form.js
browser/components/payments/res/containers/basic-card-form.js
browser/components/payments/test/browser/browser_card_edit.js
browser/components/payments/test/mochitest/test_address_form.html
browser/components/payments/test/mochitest/test_basic_card_form.html
--- a/browser/components/payments/content/paymentDialogWrapper.js
+++ b/browser/components/payments/content/paymentDialogWrapper.js
@@ -572,26 +572,32 @@ var paymentDialogWrapper = {
         // so add updated collection here
         Object.assign(successStateChange, {
           tempBasicCards: this.temporaryStore.creditCards.getAll(),
         });
       }
 
       // Select the new record
       if (selectedStateKey) {
-        Object.assign(successStateChange, {
-          [selectedStateKey]: guid,
-        });
+        if (selectedStateKey.length == 1) {
+          Object.assign(successStateChange, {
+            [selectedStateKey[0]]: guid,
+          });
+        } else if (selectedStateKey.length == 2) {
+          // Need to keep properties like preserveFieldValues from getting removed.
+          let subObj = Object.assign({}, successStateChange[selectedStateKey[0]]);
+          subObj[selectedStateKey[1]] = guid;
+          Object.assign(successStateChange, {
+            [selectedStateKey[0]]: subObj,
+          });
+        } else {
+          throw new Error(`selectedStateKey not supported: '${selectedStateKey}'`);
+        }
       }
 
-      const pageId = collectionName == "creditCards" ?
-                                   "basic-card-page" :
-                                   "address-page";
-      successStateChange[pageId].guid = guid;
-
       this.sendMessageToContent("updateState", successStateChange);
     } catch (ex) {
       this.sendMessageToContent("updateState", errorStateChange);
     }
   },
 
   /**
    * @implements {nsIObserver}
--- a/browser/components/payments/res/containers/address-form.js
+++ b/browser/components/payments/res/containers/address-form.js
@@ -169,23 +169,23 @@ export default class AddressForm extends
       default: {
         throw new Error("Unexpected click target");
       }
     }
   }
 
   saveRecord() {
     let record = this.formHandler.buildFormObject();
+    let currentState = this.requestStore.getState();
     let {
       page,
       tempAddresses,
       savedBasicCards,
       "address-page": addressPage,
-      "basic-card-page": basicCardPage,
-    } = this.requestStore.getState();
+    } = currentState;
     let editing = !!addressPage.guid;
 
     if (editing ? (addressPage.guid in tempAddresses) : !this.persistCheckbox.checked) {
       record.isTemporary = true;
     }
 
     let state = {
       errorStateChange: {
@@ -195,39 +195,36 @@ export default class AddressForm extends
           error: this.dataset.errorGenericSave,
         },
         "address-page": addressPage,
       },
       preserveOldProperties: true,
       selectedStateKey: page.selectedStateKey,
     };
 
+    const previousId = page.previousId;
     if (page.onboardingWizard && !Object.keys(savedBasicCards).length) {
       state.successStateChange = {
         page: {
           id: "basic-card-page",
           previousId: "address-page",
           onboardingWizard: page.onboardingWizard,
         },
       };
     } else {
       state.successStateChange = {
         page: {
-          id: page.previousId || "payment-summary",
+          id: previousId || "payment-summary",
           onboardingWizard: page.onboardingWizard,
         },
       };
     }
 
-    state.successStateChange["address-page"] = addressPage;
-    state.successStateChange["basic-card-page"] = basicCardPage;
-
-    const previousId = page.previousId;
     if (previousId) {
+      state.successStateChange[previousId] = Object.assign({}, currentState[previousId]);
       state.successStateChange[previousId].preserveFieldValues = true;
-      state.successStateChange[previousId].addressesModified = true;
     }
 
     paymentRequest.updateAutofillRecord("addresses", record, addressPage.guid, state);
   }
 }
 
 customElements.define("address-form", AddressForm);
--- a/browser/components/payments/res/containers/basic-card-form.js
+++ b/browser/components/payments/res/containers/basic-card-form.js
@@ -150,18 +150,18 @@ export default class BasicCardForm exten
       this.persistCheckbox.hidden = false;
       this.persistCheckbox.checked = !state.isPrivate;
     }
 
     this.formHandler.loadRecord(record, addresses, basicCardPage.preserveFieldValues);
 
     this.form.querySelector(".billingAddressRow").hidden = false;
 
-    if (basicCardPage.addressesModified) {
-      let addressGuid = state["address-page"].guid;
+    if (basicCardPage.billingAddressGUID) {
+      let addressGuid = basicCardPage.billingAddressGUID;
       let billingAddressSelect = this.form.querySelector("#billingAddressGUID");
       billingAddressSelect.value = addressGuid;
     }
   }
 
   handleEvent(event) {
     switch (event.type) {
       case "click": {
@@ -181,16 +181,17 @@ export default class BasicCardForm exten
       case this.addressEditLink: {
         let {
           "basic-card-page": basicCardPage,
         } = this.requestStore.getState();
         let nextState = {
           page: {
             id: "address-page",
             previousId: "basic-card-page",
+            selectedStateKey: ["basic-card-page", "billingAddressGUID"],
           },
           "address-page": {
             guid: null,
             title: this.dataset.billingAddressTitleAdd,
           },
           "basic-card-page": {
             preserveFieldValues: true,
             guid: basicCardPage.guid,
@@ -251,17 +252,17 @@ export default class BasicCardForm exten
     let state = {
       errorStateChange: {
         page: {
           id: "basic-card-page",
           error: this.dataset.errorGenericSave,
         },
       },
       preserveOldProperties: true,
-      selectedStateKey: "selectedPaymentCard",
+      selectedStateKey: ["selectedPaymentCard"],
       successStateChange: {
         page: {
           id: "payment-summary",
         },
       },
     };
 
     const previousId = page.previousId;
--- a/browser/components/payments/test/browser/browser_card_edit.js
+++ b/browser/components/payments/test/browser/browser_card_edit.js
@@ -102,24 +102,35 @@ add_task(async function test_add_link() 
     }
 
     content.document.querySelector("address-form button:last-of-type").click();
     state = await PTU.DialogContentUtils.waitForState(content, (state) => {
       return state.page.id == "basic-card-page" && !state["basic-card-page"].guid &&
              Object.keys(state.savedAddresses).length == 1;
     }, "Check address was added and we're back on basic-card page (add)");
 
+    ok(state["basic-card-page"].preserveFieldValues,
+       "preserveFieldValues should be set when coming back from address-page");
+
+    ok(state["basic-card-page"].billingAddressGUID,
+       "billingAddressGUID should be set when coming back from address-page");
+
     is(billingAddressSelect.childElementCount, 2,
        "Two options should exist in the billingAddressSelect");
     let selectedOption =
       billingAddressSelect.children[billingAddressSelect.selectedIndex];
     let selectedAddressGuid = selectedOption.value;
     is(selectedAddressGuid, Object.values(state.savedAddresses)[0].guid,
        "The select should have the new address selected");
 
+    for (let [key, val] of Object.entries(card)) {
+      let field = content.document.getElementById(key);
+      is(field.value, val, `Field #${key} should have value`);
+    }
+
     content.document.querySelector("basic-card-form button:last-of-type").click();
 
     state = await PTU.DialogContentUtils.waitForState(content, (state) => {
       return Object.keys(state.savedBasicCards).length == 1;
     }, "Check card was not added again");
 
     let cardGUIDs = Object.keys(state.savedBasicCards);
     is(cardGUIDs.length, 1, "Check there is one card");
--- a/browser/components/payments/test/mochitest/test_address_form.html
+++ b/browser/components/payments/test/mochitest/test_address_form.html
@@ -160,22 +160,16 @@ add_task(async function test_saveButton(
       "tel": "+15555551212",
     },
     selectedStateKey: undefined,
     successStateChange: {
       page: {
         id: "payment-summary",
         onboardingWizard: undefined,
       },
-      "address-page": {
-        title: "Sample page title",
-      },
-      "basic-card-page": {
-        guid: null,
-      },
     },
   }, "Check event details for the message to chrome");
   form.remove();
 });
 
 add_task(async function test_genericError() {
   let form = new AddressForm();
   await form.requestStore.setState({
--- a/browser/components/payments/test/mochitest/test_basic_card_form.html
+++ b/browser/components/payments/test/mochitest/test_basic_card_form.html
@@ -125,17 +125,17 @@ add_task(async function test_saveButton(
     messageType: "updateAutofillRecord",
     preserveOldProperties: true,
     record: {
       "cc-exp-month": "11",
       "cc-exp-year": year,
       "cc-name": "J. Smith",
       "cc-number": "4111111111111111",
     },
-    selectedStateKey: "selectedPaymentCard",
+    selectedStateKey: ["selectedPaymentCard"],
     successStateChange: {
       page: {
         id: "payment-summary",
       },
     },
   }, "Check event details for the message to chrome");
   form.remove();
 });