From e7e9cc5ef7303b8ec82353f05b9775aaaef73a5d Mon Sep 17 00:00:00 2001 From: Droid-An Date: Thu, 2 Oct 2025 18:52:45 +0100 Subject: [PATCH] fixed redundant api calls --- front-end/tests/hashtag.spec.mjs | 23 +++++++++++++++++++++++ front-end/views/hashtag.mjs | 18 +++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 front-end/tests/hashtag.spec.mjs diff --git a/front-end/tests/hashtag.spec.mjs b/front-end/tests/hashtag.spec.mjs new file mode 100644 index 0000000..f1403b7 --- /dev/null +++ b/front-end/tests/hashtag.spec.mjs @@ -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); +}); diff --git a/front-end/views/hashtag.mjs b/front-end/views/hashtag.mjs index 7b7e996..7ff202f 100644 --- a/front-end/views/hashtag.mjs +++ b/front-end/views/hashtag.mjs @@ -1,4 +1,4 @@ -import {renderOne, renderEach, destroy} from "../lib/render.mjs"; +import { renderOne, renderEach, destroy } from "../lib/render.mjs"; import { state, apiService, @@ -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); + } renderOne( state.isLoggedIn, @@ -52,4 +56,4 @@ function hashtagView(hashtag) { ); } -export {hashtagView}; +export { hashtagView };