tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

change-shipping-option-manual.https.html (5032B)


      1 <!DOCTYPE html>
      2 <meta charset="utf-8" />
      3 <title>Tests for PaymentRequestEvent.changeShippingOption()</title>
      4 <link
      5  rel="help"
      6  href="https://w3c.github.io/payment-handler/#changeshippingoption-method"
      7 />
      8 
      9 <script src="/resources/testharness.js"></script>
     10 <script src="/resources/testharnessreport.js"></script>
     11 <script src="/resources/testdriver.js"></script>
     12 <script src="/resources/testdriver-vendor.js"></script>
     13 <p>If the payment sheet is shown, please authorize the mock payment.</p>
     14 <script>
     15 const methodName = window.location.origin
     16    + '/payment-handler/change-shipping-option-manual-manifest.json';
     17 function createRequest() {
     18  return new PaymentRequest([{supportedMethods: methodName}], {
     19      total: {label: 'Total', amount: {currency: 'USD', value: '0.01'}},
     20      shippingOptions: [{
     21        id: 'freeShippingOption',
     22        label: 'Free global shipping',
     23        amount: {
     24          currency: 'USD',
     25          value: '0',
     26        },
     27        selected: false,
     28      },
     29      {
     30        id: 'expressShippingOption',
     31        label: 'express global shipping',
     32        amount: {
     33          currency: 'USD',
     34          value: '0',
     35        },
     36        selected: true,
     37      }],
     38    }, {requestShipping: true});
     39 }
     40 
     41 promise_test(async (t) => {
     42  const request = createRequest();
     43  // Intentionally do not respond to the 'shippingoptionchange' event.
     44  const response = await test_driver.bless('showing a payment sheet', () =>
     45    request.show()
     46  );
     47  const complete_promise = response.complete('success');
     48 
     49  assert_equals(response.details.changeShippingOptionReturned, null);
     50 
     51  return complete_promise;
     52 }, 'If updateWith(details) is not run, changeShippingOption() returns null.');
     53 
     54 promise_test(async (t) => {
     55  const request = createRequest();
     56  request.addEventListener('shippingoptionchange', (event) => {
     57    assert_equals(request.shippingOption, 'freeShippingOption');
     58    event.updateWith({
     59      total: {label: 'Total', amount: {currency: 'GBP', value: '0.02'}},
     60      error: 'Error for test',
     61      modifiers: [
     62        {
     63          supportedMethods: methodName,
     64          data: {soup: 'potato'},
     65          total: {
     66            label: 'Modified total',
     67            amount: {currency: 'EUR', value: '0.03'},
     68          },
     69          additionalDisplayItems: [
     70            {
     71              label: 'Modified display item',
     72              amount: {currency: 'INR', value: '0.06'},
     73            },
     74          ],
     75        },
     76        {
     77          supportedMethods: methodName + '2',
     78          data: {soup: 'tomato'},
     79          total: {
     80            label: 'Modified total #2',
     81            amount: {currency: 'CHF', value: '0.07'},
     82          },
     83          additionalDisplayItems: [
     84            {
     85              label: 'Modified display item #2',
     86              amount: {currency: 'CAD', value: '0.08'},
     87            },
     88          ],
     89        },
     90      ],
     91      displayItems: [
     92        {
     93          label: 'Display item',
     94          amount: {currency: 'CNY', value: '0.04'},
     95        },
     96      ],
     97      shippingOptions: [
     98        {
     99          id: 'freeShippingOption',
    100          label: 'express global shipping',
    101          amount: {
    102            currency: 'USD',
    103            value: '0',
    104          },
    105          selected: true,
    106        }
    107      ],
    108    });
    109  });
    110  const response = await test_driver.bless('showing a payment sheet', () =>
    111    request.show()
    112  );
    113  const complete_promise = response.complete('success');
    114  const changeShippingOptionReturned =
    115    response.details.changeShippingOptionReturned;
    116 
    117  assert_equals(changeShippingOptionReturned.total.currency, 'GBP');
    118  assert_equals(changeShippingOptionReturned.total.value, '0.02');
    119  assert_equals(changeShippingOptionReturned.total.label, undefined);
    120  assert_equals(changeShippingOptionReturned.error, 'Error for test');
    121  assert_equals(changeShippingOptionReturned.modifiers.length, 1);
    122  assert_equals(changeShippingOptionReturned.displayItems, undefined);
    123  assert_equals(changeShippingOptionReturned.shippingOptions.length, 1);
    124  assert_equals(changeShippingOptionReturned.paymentMethodErrors, undefined);
    125  assert_equals(changeShippingOptionReturned.shippingAddressErrors, undefined);
    126 
    127  const shipping_option = changeShippingOptionReturned.shippingOptions[0];
    128  assert_equals(shipping_option.id, 'freeShippingOption' );
    129  assert_equals(shipping_option.label, 'express global shipping');
    130  assert_equals(shipping_option.amount.currency, 'USD');
    131  assert_equals(shipping_option.amount.value, '0');
    132  assert_true(shipping_option.selected);
    133 
    134  const modifier = changeShippingOptionReturned.modifiers[0];
    135  assert_equals(modifier.supportedMethods, methodName);
    136  assert_equals(modifier.data.soup, 'potato');
    137  assert_equals(modifier.total.label, '');
    138  assert_equals(modifier.total.amount.currency, 'EUR');
    139  assert_equals(modifier.total.amount.value, '0.03');
    140  assert_equals(modifier.additionalDisplayItems, undefined);
    141 
    142  return complete_promise;
    143 }, 'The changeShippingOption() returns some details from the "shippingoptionchange" event\'s updateWith(details) call.');
    144 </script>