|
| 1 | +import React from "react"; |
| 2 | +import { render } from "@testing-library/react"; |
| 3 | +import { MemoryRouter } from "react-router-dom"; |
| 4 | +import { |
| 5 | + MediumBlogPreview, |
| 6 | + SmallBlogPreview, |
| 7 | + FeaturedBlogPreview, |
| 8 | +} from "../../pages/home/HomeBlogPreview"; |
| 9 | +import posts from "../../posts/posts.json"; |
| 10 | + |
| 11 | +// Mock the image loader |
| 12 | +jest.mock( |
| 13 | + "../../images/posts/obbba-household-by-household.png", |
| 14 | + () => "mocked-image", |
| 15 | + { virtual: true }, |
| 16 | +); |
| 17 | + |
| 18 | +// Mock the postTransformers to ensure slug is set |
| 19 | +jest.mock("../../posts/postTransformers", () => { |
| 20 | + const actualPosts = require("../../posts/posts.json"); |
| 21 | + const postsSorted = actualPosts.sort((a, b) => (a.date < b.date ? 1 : -1)); |
| 22 | + |
| 23 | + for (let post of postsSorted) { |
| 24 | + if (post.filename) { |
| 25 | + post.slug = post.filename.substring(0, post.filename.indexOf(".")); |
| 26 | + } else if (post.external_url) { |
| 27 | + post.slug = post.title |
| 28 | + .toLowerCase() |
| 29 | + .replace(/\s+/g, "-") |
| 30 | + .replace(/[^a-z0-9-]/g, ""); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return { |
| 35 | + posts: postsSorted, |
| 36 | + locationLabels: {}, |
| 37 | + locationTags: [], |
| 38 | + topicLabels: {}, |
| 39 | + topicTags: [], |
| 40 | + }; |
| 41 | +}); |
| 42 | + |
| 43 | +describe("External URL redirect behavior", () => { |
| 44 | + const obbbaPost = posts.find( |
| 45 | + (post) => post.external_url === "/us/obbba-household-by-household", |
| 46 | + ); |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + // Ensure the post has a slug for testing |
| 50 | + if (obbbaPost && !obbbaPost.slug) { |
| 51 | + obbbaPost.slug = "obbba-household-by-household-dummy"; |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + test("MediumBlogPreview should link directly to external URL", () => { |
| 56 | + expect(obbbaPost).toBeDefined(); |
| 57 | + |
| 58 | + const { container } = render( |
| 59 | + <MemoryRouter> |
| 60 | + <MediumBlogPreview blog={obbbaPost} minHeight={300} /> |
| 61 | + </MemoryRouter>, |
| 62 | + ); |
| 63 | + |
| 64 | + const linkElement = container.querySelector("a"); |
| 65 | + expect(linkElement).toBeTruthy(); |
| 66 | + expect(linkElement.getAttribute("href")).toBe( |
| 67 | + "/us/obbba-household-by-household", |
| 68 | + ); |
| 69 | + expect(linkElement.getAttribute("href")).not.toBe( |
| 70 | + "/us/research/obbba-household-by-household-dummy", |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + test("SmallBlogPreview should link directly to external URL", () => { |
| 75 | + expect(obbbaPost).toBeDefined(); |
| 76 | + |
| 77 | + const { container } = render( |
| 78 | + <MemoryRouter> |
| 79 | + <SmallBlogPreview blog={obbbaPost} /> |
| 80 | + </MemoryRouter>, |
| 81 | + ); |
| 82 | + |
| 83 | + const linkElement = container.querySelector("a"); |
| 84 | + expect(linkElement).toBeTruthy(); |
| 85 | + expect(linkElement.getAttribute("href")).toBe( |
| 86 | + "/us/obbba-household-by-household", |
| 87 | + ); |
| 88 | + expect(linkElement.getAttribute("href")).not.toBe( |
| 89 | + "/us/research/obbba-household-by-household-dummy", |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + test("FeaturedBlogPreview should link directly to external URL", () => { |
| 94 | + expect(obbbaPost).toBeDefined(); |
| 95 | + |
| 96 | + const { container } = render( |
| 97 | + <MemoryRouter> |
| 98 | + <FeaturedBlogPreview blogs={obbbaPost} width="100%" imageHeight={200} /> |
| 99 | + </MemoryRouter>, |
| 100 | + ); |
| 101 | + |
| 102 | + const linkElement = container.querySelector("a"); |
| 103 | + expect(linkElement).toBeTruthy(); |
| 104 | + expect(linkElement.getAttribute("href")).toBe( |
| 105 | + "/us/obbba-household-by-household", |
| 106 | + ); |
| 107 | + expect(linkElement.getAttribute("href")).not.toBe( |
| 108 | + "/us/research/obbba-household-by-household-dummy", |
| 109 | + ); |
| 110 | + }); |
| 111 | +}); |
0 commit comments