diff --git a/.vscode/settings.json b/.vscode/settings.json index 166be53..547ec79 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ { "liveServer.settings.root": "front-end/", - "python.defaultInterpreterPath": "backend/.venv/bin/python" + "python.defaultInterpreterPath": "backend/.venv/bin/python", + "liveServer.settings.port": 5501 } diff --git a/front-end/tests/hashtag.spec.mjs b/front-end/tests/hashtag.spec.mjs new file mode 100644 index 0000000..06cca83 --- /dev/null +++ b/front-end/tests/hashtag.spec.mjs @@ -0,0 +1,24 @@ +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"); + //Wait for the UI to show the blooms have loaded + await page.locator("[data-bloom]").first().waitFor(); + + // ====== ASSERT + // Then the number of requests should be 1 + expect(requests.length).toEqual(1); +}); \ No newline at end of file diff --git a/front-end/views/hashtag.mjs b/front-end/views/hashtag.mjs index 7b7e996..cae20bb 100644 --- a/front-end/views/hashtag.mjs +++ b/front-end/views/hashtag.mjs @@ -14,10 +14,16 @@ import {createHeading} from "../components/heading.mjs"; // Hashtag view: show all tweets containing this tag -function hashtagView(hashtag) { - destroy(); - - apiService.getBloomsByHashtag(hashtag); + function hashtagView(hashtag) { + destroy();// Tear down any previously rendered view before drawing this one. + const formattedHashtag = hashtag.startsWith('#') ? hashtag : `#${hashtag}`;// Ensure the hashtag always has a leading '#' so comparisons are reliable. + + if (state.currentHashtag !== formattedHashtag) { + state.currentHashtag = formattedHashtag; + apiService.getBloomsByHashtag(formattedHashtag); + } + + renderOne( state.isLoggedIn, @@ -50,6 +56,6 @@ function hashtagView(hashtag) { "bloom-template", createBloom ); -} + } export {hashtagView};