fix(department): wait for auto-role redirect before department test

When user has exactly 1 active role, chooseRole auto-selects it and
enqueues a router.push to the landing page. loginAndDismissSelectors
now waits for this SPA redirect to complete, so the route guard's
org/department loading runs before page.goto('/clazz') does a full
reload. Also adds extra retry for department loading as fallback.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-02 22:36:35 +08:00
parent 39f22ca0da
commit d95c675f75
+31 -1
View File
@@ -51,7 +51,23 @@ async function loginAndDismissSelectors(page: any) {
console.log('[DeptTest] clicked role');
await page.waitForTimeout(3_000);
} else {
console.log('[DeptTest] at / but no role selector, break');
console.log('[DeptTest] at / but no role selector');
// 无角色选择器 → 当前用户角色已自动选中(1个活跃角色)。
// chooseRole 内部调用 router.push('/role/profile') 是异步的,
// 等待重定向完成,让该 navigtion 的 route guard 走完 org/dept 加载。
// 避免后续 page.goto 的完整页面加载冲掉未完成的 SPA 导航。
try {
await page.waitForFunction(
() => window.location.pathname !== '/',
{ timeout: 10_000 }
);
const newPath = await page.evaluate(() => window.location.pathname);
console.log('[DeptTest] role auto-redirected to', newPath);
// route guard 已在这个导航中完成 org/dept 加载,CurrentDepartmentId 已写入
} catch {
// 10s 内未重定向(可能没有活跃角色),不做处理
console.log('[DeptTest] no redirect within 10s (0 roles?)');
}
break;
}
} else {
@@ -118,6 +134,20 @@ test.describe('单部门透明', () => {
});
console.log('[DeptTest] state after goto:', JSON.stringify(pageState, null, 2));
console.log('[DeptTest] deptId after goto:', pageState.CurrentDepartmentId);
// 如果部门尚未设置,给 route guard 额外时间完成异步加载
if (!pageState.CurrentDepartmentId) {
console.log('[DeptTest] waiting extra for department loading...');
await page.waitForTimeout(8_000);
const retry = await page.evaluate(() => window.localStorage.getItem('CurrentDepartmentId'));
console.log('[DeptTest] CurrentDepartmentId after extra wait:', retry);
if (retry) {
pageState.CurrentDepartmentId = retry;
pageState.pathname = await page.evaluate(() => window.location.pathname);
}
}
// 验证 localStorage 写入了 CurrentDepartmentId
const deptId = pageState.CurrentDepartmentId;
expect(deptId).toBeTruthy();