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