|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <title>MozPageHeader Tests</title> |
| 6 | + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
| 7 | + <link |
| 8 | + rel="stylesheet" |
| 9 | + href="chrome://mochikit/content/tests/SimpleTest/test.css" |
| 10 | + /> |
| 11 | + <link rel="stylesheet" href="chrome://global/skin/in-content/common.css" /> |
| 12 | + <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script> |
| 13 | + <script |
| 14 | + type="module" |
| 15 | + src="chrome://global/content/elements/moz-page-header.mjs" |
| 16 | + ></script> |
| 17 | + <script src="lit-test-helpers.js"></script> |
| 18 | + <script> |
| 19 | + let html; |
| 20 | + let testHelpers = new LitTestHelpers(); |
| 21 | + |
| 22 | + add_setup(async function setup() { |
| 23 | + ({ html } = await testHelpers.setupLit()); |
| 24 | + const templateFn = attrs => html` |
| 25 | + <moz-page-header ${attrs}></moz-page-header> |
| 26 | + `; |
| 27 | + testHelpers.setupTests({ templateFn }); |
| 28 | + }); |
| 29 | + |
| 30 | + add_task(async function testMozPageHeaderProperties() { |
| 31 | + const HEADING = "Page Heading"; |
| 32 | + const DESCRIPTION = "Some description of this page."; |
| 33 | + const ICON_SRC = "chrome://global/skin/icons/info.svg"; |
| 34 | + |
| 35 | + let template = testHelpers.templateFn({ |
| 36 | + heading: HEADING, |
| 37 | + description: DESCRIPTION, |
| 38 | + iconsrc: ICON_SRC, |
| 39 | + }); |
| 40 | + let renderTarget = await testHelpers.renderTemplate(template); |
| 41 | + let header = renderTarget.firstElementChild; |
| 42 | + |
| 43 | + ok(header, "The page header renders."); |
| 44 | + |
| 45 | + let headingEl = header.shadowRoot.querySelector("h1"); |
| 46 | + is( |
| 47 | + headingEl?.textContent.trim(), |
| 48 | + HEADING, |
| 49 | + "Heading element renders with the expected text." |
| 50 | + ); |
| 51 | + |
| 52 | + let descriptionEl = header.shadowRoot.querySelector(".description"); |
| 53 | + is( |
| 54 | + descriptionEl?.textContent.trim(), |
| 55 | + DESCRIPTION, |
| 56 | + "Description element renders with the expected text." |
| 57 | + ); |
| 58 | + |
| 59 | + let iconEl = header.shadowRoot.querySelector(".icon"); |
| 60 | + ok(iconEl, "Icon element is rendered when iconSrc is set."); |
| 61 | + is(iconEl.getAttribute("src"), ICON_SRC, "Icon has the expected src."); |
| 62 | + |
| 63 | + let backButton = header.shadowRoot.querySelector(".back-button"); |
| 64 | + ok(!backButton, "Back button is not rendered by default."); |
| 65 | + |
| 66 | + header.backButton = true; |
| 67 | + await header.updateComplete; |
| 68 | + |
| 69 | + backButton = header.shadowRoot.querySelector(".back-button"); |
| 70 | + ok(backButton, "Back button appears when backButton is true."); |
| 71 | + |
| 72 | + header.backButton = false; |
| 73 | + await header.updateComplete; |
| 74 | + |
| 75 | + backButton = header.shadowRoot.querySelector(".back-button"); |
| 76 | + ok( |
| 77 | + !backButton, |
| 78 | + "Back button is removed when backButton is set back to false." |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + add_task(async function testMozPageHeaderBackButtonEvent() { |
| 83 | + let template = testHelpers.templateFn({ |
| 84 | + heading: "With back button", |
| 85 | + backbutton: "", |
| 86 | + }); |
| 87 | + let renderTarget = await testHelpers.renderTemplate(template); |
| 88 | + let header = renderTarget.firstElementChild; |
| 89 | + |
| 90 | + let backButton = header.shadowRoot.querySelector(".back-button"); |
| 91 | + ok(backButton, "Back button is present for click test."); |
| 92 | + |
| 93 | + let eventPromise = new Promise(resolve => { |
| 94 | + header.addEventListener("navigate-back", resolve, { once: true }); |
| 95 | + }); |
| 96 | + |
| 97 | + backButton.click(); |
| 98 | + let event = await eventPromise; |
| 99 | + |
| 100 | + ok(event instanceof Event, "Clicking back button dispatches an Event."); |
| 101 | + is( |
| 102 | + event.type, |
| 103 | + "navigate-back", |
| 104 | + "The event type is a 'navigate-back' event." |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + add_task(async function testMozPageHeaderBreadcrumbSlot() { |
| 109 | + const template = html` |
| 110 | + <moz-page-header |
| 111 | + heading="With Breadcrumbs" |
| 112 | + icon-src="chrome://global/skin/icons/info.svg" |
| 113 | + > |
| 114 | + <moz-breadcrumb-group slot="breadcrumbs"> |
| 115 | + <moz-breadcrumb href="about#first" label="First"></moz-breadcrumb> |
| 116 | + <moz-breadcrumb |
| 117 | + href="about#current" |
| 118 | + label="Current" |
| 119 | + ></moz-breadcrumb> |
| 120 | + </moz-breadcrumb-group> |
| 121 | + </moz-page-header> |
| 122 | + `; |
| 123 | + const renderTarget = await testHelpers.renderTemplate(template); |
| 124 | + const header = renderTarget.firstElementChild; |
| 125 | + |
| 126 | + const slot = header.shadowRoot.querySelector( |
| 127 | + 'slot[name="breadcrumbs"]' |
| 128 | + ); |
| 129 | + ok(slot, "Page header renders a slot for breadcrumbs."); |
| 130 | + |
| 131 | + const assigned = slot.assignedElements(); |
| 132 | + is( |
| 133 | + assigned.length, |
| 134 | + 1, |
| 135 | + "One element is assigned into the breadcrumbs slot." |
| 136 | + ); |
| 137 | + const group = assigned[0]; |
| 138 | + is( |
| 139 | + group.localName, |
| 140 | + "moz-breadcrumb-group", |
| 141 | + "moz-breadcrumb-group is slotted in the breadcrumbs slot." |
| 142 | + ); |
| 143 | + }); |
| 144 | + |
| 145 | + add_task(async function testMozPageHeaderSupportPage() { |
| 146 | + let supportPageTemplate = testHelpers.templateFn({ |
| 147 | + label: "Testing support-page", |
| 148 | + "support-page": "test", |
| 149 | + }); |
| 150 | + let renderTarget = |
| 151 | + await testHelpers.renderTemplate(supportPageTemplate); |
| 152 | + let header = renderTarget.firstElementChild; |
| 153 | + |
| 154 | + let supportLink = header.shadowRoot.querySelector(".support-link"); |
| 155 | + ok( |
| 156 | + supportLink, |
| 157 | + "moz-page-header renders a support link when supportPage is provided." |
| 158 | + ); |
| 159 | + |
| 160 | + let adjacentEl = supportLink.previousElementSibling; |
| 161 | + is( |
| 162 | + adjacentEl.id, |
| 163 | + "heading", |
| 164 | + "support link is placed next to the heading when no description is provided." |
| 165 | + ); |
| 166 | + |
| 167 | + header.description = "now we have a description."; |
| 168 | + await header.updateComplete; |
| 169 | + |
| 170 | + supportLink = header.shadowRoot.querySelector(".support-link"); |
| 171 | + adjacentEl = supportLink.previousElementSibling; |
| 172 | + is( |
| 173 | + adjacentEl.id, |
| 174 | + "description", |
| 175 | + "support link is placed next to the description when provided." |
| 176 | + ); |
| 177 | + }); |
| 178 | + </script> |
| 179 | + </head> |
| 180 | + <body> |
| 181 | + <p id="display"></p> |
| 182 | + <div id="content" style="display: none"></div> |
| 183 | + <pre id="test"></pre> |
| 184 | + </body> |
| 185 | +</html> |
0 commit comments