head.js (2959B)
1 /** @import { Setting } from "chrome://global/content/preferences/Setting.mjs"; */ 2 3 /** 4 * @callback TestSettingControlCommonPropertiesFunction 5 * @param {(config: Record<string, any>) => Promise<HTMLElement>} renderTemplateFunction 6 */ 7 8 /** 9 * Asserts all properties on the element 10 * that is returned from the provided function. 11 * 12 * @type {TestSettingControlCommonPropertiesFunction} 13 */ 14 async function testCommonSettingControlPropertiesSet(renderTemplateFunction) { 15 const l10nId = "l10n-test-id"; 16 const l10nArgs = { foo: "bar" }; 17 const iconSrc = "anicon.png"; 18 const supportPage = "https://support.page"; 19 const subcategory = "the sub category"; 20 const label = "foo-bar"; 21 const slot = "foo"; 22 23 const element = await renderTemplateFunction({ 24 l10nId, 25 l10nArgs, 26 iconSrc, 27 supportPage, 28 subcategory: "the sub category", 29 slot, 30 controlAttrs: { 31 label, 32 }, 33 }); 34 35 is( 36 element.getAttribute("data-l10n-id"), 37 l10nId, 38 "sets data-l10n-id attribute" 39 ); 40 41 is( 42 element.getAttribute("data-l10n-args"), 43 JSON.stringify(l10nArgs), 44 "converts data-l10n-args to stringified JSON object" 45 ); 46 47 is(element.dataset.subcategory, subcategory, "sets subcategory"); 48 49 is(element.getAttribute("label"), label, "sets controlAttrs.label"); 50 51 is(element.iconSrc, iconSrc, "sets iconSrc"); 52 53 is(element.supportPage, supportPage, "sets supportPage"); 54 55 is(element.slot, slot, "sets slot"); 56 } 57 58 /** 59 * Asserts all unset properties on the element 60 * that is returned from the provided function. 61 * 62 * @type {TestSettingControlCommonPropertiesFunction} 63 */ 64 async function testCommonSettingControlPropertiesUnset(renderTemplateFunction) { 65 info("Test common properties when unset"); 66 const element = await renderTemplateFunction({}); 67 ok(!element.hasAttribute("data-l10n-id"), "no data-l10n-id attribute"); 68 ok(!element.hasAttribute("data-l10n-args"), "no data-l10n-args"); 69 ok(!element.dataset.subcategory, "no subcategory"); 70 ok(!element.hasAttribute("label"), "no controlAttrs.label"); 71 ok(!element.iconSrc, "no iconSrc"); 72 ok(!element.supportPage, "no supportPage"); 73 ok(!element.slot, "no slot"); 74 } 75 76 /** 77 * Asserts all properties set on the element 78 * that is returned from the provided function. 79 * 80 * @type {TestSettingControlCommonPropertiesFunction} 81 */ 82 async function testCommonSettingControlProperties(renderTemplateFunction) { 83 await testCommonSettingControlPropertiesSet(renderTemplateFunction); 84 await testCommonSettingControlPropertiesUnset(renderTemplateFunction); 85 } 86 87 /** 88 * Waits for a setting to emit a "change" event. 89 * 90 * @param {Setting} setting - The setting object to watch for changes. 91 * @returns {Promise<void>} A promise that resolves when the setting emits a 92 "change" event 93 */ 94 function waitForSettingChange(setting) { 95 return new Promise(resolve => { 96 setting.on("change", function handler() { 97 setting.off("change", handler); 98 resolve(); 99 }); 100 }); 101 }