|
1 | 1 | import "./sgds-web-component";
|
2 |
| -import { aTimeout, assert, expect, fixture, fixtureCleanup, nextFrame } from "@open-wc/testing"; |
| 2 | +import { aTimeout, assert, expect, fixture, fixtureCleanup, nextFrame, oneEvent, waitUntil } from "@open-wc/testing"; |
3 | 3 | import { html } from "lit";
|
4 | 4 | import { SgdsMainnav, SgdsMainnavDropdown, SgdsMainnavItem, type MainnavExpandSize } from "../src/components";
|
5 | 5 |
|
@@ -273,9 +273,49 @@ describe("sgds-mainnav-dropdown", () => {
|
273 | 273 | { ignoreAttributes: ["id"] }
|
274 | 274 | );
|
275 | 275 | });
|
| 276 | + |
276 | 277 | it("when prop active=true, .active class is defined in the button", async () => {
|
277 | 278 | const el = await fixture(html`<sgds-mainnav-dropdown active></sgds-mainnav-dropdown>`);
|
278 | 279 |
|
279 | 280 | expect(el.shadowRoot?.querySelector("a[role=button]")).to.have.class("active");
|
280 | 281 | });
|
| 282 | + |
| 283 | + it("calls mainnav.hide() and emits sgds-after-hide when dropdown item is clicked", async () => { |
| 284 | + const mainnav = await fixture<SgdsMainnav>(html` |
| 285 | + <sgds-mainnav expand="lg"> |
| 286 | + <sgds-mainnav-dropdown> |
| 287 | + <span slot="toggler">Menu</span> |
| 288 | + <sgds-dropdown-item><a href="#">Item 1</a></sgds-dropdown-item> |
| 289 | + <sgds-dropdown-item><a href="#">Item 2</a></sgds-dropdown-item> |
| 290 | + </sgds-mainnav-dropdown> |
| 291 | + </sgds-mainnav> |
| 292 | + `); |
| 293 | + |
| 294 | + const dropdown = mainnav.querySelector("sgds-mainnav-dropdown")!; |
| 295 | + // simulate mobile state if needed |
| 296 | + (dropdown as any)._breakpointReached = true; |
| 297 | + await dropdown.updateComplete; |
| 298 | + |
| 299 | + // Wait for slotchange wiring to run and for defaultSlotItems to be populated |
| 300 | + await waitUntil(() => dropdown.defaultSlotItems.length > 0, "slot items not ready", { |
| 301 | + interval: 20, |
| 302 | + timeout: 1000 |
| 303 | + }); |
| 304 | + |
| 305 | + const firstItem = dropdown.defaultSlotItems[0] as HTMLElement & { shadowRoot?: ShadowRoot }; |
| 306 | + |
| 307 | + // anchor may be in light DOM or inside shadowRoot of dropdown item — handle both |
| 308 | + const anchor = (firstItem.shadowRoot?.querySelector("a") ?? firstItem.querySelector("a")) as HTMLElement | null; |
| 309 | + expect(anchor, "expected anchor to exist").to.exist; |
| 310 | + |
| 311 | + // listen for the mainnav hide event (mainnav emits sgds-after-hide when hide finishes) |
| 312 | + const evPromise = oneEvent(mainnav, "sgds-after-hide"); |
| 313 | + |
| 314 | + // trigger click that should call mainnav.hide() |
| 315 | + anchor!.click(); |
| 316 | + |
| 317 | + // wait for hide to finish |
| 318 | + const ev = await evPromise; |
| 319 | + expect(ev).to.exist; |
| 320 | + }); |
281 | 321 | });
|
0 commit comments