Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __tests__/__mocks__/createMockState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export function createMockState(
horizontal: false,
initialContainerPoolRatio: 2,
initialScroll: undefined,
initialScrollAtWindowEnd: false,
itemsAreEqual: undefined,
keyExtractor: (_: any, index: number) => `item_${index}`,
maintainScrollAtEnd: undefined,
Expand Down
39 changes: 39 additions & 0 deletions __tests__/components/ListComponentScrollView.web.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const addEventListener = mock((type: string, listener: ScrollListener) => {
const removeEventListener = mock((type: string) => {
scrollListeners.delete(type);
});
const scrollToTarget = mock((_options?: ScrollToOptions) => {});
const schedule = mock(() => true);
const flush = mock(() => {});
const cancel = mock(() => {});
Expand All @@ -37,6 +38,7 @@ function registerWebScrollMocks() {
mock.module("../../src/components/webScrollUtils", () => ({
clampOffset: (offset: number) => offset,
getContentSize: () => ({ height: 0, width: 0 }),
getDocumentMaxOffset: () => 444,
getElementDocumentPosition: () => ({ left: 0, top: 0 }),
getLayoutMeasurement: () => ({ height: 0, width: 0 }),
getLayoutRectangle: () => ({ height: 0, width: 0, x: 0, y: 0 }),
Expand All @@ -47,6 +49,7 @@ function registerWebScrollMocks() {
resolveScrollEventTarget: () => ({
addEventListener,
removeEventListener,
scrollTo: scrollToTarget,
}),
resolveWindowScrollTarget: () => ({ left: 0, top: 0 }),
}));
Expand All @@ -56,6 +59,7 @@ function resetMocks() {
scrollListeners.clear();
addEventListener.mockClear();
removeEventListener.mockClear();
scrollToTarget.mockClear();
schedule.mockClear();
flush.mockClear();
cancel.mockClear();
Expand Down Expand Up @@ -140,4 +144,39 @@ describe("ListComponentScrollView (web)", () => {
});
}
});

it("scrolls the document to its end for initial window-end requests", async () => {
resetMocks();
const ref = { current: null as any };
const { ListComponentScrollView } = await import(
"../../src/components/ListComponentScrollView?web-scroll-window-end"
);
let renderer: TestRenderer.ReactTestRenderer | undefined;

try {
act(() => {
renderer = TestRenderer.create(
<ListComponentScrollView
initialScrollAtWindowEnd
onLayout={() => {}}
ref={ref as any}
style={{}}
useWindowScroll
>
<div />
</ListComponentScrollView>,
);
});

act(() => {
ref.current.scrollTo({ animated: false, initialScrollAtWindowEnd: true, y: 120 });
});

expect(scrollToTarget).toHaveBeenCalledWith({ behavior: "auto", left: 0, top: 444 });
} finally {
act(() => {
renderer?.unmount();
});
}
});
});
12 changes: 9 additions & 3 deletions __tests__/core/bootstrapInitialScroll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ describe("bootstrapInitialScroll", () => {
Platform.OS = "android";

const data = Array.from({ length: 8 }, (_, index) => ({ id: `item-${index}` }));
const scrollToCalls: Array<{ animated: boolean; x: number; y: number }> = [];
const scrollToCalls: Array<{ animated: boolean; initialScrollAtWindowEnd?: boolean; x: number; y: number }> =
[];
const ctx = createMockContext(
{
totalSize: 800,
Expand Down Expand Up @@ -441,7 +442,12 @@ describe("bootstrapInitialScroll", () => {
refScroller: {
current: {
getScrollableNode: () => ({}),
scrollTo: (params: { animated: boolean; x: number; y: number }) => scrollToCalls.push(params),
scrollTo: (params: {
animated: boolean;
initialScrollAtWindowEnd?: boolean;
x: number;
y: number;
}) => scrollToCalls.push(params),
},
} as StateContext["state"]["refScroller"],
scrollLength: 200,
Expand All @@ -457,7 +463,7 @@ describe("bootstrapInitialScroll", () => {
evaluateBootstrapInitialScroll(ctx);
evaluateBootstrapInitialScroll(ctx);

expect(scrollToCalls).toEqual([{ animated: false, x: 0, y: 500 }]);
expect(scrollToCalls).toEqual([{ animated: false, initialScrollAtWindowEnd: false, x: 0, y: 500 }]);
expect(ctx.state.didFinishInitialScroll).toBe(false);
expect(ctx.state.initialScroll).toMatchObject({
contentOffset: 500,
Expand Down
21 changes: 21 additions & 0 deletions __tests__/core/scrollTo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ describe("scrollTo", () => {
expect(doScrollToSpy).toHaveBeenCalledWith(mockCtx, {
animated: true,
horizontal: false,
initialScrollAtWindowEnd: false,
isInitialScroll: true,
offset: 140,
});
Expand Down Expand Up @@ -158,6 +159,7 @@ describe("scrollTo", () => {
expect(doScrollToSpy).toHaveBeenCalledWith(mockCtx, {
animated: false,
horizontal: false,
initialScrollAtWindowEnd: false,
isInitialScroll: true,
offset: 0,
});
Expand All @@ -169,4 +171,23 @@ describe("scrollTo", () => {
targetOffset: 0,
});
});

it("passes initialScrollAtWindowEnd to forced initial scroll dispatches", () => {
mockCtx.state.props.initialScrollAtWindowEnd = true;

scrollTo(mockCtx, {
animated: false,
forceScroll: true,
isInitialScroll: true,
offset: 140,
});

expect(doScrollToSpy).toHaveBeenCalledWith(mockCtx, {
animated: false,
horizontal: false,
initialScrollAtWindowEnd: true,
isInitialScroll: true,
offset: 140,
});
});
});
Loading