summaryrefslogtreecommitdiffstats
path: root/browser/components/payments/test/unit/test_response_creation.js
blob: 2b4b5dc98b3d72ceb1adea0fa16a222fec20cc6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"use strict";

/**
 * Basic checks to ensure that helpers constructing responses map their
 * destructured arguments properly to the `init` methods. Full testing of the init
 * methods is left to the DOM code.
 */

const DIALOG_WRAPPER_URI = "chrome://payments/content/paymentDialogWrapper.js";
let dialogGlobal = {};
Services.scriptloader.loadSubScript(DIALOG_WRAPPER_URI, dialogGlobal);

add_task(async function test_createBasicCardResponseData_basic() {
  let expected = {
    cardholderName: "John Smith",
    cardNumber: "1234567890",
    expiryMonth: "01",
    expiryYear: "2017",
    cardSecurityCode: "0123",
  };
  let actual = dialogGlobal.paymentDialogWrapper.createBasicCardResponseData(
    expected
  );
  Assert.equal(
    actual.cardholderName,
    expected.cardholderName,
    "Check cardholderName"
  );
  Assert.equal(actual.cardNumber, expected.cardNumber, "Check cardNumber");
  Assert.equal(actual.expiryMonth, expected.expiryMonth, "Check expiryMonth");
  Assert.equal(actual.expiryYear, expected.expiryYear, "Check expiryYear");
  Assert.equal(
    actual.cardSecurityCode,
    expected.cardSecurityCode,
    "Check cardSecurityCode"
  );
});

add_task(async function test_createBasicCardResponseData_minimal() {
  let expected = {
    cardNumber: "1234567890",
  };
  let actual = dialogGlobal.paymentDialogWrapper.createBasicCardResponseData(
    expected
  );
  info(actual.cardNumber);
  Assert.equal(actual.cardNumber, expected.cardNumber, "Check cardNumber");
});

add_task(async function test_createBasicCardResponseData_withoutNumber() {
  let data = {
    cardholderName: "John Smith",
    expiryMonth: "01",
    expiryYear: "2017",
    cardSecurityCode: "0123",
  };
  Assert.throws(
    () => dialogGlobal.paymentDialogWrapper.createBasicCardResponseData(data),
    /NS_ERROR_FAILURE/,
    "Check cardNumber is required"
  );
});

function checkAddress(actual, expected) {
  for (let [propName, propVal] of Object.entries(expected)) {
    if (propName == "addressLines") {
      // Note the singular vs. plural here.
      Assert.equal(
        actual.addressLine.length,
        propVal.length,
        "Check number of address lines"
      );
      for (let [i, line] of expected.addressLines.entries()) {
        Assert.equal(
          actual.addressLine.queryElementAt(i, Ci.nsISupportsString).data,
          line,
          `Check ${propName} line ${i}`
        );
      }
      continue;
    }
    Assert.equal(actual[propName], propVal, `Check ${propName}`);
  }
}

add_task(async function test_createPaymentAddress_minimal() {
  let data = {
    country: "CA",
  };
  let actual = dialogGlobal.paymentDialogWrapper.createPaymentAddress(data);
  checkAddress(actual, data);
});

add_task(async function test_createPaymentAddress_basic() {
  let data = {
    country: "CA",
    addressLines: ["123 Sesame Street", "P.O. Box ABC"],
    region: "ON",
    city: "Delhi",
    dependentLocality: "N/A",
    postalCode: "94041",
    sortingCode: "1234",
    organization: "Mozilla Corporation",
    recipient: "John Smith",
    phone: "+15195555555",
  };
  let actual = dialogGlobal.paymentDialogWrapper.createPaymentAddress(data);
  checkAddress(actual, data);
});

add_task(async function test_createShowResponse_basic() {
  let requestId = "876hmbvfd45hb";
  dialogGlobal.paymentDialogWrapper.request = {
    requestId,
  };

  let cardData = {
    cardholderName: "John Smith",
    cardNumber: "1234567890",
    expiryMonth: "01",
    expiryYear: "2099",
    cardSecurityCode: "0123",
  };
  let methodData = dialogGlobal.paymentDialogWrapper.createBasicCardResponseData(
    cardData
  );

  let responseData = {
    acceptStatus: Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
    methodName: "basic-card",
    methodData,
    payerName: "My Name",
    payerEmail: "my.email@example.com",
    payerPhone: "+15195555555",
  };
  let actual = dialogGlobal.paymentDialogWrapper.createShowResponse(
    responseData
  );
  for (let [propName, propVal] of Object.entries(actual)) {
    if (typeof propVal != "string") {
      continue;
    }
    if (propName == "requestId") {
      Assert.equal(propVal, requestId, `Check ${propName}`);
      continue;
    }
    Assert.equal(propVal, responseData[propName], `Check ${propName}`);
  }
});