fix(admin): 折叠时恢复图标栏和右侧悬浮菜单

This commit is contained in:
2026-12-02 09:31:36 +08:00
parent 608e912966
commit edfc67e6f6
10 changed files with 487 additions and 426 deletions
+114 -75
View File
@@ -23,6 +23,7 @@ NAMES = "业务报表"
SIMILAR = ("全院财务结算与跨部门对账月报表", "全院财务退款与跨部门对账月报表")
DUPLICATE = "回访明细"
TITLE = ".el-sub-menu__title"
POPUP = ".admin-nav-popper.el-menu--popup-container > .el-menu--popup:visible"
def setup_menu(fixture):
@@ -190,35 +191,46 @@ def compact_layout(page, rail, panel, fold, baseline=None):
assert abs(metrics["rail"] - 64) <= 1, ("compact rail", metrics)
assert abs(metrics["main_x"] - 64) <= 2, ("main should start next to the rail", metrics)
if baseline is not None:
assert metrics == baseline, ("drilling or resizing moved the content", metrics, baseline)
# Measuring width alone would pass even with an absolutely positioned flyout over the page.
expect(page.locator(".el-menu--popup:visible, .el-popover:visible, .el-drawer:visible")).to_have_count(0)
assert rail.evaluate("""nav => {
const b = nav.getBoundingClientRect();
return [...nav.querySelectorAll('a,button')].every(item => {
const r = item.getBoundingClientRect();
return !r.width || (r.left >= b.left - 1 && r.right <= b.right + 1);
});
}"""), "menu controls extend outside the compact rail"
assert page.locator("main").evaluate("""main => {
const b = main.getBoundingClientRect();
return [4, 64, 160].every(dx => [8, 64, 144].every(dy =>
main.contains(document.elementFromPoint(b.left + dx, b.top + dy))));
}"""), "sidebar flyout or backdrop intercepts the content"
assert metrics == baseline, ("opening a flyout or resizing moved the content", metrics, baseline)
return metrics
def compact_label(row):
# A full title can be shortened on screen, but it may not collapse to a single glyph
# or become tooltip-only. The full title remains the control's accessible name.
label = row.locator(".compact-label")
expect(label).to_be_visible()
text = label.inner_text().strip()
assert len(text) > 1 and row.get_attribute("title"), ("missing visible or full label", text)
box, ink = row.bounding_box(), label.bounding_box()
assert box["x"] - 1 <= ink["x"] and ink["x"] + ink["width"] <= box["x"] + box["width"] + 1
return text
def icon_only(row, name):
# Count painted text within the row, not textContent: Element keeps accessible
# names for controls whose visual label is hidden. A fallback initial is an icon.
expect(row).to_have_accessible_name(name)
visible = row.evaluate("""row => {
const walk = document.createTreeWalker(row, NodeFilter.SHOW_TEXT);
const rect = row.getBoundingClientRect(), words = [];
for (let n; (n = walk.nextNode());) {
if (!n.textContent.trim() || n.parentElement.closest('.menu-item-icon,svg')) continue;
const range = document.createRange(); range.selectNodeContents(n);
const r = range.getBoundingClientRect();
if (r.width < 1 || r.height < 1) continue;
const x = Math.max(r.left, rect.left), y = Math.max(r.top, rect.top);
if (x >= Math.min(r.right, rect.right) || y >= Math.min(r.bottom, rect.bottom)) continue;
if (n.parentElement.checkVisibility({opacityProperty: true, visibilityProperty: true}))
words.push(n.textContent.trim());
}
return words;
}""")
assert visible == [], ("compact rail must contain icons only", name, visible)
def popup_box(row, anchor):
# A child that opens in place would also be visible. Verify it is painted to
# the right of its own trigger, within the viewport, not clipped by a rail.
expect(row).to_be_visible()
box = row.locator("xpath=ancestor::ul[contains(@class,'el-menu--popup')][1]")
settle(box.locator("xpath=ancestor::*[contains(concat(' ',normalize-space(@class),' '),' el-popper ')][1]"))
b, a = box.bounding_box(), anchor.bounding_box()
assert a["x"] + a["width"] - 2 <= b["x"] <= a["x"] + a["width"] + 32, (a, b)
assert b["x"] + b["width"] <= row.page.viewport_size["width"] + 1, b
assert row.evaluate("""e => {
const r = e.getBoundingClientRect();
return e.contains(document.elementFromPoint(r.x + r.width / 2, r.y + r.height / 2));
}"""), "floating menu row is covered or clipped"
return {k: round(v, 1) for k, v in b.items()}
def clipped(row, selector, label):
@@ -373,92 +385,119 @@ def check(page, fixture, out):
def unchanged():
return compact_layout(page, compact, panel, fold, report["collapsed"])
group = lambda name: compact.get_by_role("button", name=name, exact=True)
entry = lambda name: compact.get_by_role("link", name=name, exact=True)
back = group("返回上一级")
popups = page.locator(POPUP)
group = lambda name: compact.get_by_role("menuitem", name=name, exact=True)
entry = lambda name: popups.get_by_role("link", name=name, exact=True)
top_leaf = compact.get_by_role("link", name=TOP_LEAF, exact=True)
def only_roots():
expect(compact.get_by_role("menuitem")).to_have_count(4)
for name in (ROOT, OTHER, TOP_LEAF, NAMES):
icon_only(group(name), name)
expect(compact.get_by_role("button", name=re.compile("返回"))).to_have_count(0)
expect(group(NESTED)).to_have_count(0)
expect(group(HIDDEN_ROOT)).to_have_count(0)
def leave_menus():
page.mouse.move(page.viewport_size["width"] - 12, page.viewport_size["height"] - 12)
expect(popups).to_have_count(0)
page.set_viewport_size({"width": 1350, "height": 960})
unchanged()
expect(back).to_be_hidden()
expect(group(HIDDEN_ROOT)).to_have_count(0)
only_roots()
expect(popups).to_have_count(0)
report["collapsed_centers"] = {"logo": center(compact, compact.locator(".logo img"), "brand image")}
for name, row in ((ROOT, group(ROOT)), (OTHER, group(OTHER)), (TOP_LEAF, entry(TOP_LEAF))):
compact_label(row)
expect(row).to_have_attribute("title", name)
report["collapsed_centers"][name] = center(compact, row.locator(".compact-icon svg"), name)
for name in (ROOT, OTHER, TOP_LEAF):
icon = group(name).locator(".menu-item-icon")
report["collapsed_centers"][name] = center(compact, icon, name)
initial = group(OTHER).locator(".menu-item-icon")
assert initial.evaluate("""e => e.querySelector('svg') !== null || !!e.textContent.trim() ||
!['none','normal','""'].includes(getComputedStyle(e, '::before').content)"""), "missing fallback icon"
screenshot(page, out, "collapsed")
# Similar and identical sibling names must be distinguishable without hovering.
group(NAMES).click()
assert compact_label(entry(SIMILAR[0])) != compact_label(entry(SIMILAR[1]))
twins = compact.get_by_role("link", name=re.compile("^" + DUPLICATE))
# Hover previews groups without navigating. Full labels and distinct link targets
# remain available in the flyout rather than abbreviated or wrapped in the rail.
group(NAMES).hover()
for name in SIMILAR:
expect(entry(name).get_by_text(name, exact=True)).to_be_visible()
twins = popups.get_by_role("link", name=re.compile("^" + DUPLICATE))
expect(twins).to_have_count(2)
assert twins.nth(0).inner_text() != twins.nth(1).inner_text()
assert twins.nth(0).get_attribute("aria-label") != twins.nth(1).get_attribute("aria-label")
assert twins.nth(0).get_attribute("href") != twins.nth(1).get_attribute("href")
route_at(page, h_path, h_query)
unchanged()
back.click()
expect(group(NAMES)).to_be_focused()
# Browsing directories (including one with only one permitted child) never chooses a route.
group(OTHER).hover()
page.wait_for_timeout(650) # Exercise the legacy Element Plus hover-to-open delay.
unchanged()
expect(entry(DETAIL)).to_be_hidden()
group(OTHER).press("Enter")
expect(entry(DETAIL)).to_be_focused()
report["single_level_popup"] = popup_box(entry(DETAIL), group(OTHER))
expect(twins).to_have_count(0)
expect(entry(HIDDEN)).to_have_count(0)
entry(DETAIL).hover()
page.wait_for_timeout(650) # Hold the pointer in the popper beyond the menu's leave delay.
expect(entry(DETAIL)).to_be_visible()
route_at(page, h_path, h_query)
only_roots()
unchanged()
back.press("Space")
expect(group(OTHER)).to_be_focused()
leave_menus()
route_at(page, h_path, h_query)
unchanged()
group(ROOT).click()
expect(group(NESTED)).to_be_focused()
# The next level is another flyout, not a replacement list or an expanded aside.
group(ROOT).hover()
nested = title(popups, NESTED)
report["second_level_popup"] = popup_box(nested, group(ROOT))
arrow_metrics(nested, NESTED)
expect(entry(LEAF)).to_be_hidden()
route_at(page, h_path, h_query)
unchanged()
group(NESTED).press("Space")
expect(entry(HOME)).to_be_focused()
compact_label(entry(LEAF))
expect(group(ROOT)).to_be_hidden()
nested.hover()
leaf = entry(LEAF)
report["third_level_popup"] = popup_box(leaf, nested)
leaf.hover()
page.wait_for_timeout(650)
expect(leaf).to_be_visible()
expect(nested).to_be_visible()
expect(popups).to_have_count(2)
only_roots()
route_at(page, h_path, h_query)
unchanged()
screenshot(page, out, "collapsed-third-level")
page.keyboard.press("Tab")
expect(entry(LEAF)).to_be_focused()
page.keyboard.press("Enter")
leaf.click()
route_at(page, c_path, c_query)
expect(cost_page).to_be_visible()
expect(entry(LEAF)).to_have_attribute("aria-current", "page")
unchanged()
back.click()
expect(group(NESTED)).to_be_focused()
back.press("Enter")
expect(group(ROOT)).to_be_focused()
expect(back).to_be_hidden()
route_at(page, c_path, c_query)
expect(popups).to_have_count(0)
only_roots()
unchanged()
entry(TOP_LEAF).click()
route_at(page, "solo", {})
expect(entry(TOP_LEAF)).to_have_attribute("aria-current", "page")
# Visit a nested index (empty path with default params), a direct root leaf, then
# a single-child directory using the exact router links the collapsed sidebar displays.
group(ROOT).hover()
nested.hover()
entry(HOME).click()
route_at(page, h_path, h_query, trailing=True)
expect(tab(HOME)).to_have_count(1)
expect(popups).to_have_count(0)
unchanged()
group(OTHER).click()
top_leaf.click()
route_at(page, "solo", {})
expect(top_leaf).to_have_attribute("aria-current", "page")
unchanged()
group(OTHER).hover()
route_at(page, "solo", {})
entry(DETAIL).click()
route_at(page, d_path, d_query)
expect(entry(DETAIL)).to_have_attribute("aria-current", "page")
expect(cost_page).to_be_visible()
expect(popups).to_have_count(0)
unchanged()
page.reload()
route_at(page, d_path, d_query)
expect(cost_page).to_be_visible()
expect(popups).to_have_count(0)
only_roots()
unchanged()
# The mobile drawer starts closed, performs real link navigation, then closes. Neither
# opening it nor choosing a link changes the desktop preference saved by the fold button.
group(OTHER).hover()
expect(entry(DETAIL)).to_be_visible()
page.set_viewport_size({"width": 390, "height": 844})
expect(popups).to_have_count(0)
expect(compact).to_be_hidden()
expect(rail).to_be_hidden()
expect(panel).to_be_hidden()