|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Josimar Silva |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +import config from "./config"; |
| 26 | +import { generateMetadata } from "./metadata"; |
| 27 | +import { getPostBySlug } from "./posts"; |
| 28 | + |
| 29 | +jest.mock("./posts", () => ({ |
| 30 | + getPostBySlug: jest.fn(), |
| 31 | +})); |
| 32 | + |
| 33 | +jest.mock("./config", () => ({ |
| 34 | + __esModule: true, |
| 35 | + default: { |
| 36 | + siteUrl: "https://example.com", |
| 37 | + }, |
| 38 | +})); |
| 39 | + |
| 40 | +describe("generateMetadata", () => { |
| 41 | + const mockPost = { |
| 42 | + title: "Test Post Title", |
| 43 | + excerpt: "Test Post Excerpt", |
| 44 | + image: "/assets/test-image.jpg", |
| 45 | + date: "2025-01-01T00:00:00Z", |
| 46 | + author: "Test Author", |
| 47 | + slug: "test-post-slug", |
| 48 | + }; |
| 49 | + |
| 50 | + it("should generate correct metadata for a post", async () => { |
| 51 | + (getPostBySlug as jest.Mock).mockResolvedValue(mockPost); |
| 52 | + |
| 53 | + const metadata = await generateMetadata({ |
| 54 | + params: { slug: "test-post-slug" }, |
| 55 | + }); |
| 56 | + |
| 57 | + expect(metadata.title).toBe(mockPost.title); |
| 58 | + expect(metadata.description).toBe(mockPost.excerpt); |
| 59 | + expect(metadata.openGraph?.title).toBe(mockPost.title); |
| 60 | + expect(metadata.openGraph?.description).toBe(mockPost.excerpt); |
| 61 | + expect(metadata.openGraph?.url).toBe( |
| 62 | + `${config.siteUrl}/blog/${mockPost.slug}`, |
| 63 | + ); |
| 64 | + expect(metadata.openGraph?.type).toBe("article"); |
| 65 | + expect(metadata.openGraph?.publishedTime).toBe(mockPost.date); |
| 66 | + expect(metadata.openGraph?.authors).toEqual([mockPost.author]); |
| 67 | + expect(metadata.openGraph?.images?.[0].url).toBe( |
| 68 | + new URL(mockPost.image, config.siteUrl).toString(), |
| 69 | + ); |
| 70 | + expect(metadata.openGraph?.images?.[0].width).toBe(1200); |
| 71 | + expect(metadata.openGraph?.images?.[0].height).toBe(630); |
| 72 | + expect(metadata.openGraph?.images?.[0].alt).toBe(mockPost.title); |
| 73 | + |
| 74 | + expect(metadata.twitter?.card).toBe("summary_large_image"); |
| 75 | + expect(metadata.twitter?.title).toBe(mockPost.title); |
| 76 | + expect(metadata.twitter?.description).toBe(mockPost.excerpt); |
| 77 | + expect(metadata.twitter?.images?.[0]).toBe( |
| 78 | + new URL(mockPost.image, config.siteUrl).toString(), |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should return empty metadata if post is not found", async () => { |
| 83 | + (getPostBySlug as jest.Mock).mockResolvedValue(null); |
| 84 | + |
| 85 | + const metadata = await generateMetadata({ |
| 86 | + params: { slug: "non-existent-post" }, |
| 87 | + }); |
| 88 | + |
| 89 | + expect(metadata).toEqual({}); |
| 90 | + }); |
| 91 | +}); |
0 commit comments