summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/payment-request/payment-request-onshippingoptionchange-attribute.https.html
blob: 43ea5dcce87afe9dff0387891d57c3265be303f5 (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
<!DOCTYPE html>
<!-- Copyright © 2017 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<meta charset="utf-8">
<title>Test for onshippingoptionchange attribute</title>
<link rel="help" href="https://w3c.github.io/browser-payment-api/#onshippingoptionchange-attribute">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
"use strict";
const applePay = Object.freeze({
  supportedMethods: "https://apple.com/apple-pay",
  data: {
    version: 3,
    merchantIdentifier: "merchant.com.example",
    countryCode: "US",
    merchantCapabilities: ["supports3DS"],
    supportedNetworks: ["visa"],
  }
});
const basicCard = Object.freeze({ supportedMethods: "basic-card" });
const defaultMethods = Object.freeze([basicCard, applePay]);
const defaultDetails = Object.freeze({
  total: {
    label: "Total",
    amount: {
      currency: "USD",
      value: "1.00",
    },
  },
});

test(() => {
  const request = new PaymentRequest(defaultMethods, defaultDetails);
  assert_idl_attribute(request, "onshippingoptionchange");
}, "Must have a onshippingoptionchange IDL attribute");

test(() => {
  const request = new PaymentRequest(defaultMethods, defaultDetails);
  const ev = new Event("shippingoptionchange");
  let didHandle = false;
  request.onshippingoptionchange = evt => {
    assert_equals(ev, evt, "must be same event");
    didHandle = true;
  };
  request.dispatchEvent(ev);
  assert_true(didHandle, "event did not fire");
}, `onshippingoptionchange attribute is a generic handler for "shippingoptionchange"`);

test(() => {
  const request = new PaymentRequest(defaultMethods, defaultDetails);
  const ev = new PaymentRequestUpdateEvent("shippingoptionchange");
  let didHandle = false;
  request.onshippingoptionchange = evt => {
    assert_equals(ev, evt, "must be same event");
    didHandle = true;
  };
  request.dispatchEvent(ev);
  assert_true(didHandle, "event did not fire");
}, `onshippingoptionchange attribute is a handler for PaymentRequestUpdateEvent`);

test(() => {
  const request = new PaymentRequest(defaultMethods, defaultDetails);
  const ev = new PaymentRequestUpdateEvent("shippingoptionchange");
  let didHandle = false;
  let didListen = false;
  request.onshippingoptionchange = evt => {
    assert_equals(ev, evt, "must be same event");
    didHandle = true;
  };
  request.addEventListener("shippingoptionchange", evt => {
    assert_equals(ev, evt, "must be same event");
    didListen = true;
  });
  request.dispatchEvent(ev);
  assert_true(didHandle, "onshippingoptionchange did not receive the event");
  assert_true(didListen, "addEventListener did not receive the event");
}, `onshippingoptionchange attribute and listeners both work`);

</script>