updateWith-state-checks-manual.https.html (4232B)
1 <!doctype html> 2 <meta charset="utf8"> 3 <link rel="help" href="https://www.w3.org/TR/payment-request/#updatewith()-method"> 4 <title>updateWith() method - state machine checks</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script> 8 setup({ explicit_done: true, explicit_timeout: true }); 9 const applePay = Object.freeze({ 10 supportedMethods: "https://apple.com/apple-pay", 11 data: { 12 version: 3, 13 merchantIdentifier: "merchant.com.example", 14 countryCode: "US", 15 merchantCapabilities: ["supports3DS"], 16 supportedNetworks: ["visa"], 17 } 18 }); 19 const validMethod = Object.freeze({ supportedMethods: "basic-card" }); 20 const validMethods = Object.freeze([validMethod, applePay]); 21 const validAmount = Object.freeze({ currency: "USD", value: "5.00" }); 22 const validTotal = Object.freeze({ 23 label: "label", 24 amount: validAmount, 25 }); 26 const validShippingOption = Object.freeze({ 27 id: "a-shipping-option", 28 label: "A shipping option", 29 amount: validAmount, 30 selected: true, 31 }); 32 const validDetails = Object.freeze({ 33 total: validTotal, 34 shippingOptions: [validShippingOption], 35 }); 36 const validOptions = Object.freeze({ 37 requestShipping: true, 38 }); 39 40 function getPaymentPromises() { 41 const request = new PaymentRequest(validMethods, validDetails, validOptions); 42 const eventPromise = new Promise(resolve => { 43 request.addEventListener("shippingaddresschange", resolve); 44 }); 45 const responsePromise = request.show(); 46 return { eventPromise, responsePromise }; 47 } 48 49 function testRequestIsClosed(button) { 50 button.disabled = "true"; 51 promise_test(async t => { 52 const { eventPromise, responsePromise } = getPaymentPromises(); 53 const event = await eventPromise; 54 // We are going to abort the responsePromise, so we can ignore error. 55 responsePromise.catch(err => err); 56 // Set request.[[state]] to closed 57 await event.target.abort(); 58 assert_throws_dom( 59 "InvalidStateError", 60 () => { 61 event.updateWith(validDetails); 62 }, 63 "request.[[state]] is not interactive, must throw an InvalidStateError." 64 ); 65 responsePromise.catch(err => err); 66 }, button.textContent.trim()); 67 } 68 69 function testRequestIsUpdating(button) { 70 button.disabled = "true"; 71 promise_test(async t => { 72 const { eventPromise, responsePromise } = getPaymentPromises(); 73 const event = await eventPromise; 74 // We are going to put a promise into a pending state 75 // check that a second call to updateWith() throws, 76 // then resolve the pending promise below. 77 let resolver; 78 const pendingPromise = new Promise(resolve => { 79 resolver = resolve; 80 }); 81 // Set request.[[updating]] to true 82 event.updateWith(pendingPromise); 83 assert_throws_dom( 84 "InvalidStateError", 85 () => { 86 event.updateWith(validDetails); 87 }, 88 "request.[[updating]] to true, must throw an InvalidStateError." 89 ); 90 // We got the error we wanted, so let's resolve with valid details. 91 resolver(validDetails); 92 await pendingPromise; 93 await event.target.abort(); 94 responsePromise.catch(err => err); 95 }, button.textContent.trim()); 96 } 97 98 </script> 99 <h2>updateWith() method - state machine checks</h2> 100 <p> 101 Click on each button in sequence from top to bottom without refreshing the page. 102 Each button will bring up the Payment Request UI window. 103 </p> 104 <p> 105 When the payment sheet is shown, select a different shipping address once. Then pay. 106 </p> 107 <ol> 108 <li id="test-0"> 109 <button onclick="testRequestIsClosed(this);"> 110 When updateWith() is called, if request.[[state]] is not "interactive", then throw an " InvalidStateError" DOMException. 111 </button> 112 </li> 113 <li id="test-1"> 114 <button onclick="testRequestIsUpdating(this);"> 115 When updateWith() is called, If request.[[updating]] is true, then throw an "InvalidStateError" DOMException. 116 </button> 117 </li> 118 <li> 119 <button onclick="done();">Done!</button> 120 </li> 121 </ol> 122 <small> 123 If you find a buggy test, please <a href="https://github.com/web-platform-tests/wpt/issues">file a bug</a> 124 and tag one of the <a href="https://github.com/web-platform-tests/wpt/blob/master/payment-request/META.yml">suggested reviewers</a>. 125 </small>