<moz-card
id="heading-icon-card"
data-l10n-id="test-id-3"
heading="heading with icon"
type="accordion"
iconSrc="chrome://global/skin/icons/settings.svg"
class="withHeadingIcon"
>
<div>heading icon test content</div>
</moz-card>
<hr />
</div>
<pre id="test"></pre>
<script>
let generatedSlotText = "generated slotted element";
let testHeading = "test heading";
const { BrowserTestUtils } = ChromeUtils.importESModule( "resource://testing-common/BrowserTestUtils.sys.mjs"
);
function assertBasicProperties(card, expectedValues) {
info(`Testing card with ID: ${card.id}`);
ok(card, "The card element should exist");
is(card.localName, "moz-card", "The card should have the correct tag");
let l10nId = card.getAttribute("data-l10n-id");
if (expectedValues["data-l10n-id"]) {
is(
l10nId,
expectedValues["data-l10n-id"], "l10n id should be unchanged"
);
}
let cardContent = card.firstElementChild;
ok(cardContent, "The content should exist");
is(
cardContent.textContent,
expectedValues.contentText, "The content should be unchanged"
);
is(
card.contentEl.id, "content", "The content container should have the correct ID"
);
if (card.type != "accordion") {
is(
card.contentEl.getAttribute("aria-describedby"), "content", "The content container should be described by the 'content' slot"
);
}
if (expectedValues.headingText) {
ok(card.headingEl, "Heading should exist");
is(
card.headingEl.textContent,
expectedValues.headingText, "Heading should match the 'heading' attribute value"
);
}
}
async function assertAccordionCardProperties(card) {
ok(card.detailsEl, "The details element should exist");
ok(
card.detailsEl.querySelector("summary"), "There should be a summary element within the details element"
);
ok(
card.detailsEl
.querySelector("summary")
.querySelector(".chevron-icon"), "There should be a chevron icon div within the summary element"
);
let cardToggled = BrowserTestUtils.waitForEvent(card, "toggle");
card.detailsEl.querySelector("summary").click();
let openEvent = await cardToggled;
is(openEvent.newState, "open", "Event shows new state is open");
is(openEvent.oldState, "closed", "Event shows old state is closed");
ok(
card.detailsEl.open, "When the accordion is closed, it should expand when clicked"
);
cardToggled = BrowserTestUtils.waitForEvent(card, "toggle");
card.detailsEl.querySelector("summary").click();
let closeEvent = await cardToggled;
is(closeEvent.newState, "closed", "Event shows new state is closed");
is(closeEvent.oldState, "open", "Event shows old state is open");
ok(
!card.detailsEl.open, "When the accordion is open, it should collapse when clicked"
);
}
function assertHeadingIconCardProperties(card) {
ok(
card.shadowRoot
.querySelector("#heading-wrapper")
.querySelector( "img[src='chrome://global/skin/icons/settings.svg']"
), "The heading icon element should exist"
);
}
async function generateCard(values) {
let card = document.createElement("moz-card");
for (let [key, value] of Object.entries(values)) {
card.setAttribute(key, value);
}
let div = document.createElement("div"); div.innerText = generatedSlotText;
card.appendChild(div);
document.body.appendChild(card);
await card.updateComplete;
document.body.appendChild(document.createElement("hr"));
return card;
}
accordionCard = await generateCard({
type: "accordion",
id: "expanded-accordion-card", "data-l10n-id": "generated-id-2",
heading: testHeading,
expanded: true,
});
is(
accordionCard.expanded,
true, "Accordion card should be expanded on initial render"
);
is(
accordionCard.detailsEl.open,
true, "The details element should be expanded"
);
});
add_task(async function testCardRole() {
let card = await generateCard({
id: "generated-role-card",
role: "presentation",
});
let article = card.shadowRoot.querySelector("article");
is( article.getAttribute("role"), "presentation", "The role should be mapped to the inner article element"
);
ok(
!card.hasAttribute("role"), "The role attribute should be removed from the host element"
);
let cardWithoutRole = await generateCard({
id: "generated-no-role-card",
});
let articleWithoutRole =
cardWithoutRole.shadowRoot.querySelector("article");
ok(
!articleWithoutRole.hasAttribute("role"), "The article should not have a role when none is provided"
);
});
add_task(async function testHeadingIconCard() {
assertBasicProperties(document.getElementById("heading-icon-card"), { "data-l10n-id": "test-id-3",
contentText: "heading icon test content",
headingText: "heading with icon",
});
assertHeadingIconCardProperties(
document.getElementById("heading-icon-card"),
{ "data-l10n-id": "test-id-3",
contentText: "heading icon test content",
headingText: "heading with icon",
}
);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.