This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
buildDefaultSiteSettings,
|
||||
parseSiteSettingsValue,
|
||||
} from "../lib/site-settings";
|
||||
|
||||
describe("site settings helpers", () => {
|
||||
it("builds defaults from the fallback site name", () => {
|
||||
const settings = buildDefaultSiteSettings("Studio Moh");
|
||||
|
||||
expect(settings.locales.ar.siteName).toBe("Studio Moh");
|
||||
expect(settings.locales.ar.titleTemplate).toBe("{pageTitle} | {siteName}");
|
||||
expect(settings.locales.en.siteDescription).toBe("Multilingual Next.js base project");
|
||||
expect(settings.locales.de.subhead).toBe("");
|
||||
});
|
||||
|
||||
it("merges stored values with safe defaults", () => {
|
||||
const settings = parseSiteSettingsValue(
|
||||
JSON.stringify({
|
||||
locales: {
|
||||
en: {
|
||||
siteName: "Brand EN",
|
||||
titleTemplate: "{pageTitle} - {siteName}",
|
||||
siteDescription: "English description",
|
||||
},
|
||||
de: {
|
||||
siteName: "Brand DE",
|
||||
titleTemplate: "{pageTitle} | {siteName} | Freelancer",
|
||||
subhead: "German subhead",
|
||||
},
|
||||
},
|
||||
}),
|
||||
"Fallback Name",
|
||||
);
|
||||
|
||||
expect(settings.locales.en.siteName).toBe("Brand EN");
|
||||
expect(settings.locales.en.titleTemplate).toBe("{pageTitle} - {siteName}");
|
||||
expect(settings.locales.en.siteDescription).toBe("English description");
|
||||
expect(settings.locales.en.subhead).toBe("");
|
||||
expect(settings.locales.ar.siteName).toBe("Fallback Name");
|
||||
expect(settings.locales.ar.titleTemplate).toBe("{pageTitle} | {siteName}");
|
||||
expect(settings.locales.de.siteDescription).toBe("Multilingual Next.js base project");
|
||||
expect(settings.locales.de.titleTemplate).toBe("{pageTitle} | {siteName} | Freelancer");
|
||||
expect(settings.locales.de.subhead).toBe("German subhead");
|
||||
});
|
||||
|
||||
it("falls back when stored json is invalid", () => {
|
||||
const settings = parseSiteSettingsValue("{invalid-json", "Fallback Name");
|
||||
|
||||
expect(settings).toEqual(buildDefaultSiteSettings("Fallback Name"));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,85 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { buildDefaultSiteSettings } from "../lib/site-settings";
|
||||
import {
|
||||
applyTitleTemplateFn,
|
||||
buildAppMetadataFromConfig,
|
||||
buildLocalizedMetadataFromConfig,
|
||||
} from "../lib/metadata";
|
||||
|
||||
describe("metadata helpers", () => {
|
||||
it("applies the configured title template", () => {
|
||||
expect(applyTitleTemplateFn("About", "{pageTitle} | {siteName}", "Studio Moh")).toBe(
|
||||
"About | Studio Moh",
|
||||
);
|
||||
expect(applyTitleTemplateFn("About", "Studio Moh", "Studio Moh")).toBe("About | Studio Moh");
|
||||
});
|
||||
|
||||
it("builds root metadata with dynamic icons and social preview", () => {
|
||||
const settings = buildDefaultSiteSettings("Studio Moh");
|
||||
const metadata = buildAppMetadataFromConfig(settings, {
|
||||
favicon: {
|
||||
assetId: "fav",
|
||||
url: "/uploads/media/site-settings/favicon.svg",
|
||||
},
|
||||
defaultOgImage: {
|
||||
assetId: "og",
|
||||
url: "/uploads/media/site-settings/default-og.png",
|
||||
},
|
||||
});
|
||||
|
||||
expect(metadata.title).toBe("Studio Moh");
|
||||
expect(metadata.icons).toEqual({
|
||||
icon: ["/uploads/media/site-settings/favicon.svg"],
|
||||
shortcut: ["/uploads/media/site-settings/favicon.svg"],
|
||||
apple: ["/uploads/media/site-settings/favicon.svg"],
|
||||
});
|
||||
expect(metadata.twitter).toMatchObject({
|
||||
card: "summary_large_image",
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back to localized site description and omits icons when unset", () => {
|
||||
const settings = buildDefaultSiteSettings("Studio Moh");
|
||||
settings.locales.en.siteDescription = "English fallback description";
|
||||
settings.locales.en.titleTemplate = "{pageTitle} | {siteName} | Freelancer";
|
||||
const metadata = buildLocalizedMetadataFromConfig({
|
||||
settings,
|
||||
bindings: {
|
||||
favicon: null,
|
||||
defaultOgImage: null,
|
||||
},
|
||||
locale: "en",
|
||||
pathname: "/about",
|
||||
title: "About",
|
||||
});
|
||||
|
||||
expect(metadata.title).toBe("About | Studio Moh | Freelancer");
|
||||
expect(metadata.description).toBe("English fallback description");
|
||||
expect(metadata.openGraph?.siteName).toBe("Studio Moh");
|
||||
expect(metadata.twitter).toMatchObject({
|
||||
card: "summary",
|
||||
});
|
||||
});
|
||||
|
||||
it("can skip the title template for the homepage", () => {
|
||||
const settings = buildDefaultSiteSettings("Studio Moh");
|
||||
settings.locales.ar.siteName = "اسم الموقع";
|
||||
settings.locales.ar.titleTemplate = "{pageTitle} | {siteName}";
|
||||
|
||||
const metadata = buildLocalizedMetadataFromConfig({
|
||||
settings,
|
||||
bindings: {
|
||||
favicon: null,
|
||||
defaultOgImage: null,
|
||||
},
|
||||
locale: "ar",
|
||||
pathname: "/",
|
||||
title: "اسم الموقع",
|
||||
description: "وصف",
|
||||
applyTitleTemplate: false,
|
||||
});
|
||||
|
||||
expect(metadata.title).toBe("اسم الموقع");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user