-
Notifications
You must be signed in to change notification settings - Fork 7
Stop the feed new-posts poll running in background tabs #1563
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
apps/web/src/specs/app/feed/feed-poll-visibility.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import React from "react"; | ||
| import { act, type RenderResult } from "@testing-library/react"; | ||
| import "@testing-library/jest-dom"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { QueryClient } from "@tanstack/react-query"; | ||
| import { getQueryClient } from "@/core/react-query"; | ||
| import { renderWithQueryClient } from "@/specs/test-utils"; | ||
| import type { Entry } from "@/entities"; | ||
|
|
||
| const fetchSpy = vi.hoisted(() => vi.fn(async () => [] as Entry[])); | ||
|
|
||
| vi.mock("@/utils", async () => ({ | ||
| ...(await vi.importActual<typeof import("@/utils")>("@/utils")), | ||
| random: vi.fn(), | ||
| getAccessToken: vi.fn(() => "mock-token") | ||
| })); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| vi.mock("@ecency/sdk", async () => ({ | ||
| ...(await vi.importActual<Record<string, unknown>>("@ecency/sdk")), | ||
| getPostsRankedQueryOptions: vi.fn(() => ({ | ||
| queryKey: ["posts", "ranked-page", "poll"], | ||
| queryFn: fetchSpy | ||
|
qodo-code-review[bot] marked this conversation as resolved.
Outdated
|
||
| })) | ||
| })); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| vi.mock("@/api/queries", async () => ({ | ||
| ...(await vi.importActual<Record<string, unknown>>("@/api/queries")), | ||
| usePostsFeedQuery: () => ({ data: undefined, isFetching: false }) | ||
| })); | ||
| vi.mock("@/features/shared/entry-list-content", () => ({ EntryListContent: () => null })); | ||
| vi.mock("@/features/shared/linear-progress", () => ({ LinearProgress: () => null })); | ||
| vi.mock("@/features/shared/user-avatar", () => ({ UserAvatar: () => null })); | ||
|
|
||
| import { FeedLayout } from "@/app/(dynamicPages)/feed/_components/feed-layout"; | ||
|
|
||
| const ORIGINAL_VISIBILITY = Object.getOwnPropertyDescriptor( | ||
| Document.prototype, | ||
| "visibilityState" | ||
| ); | ||
|
|
||
| function setVisibility(state: "visible" | "hidden"): void { | ||
| Object.defineProperty(document, "visibilityState", { value: state, configurable: true }); | ||
| document.dispatchEvent(new Event("visibilitychange")); | ||
| } | ||
|
|
||
| function restoreVisibility(): void { | ||
| // The property is defined on Document.prototype, not on the instance, so the | ||
| // test-defined own property has to be deleted or it leaks into later suites. | ||
| delete (document as unknown as Record<string, unknown>).visibilityState; | ||
| if (ORIGINAL_VISIBILITY) { | ||
| Object.defineProperty(Document.prototype, "visibilityState", ORIGINAL_VISIBILITY); | ||
| } | ||
| } | ||
|
|
||
| function renderFeed(): RenderResult { | ||
| return renderWithQueryClient( | ||
| <FeedLayout tag="" filter="trending" observer="ecency"> | ||
| <div /> | ||
| </FeedLayout>, | ||
| { | ||
| queryClient: new QueryClient({ | ||
| defaultOptions: { queries: { retry: false, staleTime: 0, gcTime: 0 } } | ||
| }) | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * The poll fetches a full 20-post ranked page, 257 KB gzipped on /trending, and | ||
| * it runs for anonymous readers too. A background tab has nobody to show the | ||
| * "new posts" chip to, so it should not be paying for one. | ||
| */ | ||
| describe("feed poll", () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| fetchSpy.mockClear(); | ||
| // FeedLayout polls through getQueryClient(), the module-level client, not | ||
| // the one the provider holds. Its cache outlives a test, and with the poll's | ||
| // staleTime a leftover entry would satisfy the next test's fetch and make | ||
| // these assertions depend on execution order. | ||
| getQueryClient().clear(); | ||
| setVisibility("visible"); | ||
| }); | ||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| vi.clearAllMocks(); | ||
| getQueryClient().clear(); | ||
| restoreVisibility(); | ||
| }); | ||
|
|
||
| it("polls while the tab is visible", async () => { | ||
| renderFeed(); | ||
| await act(async () => { | ||
| await vi.advanceTimersByTimeAsync(31_000); | ||
| }); | ||
| expect(fetchSpy).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not poll while the tab is hidden", async () => { | ||
| renderFeed(); | ||
| setVisibility("hidden"); | ||
| fetchSpy.mockClear(); | ||
|
|
||
| await act(async () => { | ||
| await vi.advanceTimersByTimeAsync(5 * 60_000); | ||
| }); | ||
|
|
||
| expect(fetchSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("catches up as soon as the reader comes back", async () => { | ||
| renderFeed(); | ||
| setVisibility("hidden"); | ||
| await act(async () => { | ||
| await vi.advanceTimersByTimeAsync(5 * 60_000); | ||
| }); | ||
| fetchSpy.mockClear(); | ||
|
|
||
| await act(async () => { | ||
| setVisibility("visible"); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| }); | ||
|
|
||
| // No waiting out the rest of an interval for a chip that is already stale. | ||
| expect(fetchSpy).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("stops listening once the feed unmounts", async () => { | ||
| const { unmount } = renderFeed(); | ||
| unmount(); | ||
| fetchSpy.mockClear(); | ||
|
|
||
| await act(async () => { | ||
| setVisibility("visible"); | ||
| await vi.advanceTimersByTimeAsync(2 * 60_000); | ||
| }); | ||
|
|
||
| expect(fetchSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not touch state from a poll still in flight at unmount", async () => { | ||
| // clearInterval stops the next tick but not one already awaiting fetchQuery. | ||
| let release: (v: Entry[]) => void = () => {}; | ||
| fetchSpy.mockImplementationOnce( | ||
| () => new Promise<Entry[]>((resolve) => { release = resolve; }) | ||
| ); | ||
| const errors: unknown[] = []; | ||
| const onError = (e: ErrorEvent) => errors.push(e.error ?? e.message); | ||
| window.addEventListener("error", onError); | ||
|
|
||
| const { unmount } = renderFeed(); | ||
| await act(async () => { | ||
| await vi.advanceTimersByTimeAsync(31_000); | ||
| }); | ||
| unmount(); | ||
|
|
||
| await act(async () => { | ||
| release([{ author: "alice", permlink: "p", stats: {} } as unknown as Entry]); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| }); | ||
|
|
||
| window.removeEventListener("error", onError); | ||
| expect(errors).toEqual([]); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.