constructor_convert_method_data.https.html (2263B)
1 <!DOCTYPE html> <meta charset="utf-8" /> 2 <title>Validates PaymentMethodData's data member during construction</title> 3 <link 4 rel="help" 5 href="https://w3c.github.io/browser-payment-api/#constructor" 6 /> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 <script> 10 const details = { 11 total: { 12 label: "Total", 13 amount: { 14 currency: "USD", 15 value: "0.00", 16 }, 17 }, 18 }; 19 20 test(() => { 21 new PaymentRequest([{ supportedMethods: "basic-card" }], details); 22 new PaymentRequest( 23 [{ supportedMethods: "https://apple.com/apple-pay" }], 24 details 25 ); 26 }, "Smoke test."); 27 28 const knownPMIs = ["basic-card", "https://apple.com/apple-pay"]; 29 const unknownPMIs = ["fake-pmi", "https://does-not.exist"]; 30 31 promise_test(async t => { 32 for (const supportedMethods of [].concat(knownPMIs).concat(unknownPMIs)) { 33 const method = { supportedMethods }; 34 const request = new PaymentRequest([method], details); 35 assert_throws_js( 36 TypeError, 37 () => { 38 const badMethod = Object.assign( 39 {}, 40 method, 41 { data: 123 } // <- this will throw 42 ); 43 new PaymentRequest([badMethod], details); 44 }, 45 "PaymentMethodData.data can't be converted to an Object." 46 ); 47 } 48 }, "Tries to convert data member during Payment Request construction, irrespective of PMI."); 49 50 promise_test(async t => { 51 for (const supportedMethods of knownPMIs) { 52 const method = { supportedMethods }; 53 const request = new PaymentRequest([method], details); 54 55 // Only check the PMIs that are actually supported 56 if (!(await request.canMakePayment())) continue; 57 58 assert_throws_js( 59 TypeError, 60 () => { 61 const badMethod = Object.assign( 62 {}, 63 method, 64 /* This is invalid in both Apple Pay and Basic Card */ 65 { data: { supportedNetworks: "this will throw" } } 66 ); 67 new PaymentRequest([badMethod], details); 68 }, 69 "PaymentMethodData.data is invalid." 70 ); 71 } 72 }, "Converts PaymentMethodData's data to mandated IDL type during PaymentRequest construction."); 73 </script>