Files
MikrotikManager/tests/e2e/users-layout.spec.ts
T

26 lines
5.9 KiB
TypeScript

import {test,expect} from "@playwright/test"
import {user,server,catalogResponse} from "./fixtures/users"
for(const [name,width] of [["desktop",1440],["mobile",390],["narrow",320]] as const)test("Пользователи: клавиатура и адаптивность "+name,async({page})=>{
await page.setViewportSize({width,height:900});const longUser={...user,name:"Очень длинное имя пользователя для проверки таблицы и формы",email:"[email protected]",login:"[email protected]"}
await page.route("**/health",r=>r.fulfill({json:{ok:true}}));await page.route("**/api/**",r=>{const p=new URL(r.request().url()).pathname;return catalogResponse(r,{json:p==="/api/auth/config"?{required:false}:p==="/api/users"?{users:[longUser]}:p==="/api/servers"?[{...server,name:"Очень длинное имя устройства для проверки права доступа",host:"router-with-long-hostname.example.test"}]:{interfaces:[]}})})
let saved: {sections: {section:string;level:string}[];servers:{serverId:string;level:string}[]} | undefined
await page.route("**/api/users/u-test",r=>{saved=r.request().postDataJSON();return r.fulfill({status:503,json:{error:"Профиль не сохранён. Повторите позже."}})})
await page.goto("/users");await expect(page.getByRole("button",{name:"Добавить пользователя",exact:true})).toBeEnabled();expect(await page.evaluate(()=>document.documentElement.scrollWidth<=window.innerWidth)).toBe(true)
const expand=page.getByRole("button",{name:"Доступ и привязки пользователя "+longUser.name});await expand.focus();await page.keyboard.press("Enter");await expect(expand).toHaveAttribute("aria-expanded","true");await expect(page.getByText("Нет привязанных интерфейсов",{exact:true})).toBeVisible()
await page.screenshot({path:"docs/ux-ui/screens/users-overview-"+name+".png",fullPage:true})
const edit=page.getByRole("button",{name:"Редактировать пользователя "+longUser.name});await edit.focus();await page.keyboard.press("Enter");const dialog=page.getByRole("dialog");await expect(page.getByRole("textbox",{name:"Полное имя",exact:true})).toHaveValue(longUser.name);await expect(page.getByRole("textbox",{name:"Email",exact:true})).toHaveValue(longUser.email)
const bounds=await dialog.boundingBox();expect(bounds&&bounds.x>=0&&bounds.x+bounds.width<=width+1).toBe(true);expect(width>=640||bounds&&bounds.width>=width-2).toBe(true)
await page.screenshot({path:"docs/ux-ui/screens/users-profile-"+name+".png",fullPage:true})
const sections=dialog.getByRole("tab",{name:/Разделы/});await sections.focus();await page.keyboard.press("Enter");await expect(sections).toHaveAttribute("aria-selected","true")
const permission=dialog.getByRole("button",{name:"Дашборд: Управление",exact:true});await permission.focus();await page.keyboard.press("Space");await expect(permission).toHaveAttribute("aria-pressed","true")
await dialog.getByRole("tab",{name:/Серверы/}).click();await expect(dialog.getByRole("group",{name:"Права: Очень длинное имя устройства для проверки права доступа"})).toBeVisible();await dialog.getByRole("button",{name:"Очень длинное имя устройства для проверки права доступа: Управление",exact:true}).click();await dialog.getByRole("tab",{name:/Интерфейсы/}).click();await expect(dialog.getByText("Нет интерфейсов в каталоге",{exact:true})).toBeVisible()
await page.screenshot({path:"docs/ux-ui/screens/users-catalog-"+name+".png",fullPage:true})
await dialog.getByRole("button",{name:"Сохранить",exact:true}).click();await expect(dialog.getByRole("alert",{name:"Сохранение пользователя"})).toContainText("Профиль не сохранён");expect(saved?.sections).toContainEqual({section:"Дашборд",level:"write"});expect(saved?.servers).toContainEqual({serverId:"1",level:"write"});const footer=dialog.getByRole("button",{name:"Сохранить",exact:true});const box=await footer.boundingBox();expect(box&&box.y>=0&&box.y+box.height<=900).toBe(true);expect(await dialog.evaluate(el=>el.scrollWidth<=el.clientWidth+1)).toBe(true)
await page.screenshot({path:"docs/ux-ui/screens/users-error-"+name+".png",fullPage:true})
await dialog.getByRole("button",{name:"Отмена",exact:true}).click();await expect(dialog).toHaveCount(0);await expect(edit).toBeFocused()
})
test("Пользователи: ошибки обязательных полей и фокус",async({page})=>{
await page.route("**/health",r=>r.fulfill({json:{ok:true}}));await page.route("**/api/**",r=>catalogResponse(r,{json:new URL(r.request().url()).pathname==="/api/auth/config"?{required:false}:new URL(r.request().url()).pathname==="/api/users"?{users:[user]}:new URL(r.request().url()).pathname==="/api/servers"?[server]:{interfaces:[]}}))
await page.goto("/users");await page.getByRole("button",{name:"Добавить пользователя",exact:true}).click();const dialog=page.getByRole("dialog");await dialog.getByRole("tab",{name:/Интерфейсы/}).click();await dialog.getByRole("button",{name:"Создать",exact:true}).click();const name=dialog.getByRole("textbox",{name:"Полное имя",exact:true});await expect(name).toBeFocused();await expect(name).toHaveAttribute("aria-invalid","true");await name.fill("Тест");await dialog.getByRole("textbox",{name:"Email",exact:true}).fill("bad@");await dialog.getByRole("tab",{name:/Интерфейсы/}).click();await dialog.getByRole("button",{name:"Создать",exact:true}).click();const email=dialog.getByRole("textbox",{name:"Email",exact:true});await expect(email).toBeFocused();await expect(email).toHaveAttribute("aria-invalid","true")
})