Skip to content

Commit 4e9278e

Browse files
test(web): the diff loading boundary's observer counter lives in its fixture
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 03df289 commit 4e9278e

1 file changed

Lines changed: 24 additions & 21 deletions

File tree

‎apps/web/src/components/diffs/DiffFileLoadingBoundary.browser.test.tsx‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,34 @@ import { describe, expect, it, vi } from "vite-plus/test";
33
import { render } from "../../../test/render";
44
import { DiffFileLoadingBoundary } from "./DiffFileLoadingBoundary";
55

6-
// Like the real observer, every observe() reports the target's current state; this boundary
7-
// is always in range.
8-
class InRangeIntersectionObserver {
9-
static observeCount = 0;
10-
constructor(readonly callback: IntersectionObserverCallback) {}
11-
observe(target: Element) {
12-
InRangeIntersectionObserver.observeCount += 1;
13-
this.callback(
14-
[{ target, isIntersecting: true } as IntersectionObserverEntry],
15-
this as unknown as IntersectionObserver,
16-
);
17-
}
18-
unobserve() {}
19-
disconnect() {}
20-
}
21-
226
function stubIntersectionObserver() {
23-
InRangeIntersectionObserver.observeCount = 0;
7+
let observeCount = 0;
8+
// Like the real observer, every observe() reports the target's current state; this boundary
9+
// is always in range.
10+
class InRangeIntersectionObserver {
11+
constructor(readonly callback: IntersectionObserverCallback) {}
12+
observe(target: Element) {
13+
observeCount += 1;
14+
this.callback(
15+
[{ target, isIntersecting: true } as IntersectionObserverEntry],
16+
this as unknown as IntersectionObserver,
17+
);
18+
}
19+
unobserve() {}
20+
disconnect() {}
21+
}
2422
vi.stubGlobal("IntersectionObserver", InRangeIntersectionObserver);
25-
return { [Symbol.dispose]: () => vi.unstubAllGlobals() };
23+
return {
24+
get observeCount() {
25+
return observeCount;
26+
},
27+
[Symbol.dispose]: () => vi.unstubAllGlobals(),
28+
};
2629
}
2730

2831
describe("DiffFileLoadingBoundary", () => {
2932
it("observes once and loads once while its parent re-renders with a new load", async () => {
30-
using _observer = stubIntersectionObserver();
33+
using observer = stubIntersectionObserver();
3134
const load = vi.fn();
3235
// DiffPanel hands over a new loadNextFiles whenever any file patch changes.
3336
await using screen = await render(<DiffFileLoadingBoundary load={() => load()} count={8} />, {
@@ -38,12 +41,12 @@ describe("DiffFileLoadingBoundary", () => {
3841
await screen.rerender(<DiffFileLoadingBoundary load={() => load()} count={8} />);
3942

4043
await expect.element(screen.getByRole("status", { name: "Loading diff…" })).toBeInTheDocument();
41-
expect(InRangeIntersectionObserver.observeCount).toBe(1);
44+
expect(observer.observeCount).toBe(1);
4245
expect(load).toHaveBeenCalledTimes(1);
4346

4447
// A settled batch leaves the boundary in range, so it asks for the next one.
4548
await screen.rerender(<DiffFileLoadingBoundary load={() => load()} count={4} />);
46-
expect(InRangeIntersectionObserver.observeCount).toBe(1);
49+
expect(observer.observeCount).toBe(1);
4750
expect(load).toHaveBeenCalledTimes(2);
4851
});
4952
});

0 commit comments

Comments
 (0)