test_link_rel_canonical.js (1066B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { findCandidates } = ChromeUtils.importESModule( 7 "moz-src:///browser/components/tabnotes/CanonicalURL.sys.mjs" 8 ); 9 10 /** 11 * @param {string|undefined} [url] 12 * @returns {Document} 13 */ 14 function getDocument(url) { 15 const html = ` 16 <!DOCTYPE html> 17 <html> 18 <head> 19 <meta charset="utf-8"> 20 ${url ? `<link rel="canonical" href="${url}">` : ""} 21 </head> 22 <body> 23 </body> 24 </html> 25 `; 26 return Document.parseHTMLUnsafe(html); 27 } 28 29 add_task(async function test_link_rel_canonical_missing() { 30 const doc = getDocument(); 31 32 const candidates = findCandidates(doc); 33 34 Assert.equal( 35 candidates.link, 36 undefined, 37 `link[rel="canonical"] should not be found` 38 ); 39 }); 40 41 add_task(async function test_link_rel_canonical_present() { 42 const doc = getDocument("https://www.example.com"); 43 44 const candidates = findCandidates(doc); 45 46 Assert.equal( 47 candidates.link, 48 "https://www.example.com", 49 `link[rel="canonical"] should be found` 50 ); 51 });