Skip to content

Commit aa4837d

Browse files
committed
Fix: Centralize blog link generation to handle external URLs correctly
- Created getBlogPostLink() function to centralize link generation logic - Posts with external_url now link directly to that URL instead of creating research links - Fixed SmallBlogPreview, MediumBlogPreview, and FeaturedBlogPreview components - Added comprehensive tests for external URL redirect behavior This fixes the issue where clicking OBBBA tile was going to /us/research/obbba-household-by-household-dummy instead of /us/obbba-household-by-household
1 parent 8f94bd2 commit aa4837d

2 files changed

Lines changed: 126 additions & 6 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
});

src/pages/home/HomeBlogPreview.jsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import { formatFullDate } from "../../lang/format";
99
import EmphasisedLink from "../../layout/EmphasisedLink";
1010
import { FileImageOutlined } from "@ant-design/icons";
1111

12+
// Centralized function to get the link for a blog post
13+
export function getBlogPostLink(blog, countryId) {
14+
// If the post has an external URL, use it directly
15+
if (blog.external_url) {
16+
return blog.external_url;
17+
}
18+
19+
// Otherwise, create a research page link from the slug
20+
const slug = blog.slug || (blog.filename ? blog.filename.split(".")[0] : "");
21+
return `/${countryId}/research/${slug}`;
22+
}
23+
1224
export default function HomeBlogPreview() {
1325
const countryId = useCountryId();
1426
const featuredPosts = posts
@@ -338,7 +350,7 @@ export function FeaturedBlogPreview({ blogs, width, imageHeight }) {
338350
const countryId = useCountryId();
339351
const postDate = formatFullDate(moment(currentBlog.date), countryId);
340352

341-
const link = `/${countryId}/research/${currentBlog.slug}`;
353+
const link = getBlogPostLink(currentBlog, countryId);
342354
return (
343355
<div
344356
style={{
@@ -403,8 +415,7 @@ export function FeaturedBlogPreview({ blogs, width, imageHeight }) {
403415
export function MediumBlogPreview({ blog, minHeight }) {
404416
const displayCategory = useDisplayCategory();
405417
const countryId = useCountryId();
406-
const slug = blog.filename ? blog.filename.split(".")[0] : blog.slug;
407-
const link = blog.external_url || `/${countryId}/research/${slug}`;
418+
const link = getBlogPostLink(blog, countryId);
408419

409420
const imageUrl = blog.image ? handleImageLoad(blog.image) : "";
410421

@@ -529,9 +540,7 @@ export function SmallBlogPreview({ blog }) {
529540
left = <SideTags tags={blog.tags} />;
530541
}
531542

532-
const slug = blog.filename.split(".")[0];
533-
const link = `/${countryId}/research/${slug}`;
534-
543+
const link = getBlogPostLink(blog, countryId);
535544
const postDate = formatFullDate(moment(blog.date), countryId);
536545

537546
return (

0 commit comments

Comments
 (0)