Files
stock-manager/tests/unit/actions/i18n.actions.test.ts
T

51 lines
1.4 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest"
import { LOCALE_COOKIE_MAX_AGE_SECONDS } from "@/i18n/locales"
const headersMocks = vi.hoisted(() => ({
cookieSet: vi.fn(),
cookies: vi.fn(),
}))
vi.mock("next/headers", () => ({
cookies: headersMocks.cookies,
}))
import { setLocaleAction } from "@/actions/i18n.actions"
describe("setLocaleAction", () => {
beforeEach(() => {
headersMocks.cookieSet.mockReset()
headersMocks.cookies.mockReset()
headersMocks.cookies.mockResolvedValue({
set: headersMocks.cookieSet,
})
})
it("writes a validated supported locale to the locale cookie", async () => {
const result = await setLocaleAction("es")
expect(result).toEqual({ success: true, locale: "es" })
expect(headersMocks.cookies).toHaveBeenCalledOnce()
expect(headersMocks.cookieSet).toHaveBeenCalledWith(
"stock-manager-locale",
"es",
{
path: "/",
sameSite: "lax",
maxAge: LOCALE_COOKIE_MAX_AGE_SECONDS,
httpOnly: true,
secure: false,
},
)
})
it("rejects unsupported locales without writing a cookie", async () => {
const result = await setLocaleAction("fr")
expect(result).toEqual({ success: false, error: "UNSUPPORTED_LOCALE" })
expect(headersMocks.cookies).not.toHaveBeenCalled()
expect(headersMocks.cookieSet).not.toHaveBeenCalled()
})
})