This commit is contained in:
+15
-1
@@ -1,8 +1,22 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { getLocalizedPathWithDefault } from "../lib/locale";
|
||||
import { FALLBACK_LOCALE, getLocalizedPathWithDefault, isSupportedLocale, resolveLocale } from "../lib/locale";
|
||||
|
||||
describe("locale path helpers", () => {
|
||||
it("recognizes supported locales explicitly", () => {
|
||||
expect(isSupportedLocale("ar")).toBe(true);
|
||||
expect(isSupportedLocale("en")).toBe(true);
|
||||
expect(isSupportedLocale("de")).toBe(true);
|
||||
expect(isSupportedLocale("fr")).toBe(false);
|
||||
expect(isSupportedLocale(undefined)).toBe(false);
|
||||
});
|
||||
|
||||
it("resolves invalid locales with an explicit fallback", () => {
|
||||
expect(resolveLocale("ar", "de")).toBe("ar");
|
||||
expect(resolveLocale("fr", "en")).toBe("en");
|
||||
expect(resolveLocale("", FALLBACK_LOCALE)).toBe("de");
|
||||
});
|
||||
|
||||
it("keeps the configured default locale on the bare domain", () => {
|
||||
expect(getLocalizedPathWithDefault("ar", "/", "ar")).toBe("/");
|
||||
expect(getLocalizedPathWithDefault("de", "/", "ar")).toBe("/de");
|
||||
|
||||
@@ -4,6 +4,7 @@ import { buildDefaultSiteSettings } from "../lib/site-settings";
|
||||
import {
|
||||
applyTitleTemplateFn,
|
||||
buildAppMetadataFromConfig,
|
||||
buildLocaleAlternates,
|
||||
buildLocalizedMetadataFromConfig,
|
||||
} from "../lib/metadata";
|
||||
|
||||
@@ -96,4 +97,44 @@ describe("metadata helpers", () => {
|
||||
|
||||
expect(metadata.title).toBe("اسم الموقع");
|
||||
});
|
||||
|
||||
it("builds alternates and canonical from the runtime default locale", () => {
|
||||
const alternates = buildLocaleAlternates("/about", "ar");
|
||||
|
||||
expect(alternates.canonical).toBe("https://mohfarawati.de/about");
|
||||
expect(alternates.languages.ar).toBe("https://mohfarawati.de/about");
|
||||
expect(alternates.languages.de).toBe("https://mohfarawati.de/de/about");
|
||||
expect(alternates.languages["x-default"]).toBe("https://mohfarawati.de/about");
|
||||
});
|
||||
|
||||
it("builds localized metadata urls against the configured default locale", () => {
|
||||
const settings = buildDefaultSiteSettings("Studio Moh");
|
||||
settings.defaultLocale = "ar";
|
||||
|
||||
const metadata = buildLocalizedMetadataFromConfig({
|
||||
settings,
|
||||
bindings: {
|
||||
siteLogoLight: null,
|
||||
siteLogoDark: null,
|
||||
favicon: null,
|
||||
defaultOgImage: null,
|
||||
},
|
||||
locale: "de",
|
||||
pathname: "/about",
|
||||
title: "About",
|
||||
});
|
||||
|
||||
expect(metadata.alternates).toMatchObject({
|
||||
canonical: "https://mohfarawati.de/about",
|
||||
languages: {
|
||||
ar: "https://mohfarawati.de/about",
|
||||
de: "https://mohfarawati.de/de/about",
|
||||
en: "https://mohfarawati.de/en/about",
|
||||
"x-default": "https://mohfarawati.de/about",
|
||||
},
|
||||
});
|
||||
expect(metadata.openGraph).toMatchObject({
|
||||
url: "https://mohfarawati.de/de/about",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const createMiddlewareMock = vi.fn();
|
||||
const intlHandlerMock = vi.fn(() => NextResponse.next());
|
||||
|
||||
vi.mock("next-intl/middleware", () => ({
|
||||
default: createMiddlewareMock,
|
||||
}));
|
||||
|
||||
vi.mock("../lib/admin-routing", () => ({
|
||||
fromDevelopmentAdminPath: (pathname: string) => pathname,
|
||||
getAdminBaseUrl: () => "https://admin.example.com",
|
||||
getRequestHostname: (_forwardedHost: string | null, host: string | null, hostname: string) => host ?? hostname,
|
||||
isDevelopmentAdminPath: () => false,
|
||||
isAdminHost: () => false,
|
||||
hasDedicatedAdminHost: () => false,
|
||||
isInternalAdminPath: () => false,
|
||||
isLegacyAdminPath: () => false,
|
||||
toInternalAdminPath: (pathname: string) => pathname,
|
||||
}));
|
||||
|
||||
function createMockRequest(url: string) {
|
||||
const nextUrl = new URL(url) as URL & { clone: () => URL };
|
||||
nextUrl.clone = () => new URL(nextUrl.toString());
|
||||
|
||||
return {
|
||||
url,
|
||||
nextUrl,
|
||||
headers: new Headers({
|
||||
host: nextUrl.host,
|
||||
}),
|
||||
cookies: {
|
||||
has: vi.fn(() => false),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("middleware locale runtime config", () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
createMiddlewareMock.mockReset();
|
||||
intlHandlerMock.mockReset();
|
||||
intlHandlerMock.mockReturnValue(NextResponse.next());
|
||||
createMiddlewareMock.mockReturnValue(intlHandlerMock);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("passes the runtime default locale into next-intl middleware", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => ({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
defaultLocale: "ar",
|
||||
maintenanceEnabled: false,
|
||||
}),
|
||||
})),
|
||||
);
|
||||
|
||||
const { default: middleware } = await import("../middleware");
|
||||
const request = createMockRequest("https://example.com/");
|
||||
|
||||
await middleware(request as never);
|
||||
|
||||
expect(createMiddlewareMock).toHaveBeenCalledTimes(1);
|
||||
expect(createMiddlewareMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
defaultLocale: "ar",
|
||||
}),
|
||||
);
|
||||
expect(intlHandlerMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("falls back safely when the runtime locale lookup fails", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => {
|
||||
throw new Error("network failed");
|
||||
}),
|
||||
);
|
||||
|
||||
const { default: middleware } = await import("../middleware");
|
||||
const request = createMockRequest("https://example.com/");
|
||||
|
||||
await middleware(request as never);
|
||||
|
||||
expect(createMiddlewareMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
defaultLocale: "de",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("redirects maintenance traffic using the runtime default locale", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => ({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
defaultLocale: "ar",
|
||||
maintenanceEnabled: true,
|
||||
}),
|
||||
})),
|
||||
);
|
||||
|
||||
const { default: middleware } = await import("../middleware");
|
||||
const request = createMockRequest("https://example.com/");
|
||||
|
||||
const response = await middleware(request as never);
|
||||
|
||||
expect(response.headers.get("location")).toBe("https://example.com/coming-soon");
|
||||
expect(intlHandlerMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user