helpers.js (3989B)
1 setup({ explicit_done: true, explicit_timeout: true }); 2 3 const applePay = Object.freeze({ 4 supportedMethods: "https://apple.com/apple-pay", 5 data: { 6 version: 3, 7 merchantIdentifier: "merchant.com.example", 8 countryCode: "US", 9 merchantCapabilities: ["supports3DS"], 10 supportedNetworks: ["visa"], 11 } 12 }); 13 14 const validMethod = Object.freeze({ 15 supportedMethods: "basic-card", 16 }); 17 18 const validMethods = Object.freeze([validMethod, applePay]); 19 20 const validAmount = Object.freeze({ 21 currency: "USD", 22 value: "1.00", 23 }); 24 25 const validTotal = Object.freeze({ 26 label: "Valid total", 27 amount: validAmount, 28 }); 29 const validDetails = { 30 total: validTotal, 31 }; 32 33 test(() => { 34 try { 35 new PaymentRequest(validMethods, validDetails); 36 } catch (err) { 37 done(); 38 throw err; 39 } 40 }, "Can construct a payment request (smoke test)."); 41 42 /** 43 * Pops up a payment sheet, allowing options to be 44 * passed in if particular values are needed. 45 * 46 * @param PaymentOptions options 47 */ 48 async function getPaymentResponse(options, id) { 49 const { response } = await getPaymentRequestResponse(options, id); 50 return response; 51 } 52 53 /** 54 * Creates a payment request and response pair. 55 * It also shows the payment sheet. 56 * 57 * @param {PaymentOptions?} options 58 * @param {String?} id 59 */ 60 async function getPaymentRequestResponse(options, id) { 61 const methods = [{ supportedMethods: "basic-card" }]; 62 const details = { 63 id, 64 total: { 65 label: "Total due", 66 amount: { currency: "USD", value: "1.0" }, 67 }, 68 shippingOptions: [ 69 { 70 id: "fail1", 71 label: "Fail option 1", 72 amount: { currency: "USD", value: "5.00" }, 73 selected: false, 74 }, 75 { 76 id: "pass", 77 label: "Pass option", 78 amount: { currency: "USD", value: "5.00" }, 79 selected: true, 80 }, 81 { 82 id: "fail2", 83 label: "Fail option 2", 84 amount: { currency: "USD", value: "5.00" }, 85 selected: false, 86 }, 87 ], 88 }; 89 const request = new PaymentRequest(methods, details, options); 90 request.onshippingaddresschange = ev => ev.updateWith(details); 91 request.onshippingoptionchange = ev => ev.updateWith(details); 92 const response = await request.show(); 93 return { request, response }; 94 } 95 96 /** 97 * Runs a manual test for payment 98 * 99 * @param {HTMLButtonElement} button The HTML button. 100 * @param {PaymentOptions?} options. 101 * @param {Object} expected What property values are expected to pass the test. 102 * @param {String?} id And id for the request/response pair. 103 */ 104 async function runManualTest(button, options, expected = {}, id = undefined) { 105 button.disabled = true; 106 const { request, response } = await getPaymentRequestResponse(options, id); 107 await response.complete(); 108 test(() => { 109 assert_idl_attribute( 110 response, 111 "requestId", 112 "Expected requestId to be an IDL attribute." 113 ); 114 assert_equals(response.requestId, request.id, `Expected ids to match`); 115 for (const [attribute, value] of Object.entries(expected)) { 116 assert_idl_attribute( 117 response, 118 attribute, 119 `Expected ${attribute} to be an IDL attribute.` 120 ); 121 assert_equals( 122 response[attribute], 123 value, 124 `Expected response ${attribute} attribute to be ${value}` 125 ); 126 } 127 assert_idl_attribute(response, "details"); 128 assert_equals(typeof response.details, "object", "Expected an object"); 129 // Testing that this does not throw: 130 response.toJSON(); 131 if (options && options.requestShipping) { 132 assert_equals( 133 response.shippingOption, 134 "pass", 135 "request.shippingOption must be 'pass'" 136 ); 137 } else { 138 assert_equals( 139 request.shippingOption, 140 null, 141 "If requestShipping is falsy, request.shippingOption must be null" 142 ); 143 assert_equals( 144 response.shippingOption, 145 null, 146 "request.shippingOption must be null" 147 ); 148 } 149 }, button.textContent.trim()); 150 }