summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/payment-request/payment-response/helpers.js
blob: 3e4f5cfd36f898c116717c1151806f966511a945 (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
150
setup({ explicit_done: true, explicit_timeout: true });

const applePay = Object.freeze({
  supportedMethods: "https://apple.com/apple-pay",
  data: {
    version: 3,
    merchantIdentifier: "merchant.com.example",
    countryCode: "US",
    merchantCapabilities: ["supports3DS"],
    supportedNetworks: ["visa"],
  }
});

const validMethod = Object.freeze({
  supportedMethods: "basic-card",
});

const validMethods = Object.freeze([validMethod, applePay]);

const validAmount = Object.freeze({
  currency: "USD",
  value: "1.00",
});

const validTotal = Object.freeze({
  label: "Valid total",
  amount: validAmount,
});
const validDetails = {
  total: validTotal,
};

test(() => {
  try {
    new PaymentRequest(validMethods, validDetails);
  } catch (err) {
    done();
    throw err;
  }
}, "Can construct a payment request (smoke test).");

/**
 * Pops up a payment sheet, allowing options to be
 * passed in if particular values are needed.
 *
 * @param PaymentOptions options
 */
async function getPaymentResponse(options, id) {
  const { response } = await getPaymentRequestResponse(options, id);
  return response;
}

/**
 * Creates a payment request and response pair.
 * It also shows the payment sheet.
 *
 * @param {PaymentOptions?} options
 * @param {String?} id
 */
async function getPaymentRequestResponse(options, id) {
  const methods = [{ supportedMethods: "basic-card" }];
  const details = {
    id,
    total: {
      label: "Total due",
      amount: { currency: "USD", value: "1.0" },
    },
    shippingOptions: [
      {
        id: "fail1",
        label: "Fail option 1",
        amount: { currency: "USD", value: "5.00" },
        selected: false,
      },
      {
        id: "pass",
        label: "Pass option",
        amount: { currency: "USD", value: "5.00" },
        selected: true,
      },
      {
        id: "fail2",
        label: "Fail option 2",
        amount: { currency: "USD", value: "5.00" },
        selected: false,
      },
    ],
  };
  const request = new PaymentRequest(methods, details, options);
  request.onshippingaddresschange = ev => ev.updateWith(details);
  request.onshippingoptionchange = ev => ev.updateWith(details);
  const response = await request.show();
  return { request, response };
}

/**
 * Runs a manual test for payment
 *
 * @param {HTMLButtonElement} button The HTML button.
 * @param {PaymentOptions?} options.
 * @param {Object} expected What property values are expected to pass the test.
 * @param {String?} id And id for the request/response pair.
 */
async function runManualTest(button, options, expected = {}, id = undefined) {
  button.disabled = true;
  const { request, response } = await getPaymentRequestResponse(options, id);
  await response.complete();
  test(() => {
    assert_idl_attribute(
      response,
      "requestId",
      "Expected requestId to be an IDL attribute."
    );
    assert_equals(response.requestId, request.id, `Expected ids to match`);
    for (const [attribute, value] of Object.entries(expected)) {
      assert_idl_attribute(
        response,
        attribute,
        `Expected ${attribute} to be an IDL attribute.`
      );
      assert_equals(
        response[attribute],
        value,
        `Expected response ${attribute} attribute to be ${value}`
      );
    }
    assert_idl_attribute(response, "details");
    assert_equals(typeof response.details, "object", "Expected an object");
    // Testing that this does not throw:
    response.toJSON();
    if (options && options.requestShipping) {
      assert_equals(
        response.shippingOption,
        "pass",
        "request.shippingOption must be 'pass'"
      );
    } else {
      assert_equals(
        request.shippingOption,
        null,
        "If requestShipping is falsy, request.shippingOption must be null"
      );
      assert_equals(
        response.shippingOption,
        null,
        "request.shippingOption must be null"
      );
    }
  }, button.textContent.trim());
}