Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions front-end/tests/hashtag.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { test, expect } from "@playwright/test";

test("should not make infinite hashtag endpoint requests", async ({ page }) => {
// ===== ARRANGE
const requests = [];
const hashtag = "SwizBiz";

page.on("request", (request) => {
if (
request.url().includes(`:3000/hashtag/${hashtag}`) &&
request.resourceType() === "fetch"
) {
requests.push(request);
}
});
// ====== ACT
// When I navigate to the hashtag
await page.goto(`/#/hashtag/${hashtag}`);

// Wait for the hashtag API to respond before continuing
await page.waitForResponse(
(response) =>
response.url().includes(`/hashtag/${hashtag}`) &&
response.status() === 200,
);

// ====== ASSERT
// Then the number of requests should be 1
console.log("Number of requests:", requests.length);
expect(requests.length).toEqual(1);
});
25 changes: 20 additions & 5 deletions front-end/views/hashtag.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,37 @@ import {createHeading} from "../components/heading.mjs";
function hashtagView(hashtag) {
destroy();

apiService.getBloomsByHashtag(hashtag);
// Normalize the hashtag to always include a leading '#' and
// prevent infinite API calls by only fetching if the hashtag has changed

const normalizedHashtag = hashtag.startsWith("#") ? hashtag : `#${hashtag}`;
if (state.currentHashtag !== normalizedHashtag) {
// Update state only if the hashtag changed
state.currentHashtag = normalizedHashtag;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting choice - isn't there a problem here that we may show the title for the new hashtag but the content for the new hashtag, if we render again before the API request finishes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback — I understand the concern about showing the title for a new hashtag while the content might be from a previous hashtag if the API response hasn't returned yet.
To handle this, the code now checks that the hashtag in state is still current before using the API response, so old data is ignored and the view remains consistent.


// Fetch blooms for the new hashtag
apiService.getBloomsByHashtag(normalizedHashtag).then(()=>{
// Check that the hashtag is still current to avoid old data
if (state.currentHashtag === normalizedHashtag){
// Nothing to do here for now
}
});
}
renderOne(
state.isLoggedIn,
getLogoutContainer(),
"logout-template",
createLogout
createLogout,
);

document
.querySelector("[data-action='logout']")
?.addEventListener("click", handleLogout);
renderOne(
state.isLoggedIn,
getLoginContainer(),
"login-template",
createLogin
createLogin,
);
document
.querySelector("[data-action='login']")
Expand All @@ -42,13 +57,13 @@ function hashtagView(hashtag) {
state.currentHashtag,
getHeadingContainer(),
"heading-template",
createHeading
createHeading,
);
renderEach(
state.hashtagBlooms || [],
getTimelineContainer(),
"bloom-template",
createBloom
createBloom,
);
}

Expand Down