Skip to content
Open
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
49 changes: 39 additions & 10 deletions src/__tests__/RecyclerViewManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { RecyclerViewManager } from "../recyclerview/RecyclerViewManager";
import { WarningMessages } from "../errors/WarningMessages";
import { FlashListProps } from "../FlashListProps";

import {
createPopulatedLayoutManager,
LayoutManagerType,
} from "./helpers/createLayoutManager";

describe("RecyclerViewManager", () => {
let consoleWarnSpy: jest.SpyInstance;

Expand All @@ -13,18 +18,18 @@ describe("RecyclerViewManager", () => {
consoleWarnSpy.mockRestore();
});

describe("keyExtractor warning with maintainVisibleContentPosition", () => {
const createMockProps = (overrides = {}) =>
({
data: [{ id: 1 }, { id: 2 }, { id: 3 }],
renderItem: jest.fn(),
...overrides,
} as FlashListProps<unknown>);
const createMockProps = (overrides = {}) =>
({
data: [{ id: 1 }, { id: 2 }, { id: 3 }],
renderItem: jest.fn(),
...overrides,
} as FlashListProps<unknown>);

const createManager = (props: FlashListProps<unknown>) => {
return new RecyclerViewManager(props);
};
const createManager = (props: FlashListProps<unknown>) => {
return new RecyclerViewManager(props);
};

describe("keyExtractor warning with maintainVisibleContentPosition", () => {
it("should warn when onStartReached is defined but keyExtractor is not", () => {
const props = createMockProps({
onStartReached: jest.fn(),
Expand Down Expand Up @@ -71,4 +76,28 @@ describe("RecyclerViewManager", () => {
expect(consoleWarnSpy).not.toHaveBeenCalled();
});
});

describe("tryGetLayout boundary handling", () => {
it("should return undefined when no layoutManager has been initialized", () => {
const manager = createManager(createMockProps());

expect(manager.tryGetLayout(0)).toBeUndefined();
expect(manager.tryGetLayout(999)).toBeUndefined();
expect(manager.tryGetLayout(-1)).toBeUndefined();
});

it("should return undefined for an index beyond the current layout count", () => {
const manager = createManager(createMockProps());
const layoutManager = createPopulatedLayoutManager(
LayoutManagerType.LINEAR,
5
);
(manager as any).layoutManager = layoutManager;

expect(manager.tryGetLayout(2)).toBeDefined();
expect(manager.tryGetLayout(5)).toBeUndefined();
expect(manager.tryGetLayout(10)).toBeUndefined();
expect(manager.tryGetLayout(-1)).toBeUndefined();
});
});
});
5 changes: 4 additions & 1 deletion src/recyclerview/RecyclerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,10 @@ const RecyclerViewComponent = <T,>(
*/
const validateItemSize = useCallback(
(index: number, size: RVDimension) => {
const layout = recyclerViewManager.getLayout(index);
const layout = recyclerViewManager.tryGetLayout(index);
if (!layout) {
return;
}
const width = Math.max(
Math.min(layout.width, layout.maxWidth ?? Infinity),
layout.minWidth ?? 0
Expand Down
Loading