39 lines
3.3 KiB
TypeScript
39 lines
3.3 KiB
TypeScript
import { test, expect, type Page } from "@playwright/test"
|
|
import { evoCatalog, evoSettings } from "./fixtures/evobgp"
|
|
const community = { id: "copy", value: "64512:7", name: "Копируемое значение", description: "", type: "custom", filterIds: [], serverCount: 0, prefixCount: null, action: "permit", enabled: true }
|
|
async function setup(page: Page) {
|
|
await page.addInitScript(() => {
|
|
const state = window as unknown as { attempts: number; finishCopy: (failed: boolean) => void }
|
|
state.attempts = 0
|
|
Object.defineProperty(navigator, "clipboard", { value: { writeText: () => { state.attempts++; return new Promise<void>((resolve, reject) => { state.finishCopy = failed => failed ? reject(new Error("Clipboard denied")) : resolve() }) } } })
|
|
})
|
|
await page.route("**/health", r => r.fulfill({ json: { ok: true } }))
|
|
await page.route("**/api/**", r => { const p = new URL(r.request().url()).pathname; return r.fulfill({ json: p === "/api/auth/config" ? { required: false } : p === "/api/evobgp/settings" ? evoSettings : p === "/api/evobgp/catalog" ? { ...evoCatalog, communities: [community] } : {} }) })
|
|
await page.goto("/communities")
|
|
return page.getByRole("button", { name: "Скопировать community: 64512:7", exact: true })
|
|
}
|
|
for (const failed of [false, true]) test("Communities copy: pending/ack/retry " + failed, async ({ page }) => {
|
|
const button = await setup(page)
|
|
await button.evaluate(el => { (el as HTMLButtonElement).click(); (el as HTMLButtonElement).click() })
|
|
await expect(button).toBeDisabled(); await expect(page.getByText("Копирование community…", { exact: true })).toBeVisible()
|
|
expect(await page.evaluate(() => (window as unknown as { attempts: number }).attempts)).toBe(1)
|
|
await expect(page.getByText("Community 64512:7 скопировано", { exact: true })).toHaveCount(0)
|
|
await page.evaluate(f => (window as unknown as { finishCopy: (failed: boolean) => void }).finishCopy(f), failed)
|
|
await expect(button).toBeEnabled()
|
|
if (failed) {
|
|
await expect(page.getByRole("alert").filter({ hasText: "Community не скопировано" })).toBeVisible()
|
|
await expect(page.getByText("Community 64512:7 скопировано", { exact: true })).toHaveCount(0)
|
|
await button.click(); await page.evaluate(() => (window as unknown as { finishCopy: (failed: boolean) => void }).finishCopy(false))
|
|
await expect(page.getByText("Community не скопировано", { exact: true })).toHaveCount(0)
|
|
}
|
|
await expect(page.getByText("Community 64512:7 скопировано", { exact: true })).toBeVisible()
|
|
})
|
|
for (const failed of [false, true]) test("Communities copy: late result after unmount " + failed, async ({ page }) => {
|
|
const button = await setup(page); await button.click(); await expect(button).toBeDisabled()
|
|
await page.locator('a[href="/asns"]').first().click(); await expect(page).toHaveURL(/asns$/)
|
|
await page.evaluate(f => (window as unknown as { finishCopy: (failed: boolean) => void }).finishCopy(f), failed)
|
|
await page.locator('a[href="/communities"]').first().click(); await expect(button).toBeEnabled()
|
|
await expect(page.getByText("Community 64512:7 скопировано", { exact: true })).toHaveCount(0)
|
|
await expect(page.getByText("Community не скопировано", { exact: true })).toHaveCount(0)
|
|
})
|