-
-
Couldn't load subscription status.
- Fork 17
London | 25-SDC-July | Andrei Filippov | Sprint 1 | Bug Report: Hashtag slowing down my browser #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| }); | ||
| 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, | ||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
|
@@ -52,4 +56,4 @@ function hashtagView(hashtag) { | |
| ); | ||
| } | ||
|
|
||
| export {hashtagView}; | ||
| export { hashtagView }; | ||
There was a problem hiding this comment.
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!