Skip to content

Commit 2056ff9

Browse files
author
tong.hau
committed
chore(mainnav): add failed test case
1 parent f0c236c commit 2056ff9

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

test/mainnav.test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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";
33
import { html } from "lit";
44
import { SgdsMainnav, SgdsMainnavDropdown, SgdsMainnavItem, type MainnavExpandSize } from "../src/components";
55

@@ -273,9 +273,49 @@ describe("sgds-mainnav-dropdown", () => {
273273
{ ignoreAttributes: ["id"] }
274274
);
275275
});
276+
276277
it("when prop active=true, .active class is defined in the button", async () => {
277278
const el = await fixture(html`<sgds-mainnav-dropdown active></sgds-mainnav-dropdown>`);
278279

279280
expect(el.shadowRoot?.querySelector("a[role=button]")).to.have.class("active");
280281
});
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+
});
281321
});

0 commit comments

Comments
 (0)