Files
stock-manager/tests/e2e/language-switcher.spec.ts
T

133 lines
4.5 KiB
TypeScript

import { expect, type Page, test } from "@playwright/test"
async function setLocaleCookie(
page: Page,
locale: "en" | "es",
baseURL?: string,
) {
await page.context().addCookies([
{
name: "stock-manager-locale",
value: locale,
url: baseURL ?? "http://127.0.0.1:3100",
},
])
}
async function expectLocaleCookie(page: Page, locale: "en" | "es") {
await expect
.poll(async () => {
const cookies = await page.context().cookies()
return cookies.find((cookie) => cookie.name === "stock-manager-locale")
?.value
})
.toBe(locale)
}
async function signInAsAdmin(page: Page, baseURL?: string) {
await setLocaleCookie(page, "en", baseURL)
await page.goto("/login")
await page.getByLabel("Username").fill("admin")
await page.getByLabel("Password").fill("admin-password")
await page.getByRole("button", { name: "Sign In" }).click()
await expect(page).toHaveURL("/")
}
test.describe("language switcher", () => {
test("switches the login page language in place through the locale cookie", async ({
baseURL,
page,
}) => {
await setLocaleCookie(page, "en", baseURL)
await page.goto("/login")
await expect(page).toHaveURL(/\/login$/)
await expect(page.locator("html")).toHaveAttribute("lang", "en")
await expect(page.getByRole("heading", { name: "Sign In" })).toBeVisible()
await page.getByRole("button", { name: "Language: English" }).click()
const options = page.getByRole("menuitemradio")
await expect(options).toHaveCount(2)
await expect(
page.getByRole("menuitemradio", { name: "English" }),
).toBeVisible()
await page.getByRole("menuitemradio", { name: "Spanish" }).click()
await expect(page).toHaveURL(/\/login$/)
await expect(page.locator("html")).toHaveAttribute("lang", "es")
await expect(
page.getByRole("heading", { name: "Iniciar sesión" }),
).toBeVisible()
await expect(page.getByLabel("Usuario")).toBeVisible()
await expect(page.getByLabel("Contraseña")).toBeVisible()
await expect(
page.getByRole("button", { name: "Iniciar sesión" }),
).toBeVisible()
await expectLocaleCookie(page, "es")
})
test("switches the authenticated dashboard language from the navbar", async ({
baseURL,
page,
}) => {
await signInAsAdmin(page, baseURL)
await expect(page.locator("html")).toHaveAttribute("lang", "en")
await expect(page.getByRole("heading", { name: "Dashboard" })).toBeVisible()
await expect(page.getByText("E2E Admin")).toBeVisible()
await page.getByRole("button", { name: "Language: English" }).click()
await page.getByRole("menuitemradio", { name: "Spanish" }).click()
await expect(page).toHaveURL("/")
await expect(page.locator("html")).toHaveAttribute("lang", "es")
await expect(
page.getByRole("heading", { name: "Panel de control" }),
).toBeVisible()
await expect(page.getByRole("link", { name: /Inicio/ })).toBeVisible()
await page.getByRole("button", { name: /Inventario/ }).click()
await expect(page.getByRole("link", { name: /Artículos/ })).toBeVisible()
await expect(
page.getByRole("link", { name: /Destinatarios/ }),
).toBeVisible()
await expect(page.getByRole("link", { name: /Usuarios/ })).toBeVisible()
await page.getByRole("button", { name: "Añadir" }).click()
await expect(page.getByRole("menuitem", { name: /Importar/ })).toBeVisible()
await expect(
page.getByRole("menuitem", { name: /Categoría/ }),
).toBeVisible()
await expect(
page.getByRole("menuitem", { name: /Asignación/ }),
).toBeVisible()
await page.keyboard.press("Escape")
await expect(page.getByText("E2E Admin")).toBeVisible()
await page.getByText("E2E Admin").click()
await expect(page.getByText("Mi cuenta")).toBeVisible()
await expect(
page.getByRole("button", { name: "Cerrar sesión" }),
).toBeVisible()
await page.keyboard.press("Escape")
await page.goto("/admin/users")
await expect(page.getByPlaceholder("Buscar...")).toBeVisible()
await expect(page.getByRole("searchbox", { name: "Buscar" })).toBeVisible()
await page.goto("/forbidden")
await expect(
page.getByRole("heading", { name: "Acceso denegado" }),
).toBeVisible()
await expect(
page.getByText("No tienes permisos para acceder a esta sección."),
).toBeVisible()
await expect(
page.getByRole("link", { name: "Volver al inicio" }),
).toBeVisible()
await expectLocaleCookie(page, "es")
})
})