test_getAttributes.html (2935B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Test DOMLocalization.prototype.getAttributes</title> 6 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> 8 <script type="application/javascript"> 9 "use strict"; 10 11 function assertThrows(fn, expected, msg) { 12 try { 13 fn(); 14 ok(false, `Error unexpectedly NOT thrown: ${expected} (${msg})`); 15 } catch (e) { 16 is(e.message, expected, msg); 17 } 18 } 19 20 window.onload = function() { 21 SimpleTest.waitForExplicitFinish(); 22 23 const domLoc = new DOMLocalization( 24 [], 25 false, 26 ); 27 28 const p1 = document.querySelectorAll("p")[0]; 29 const p2 = document.querySelectorAll("p")[1]; 30 const p3 = document.querySelectorAll("p")[2]; 31 const p4 = document.querySelectorAll("p")[3]; 32 const p5 = document.querySelectorAll("p")[4]; 33 const p6 = document.querySelectorAll("p")[5]; 34 const p7 = document.querySelectorAll("p")[6]; 35 const attrs1 = domLoc.getAttributes(p1); 36 const attrs2 = domLoc.getAttributes(p2); 37 const attrs3 = domLoc.getAttributes(p3); 38 isDeeply(attrs1, { 39 id: null, 40 args: null, 41 }); 42 isDeeply(attrs2, { 43 id: "id1", 44 args: null, 45 }); 46 isDeeply(attrs3, { 47 id: "id2", 48 args: { 49 userName: "John", 50 }, 51 }); 52 53 // Note: In tests these errors are thrown, but in production they are 54 // merely causing logspam (bug 1741430). 55 assertThrows( 56 () => domLoc.getAttributes(p4), 57 "DOMLocalization.getAttributes: [dom/l10n] Failed to parse l10n-args JSON (id3): [not a JSON obj]", 58 "Expected error when data-l10n-args cannot be parsed as JSON" 59 ); 60 61 assertThrows( 62 () => domLoc.getAttributes(p5), 63 "DOMLocalization.getAttributes: [dom/l10n] Failed to parse l10n-args as JSON object (id4): 123456", 64 "Expected error when data-l10n-args is not a JSON object" 65 ); 66 67 assertThrows( 68 () => domLoc.getAttributes(p6), 69 "DOMLocalization.getAttributes: [dom/l10n] Failed to convert l10n-args JSON (id5): {\"a\":1,\"arr\":[],\"c\":2}", 70 "Expected error when data-l10n-args contains array members" 71 ); 72 73 assertThrows( 74 () => domLoc.getAttributes(p7), 75 "DOMLocalization.getAttributes: [dom/l10n] Failed to parse l10n-args JSON (): invalid JSON and without l10n-id", 76 "Expected error when data-l10n-args is not a JSON object, without l10n-id" 77 ); 78 79 SimpleTest.finish(); 80 }; 81 </script> 82 </head> 83 <body> 84 <p /> 85 <p data-l10n-id="id1" /> 86 <p data-l10n-id="id2" data-l10n-args='{"userName": "John"}' /> 87 <p data-l10n-id="id3" data-l10n-args="[not a JSON obj]" /> 88 <p data-l10n-id="id4" data-l10n-args="123456" /> 89 <p data-l10n-id="id5" data-l10n-args='{"a":1,"arr":[],"c":2}' /> 90 <p data-l10n-args="invalid JSON and without l10n-id" /> 91 </body> 92 </html>