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
23 changes: 23 additions & 0 deletions front-end/tests/hashtag.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { test, expect } from "@playwright/test";

test("should not make infinite hashtag endpoint requests", async ({ page }) => {
// ===== ARRANGE
const requests = [];
page.on("request", (request) => {
if (
request.url().includes(":3000/hashtag/do") &&
request.resourceType() === "fetch"
) {
requests.push(request);
}
});
// ====== ACT
// When I navigate to the hashtag
await page.goto("/#/hashtag/do");
// And I wait a reasonable time for any additional requests
await page.waitForTimeout(200);

// ====== ASSERT
// Then the number of requests should be 1
expect(requests.length).toEqual(1);
});
Comment on lines +1 to +23
Copy link

Choose a reason for hiding this comment

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

Excellent addition of a regression test!

18 changes: 11 additions & 7 deletions front-end/views/hashtag.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {renderOne, renderEach, destroy} from "../lib/render.mjs";
import { renderOne, renderEach, destroy } from "../lib/render.mjs";
import {
state,
apiService,
Expand All @@ -7,17 +7,21 @@ import {
getTimelineContainer,
getHeadingContainer,
} from "../index.mjs";
import {createLogin, handleLogin} from "../components/login.mjs";
import {createLogout, handleLogout} from "../components/logout.mjs";
import {createBloom} from "../components/bloom.mjs";
import {createHeading} from "../components/heading.mjs";
import { createLogin, handleLogin } from "../components/login.mjs";
import { createLogout, handleLogout } from "../components/logout.mjs";
import { createBloom } from "../components/bloom.mjs";
import { createHeading } from "../components/heading.mjs";

// Hashtag view: show all tweets containing this tag

function hashtagView(hashtag) {
destroy();

apiService.getBloomsByHashtag(hashtag);
// Check that hashtag starts with hash to make right comparison with state
const hashtagWithHash = hashtag.startsWith("#") ? hashtag : `#${hashtag}`;
if (state.currentHashtag !== hashtagWithHash) {
apiService.getBloomsByHashtag(hashtag);
}
Comment on lines +20 to +24
Copy link

Choose a reason for hiding this comment

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

You've correctly identified the root cause - the function was calling the API every time without checking if it already had the data. Your solution to check state.currentHashtag !== hashtagWithHash before making the API call is exactly right! This prevents the infinite loop that was causing the browser to flash.

Nice attention to detail on the edge case check! You noticed that hashtags might come in with or without the # prefix, and you normalized it to ensure the comparison works correctly. This shows you're thinking about real-world usage scenarios.


renderOne(
state.isLoggedIn,
Expand Down Expand Up @@ -52,4 +56,4 @@ function hashtagView(hashtag) {
);
}

export {hashtagView};
export { hashtagView };