1 parent
0c8d74b21d
commit
a1e51e78f0
23 files changed
+522
-516
No files matched your search
+66
-1
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { meta, MetaBuilder } from "./meta"
|
||||
import { meta, MetaBuilder, buildMetadata } from "./meta"
|
||||
|
||||
describe("meta", () => {
|
||||
it("should return defaults when called with empty object", () => {
|
||||
@@ -59,3 +59,68 @@ describe("MetaBuilder", () => {
|
||||
expect(result.type).toBe("article")
|
||||
})
|
||||
})
|
||||
|
||||
describe("buildMetadata", () => {
|
||||
const defaultMeta = meta({})
|
||||
|
||||
it("should map title and description", () => {
|
||||
const result = buildMetadata(defaultMeta)
|
||||
expect(result.title).toBe("Шлиссельбургская ДХШ")
|
||||
expect(result.description).toBe("Сайт Шлиссельбургской художественной школы")
|
||||
})
|
||||
|
||||
it("should map openGraph fields", () => {
|
||||
const result = buildMetadata(defaultMeta)
|
||||
const og = result.openGraph
|
||||
expect(og).toBeDefined()
|
||||
expect(og).toMatchObject({
|
||||
url: "https://art.shlisselburg.org/",
|
||||
title: "Шлиссельбургская ДХШ",
|
||||
description: "Сайт Шлиссельбургской художественной школы",
|
||||
siteName: "Шлиссельбургская Детская Художественная Школа",
|
||||
locale: "ru_RU",
|
||||
type: "website",
|
||||
})
|
||||
})
|
||||
|
||||
it("should map openGraph images", () => {
|
||||
const result = buildMetadata(defaultMeta)
|
||||
const images = (result.openGraph as { images: unknown[] }).images
|
||||
expect(images).toHaveLength(1)
|
||||
expect(images[0]).toEqual({
|
||||
url: "https://art.shlisselburg.org/entrance.jpg",
|
||||
width: 1200,
|
||||
height: 630,
|
||||
})
|
||||
})
|
||||
|
||||
it("should map twitter fields", () => {
|
||||
const result = buildMetadata(defaultMeta)
|
||||
expect(result.twitter).toEqual({
|
||||
card: "summary_large_image",
|
||||
site: "@",
|
||||
creator: "@tmshv",
|
||||
})
|
||||
})
|
||||
|
||||
it("should use custom values from MetaBuilder", () => {
|
||||
const m = new MetaBuilder()
|
||||
.setTitle("Custom Page")
|
||||
.setDescription("A custom description")
|
||||
.setImage({ src: "https://example.com/cover.jpg", width: 800, height: 400 })
|
||||
.setData({ url: "/custom" })
|
||||
.build()
|
||||
|
||||
const result = buildMetadata(m)
|
||||
expect(result.title).toBe("Custom Page")
|
||||
expect(result.description).toBe("A custom description")
|
||||
|
||||
const og = result.openGraph as { url: string, images: { url: string, width: number, height: number }[] }
|
||||
expect(og.url).toBe("https://art.shlisselburg.org/custom")
|
||||
expect(og.images[0]).toEqual({
|
||||
url: "https://example.com/cover.jpg",
|
||||
width: 800,
|
||||
height: 400,
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { Metadata } from "next"
|
||||
import type { Meta, Pic } from "@/types"
|
||||
|
||||
export function meta(props: Partial<Meta>): Meta {
|
||||
@@ -80,3 +81,24 @@ export class MetaBuilder {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function buildMetadata(m: Meta): Metadata {
|
||||
return {
|
||||
title: m.title,
|
||||
description: m.description,
|
||||
openGraph: {
|
||||
url: m.url,
|
||||
title: m.title,
|
||||
description: m.description,
|
||||
siteName: m.siteName,
|
||||
locale: m.locale,
|
||||
type: m.type as "website",
|
||||
images: [{ url: m.image, width: m.imageWidth, height: m.imageHeight }],
|
||||
},
|
||||
twitter: {
|
||||
card: m.twitterCard as "summary_large_image",
|
||||
site: m.twitterSite,
|
||||
creator: m.twitterCreator,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user