Skip to content

Commit 0f3ffe9

Browse files
committed
Add/Fix tests
1 parent 2c90e58 commit 0f3ffe9

File tree

4 files changed

+92
-28
lines changed

4 files changed

+92
-28
lines changed

src/FlashListProps.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ export interface ListRenderItemInfo<TItem> {
2323
extraData?: any;
2424
}
2525

26+
export interface OverrideProps {
27+
initialDrawBatchSize?: number;
28+
// rest can be string to any
29+
[key: string]: any;
30+
}
31+
2632
export type RenderTarget = "Cell" | "StickyHeader" | "Measurement";
2733

2834
export const RenderTargetOptions: Record<string, RenderTarget> = {
@@ -293,7 +299,7 @@ export interface FlashListProps<TItem>
293299
/**
294300
* For debugging and exception use cases, internal props will be overriden with these values if used
295301
*/
296-
overrideProps?: object;
302+
overrideProps?: OverrideProps;
297303

298304
/**
299305
* Set this when offset is needed for the loading indicator to show correctly.

src/__tests__/RecyclerView.test.tsx

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,92 @@ jest.mock("../recyclerview/utils/measureLayout", () => {
3333
};
3434
});
3535

36+
const renderRecyclerView = (args: {
37+
numColumns?: number;
38+
masonry?: boolean;
39+
horizontal?: boolean;
40+
}) => {
41+
const { numColumns = 1, masonry = false, horizontal = false } = args;
42+
return render(
43+
<RecyclerView
44+
data={[
45+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
46+
20, 21,
47+
]}
48+
masonry={masonry}
49+
overrideProps={{ initialDrawBatchSize: 1 }}
50+
drawDistance={0}
51+
numColumns={numColumns}
52+
horizontal={horizontal}
53+
renderItem={({ item }) => <Text>{item}</Text>}
54+
/>
55+
);
56+
};
57+
3658
describe("RecyclerView", () => {
3759
beforeEach(() => {
3860
jest.clearAllMocks();
3961
jest.useFakeTimers();
4062
});
63+
describe("Linear Layout", () => {
64+
it("renders items ", () => {
65+
const result = renderRecyclerView({});
66+
67+
expect(result).toContainReactComponent(Text, { children: 0 });
68+
expect(result).not.toContainReactComponent(Text, { children: 11 });
69+
});
70+
});
71+
72+
describe("Masonry Layout", () => {
73+
it("renders items with masonry", () => {
74+
const result = renderRecyclerView({ masonry: true });
75+
76+
expect(result).toContainReactComponent(Text, { children: 0 });
77+
});
78+
it("should not render item 20, 21 with numColumns 2", () => {
79+
const result = renderRecyclerView({ numColumns: 2, masonry: true });
80+
81+
expect(result).toContainReactComponent(Text, {
82+
children: 17,
83+
});
84+
expect(result).not.toContainReactComponent(Text, {
85+
children: 20,
86+
});
87+
88+
expect(result).not.toContainReactComponent(Text, {
89+
children: 21,
90+
});
91+
});
92+
});
93+
94+
describe("Grid Layout", () => {
95+
it("renders items with numColumns 2", () => {
96+
const result = renderRecyclerView({ numColumns: 2 });
97+
98+
expect(result).toContainReactComponent(Text, { children: 0 });
99+
});
100+
it("should not render item 18, 19 with numColumns 2", () => {
101+
const result = renderRecyclerView({ numColumns: 2 });
102+
103+
expect(result).toContainReactComponent(Text, {
104+
children: 17,
105+
});
106+
expect(result).not.toContainReactComponent(Text, {
107+
children: 20,
108+
});
109+
110+
expect(result).not.toContainReactComponent(Text, {
111+
children: 21,
112+
});
113+
});
114+
});
41115

42-
it("renders items ", () => {
43-
const result = render(
44-
<RecyclerView
45-
data={[
46-
"One",
47-
"Two",
48-
"Three",
49-
"Four",
50-
"Five",
51-
"Six",
52-
"Seven",
53-
"Eight",
54-
"Nine",
55-
"Ten",
56-
"Eleven",
57-
"Twelve",
58-
"Thirteen",
59-
"Fourteen",
60-
"Fifteen",
61-
]}
62-
renderItem={({ item }) => <Text>{item}</Text>}
63-
/>
64-
);
116+
describe("Horizontal Layout", () => {
117+
it("renders items with horizontal", () => {
118+
const result = renderRecyclerView({ horizontal: true });
65119

66-
expect(result).toContainReactComponent(Text, { children: "One" });
67-
expect(result).not.toContainReactComponent(Text, { children: "Eleven" });
120+
expect(result).toContainReactComponent(Text, { children: 0 });
121+
expect(result).not.toContainReactComponent(Text, { children: 5 });
122+
});
68123
});
69124
});

src/recyclerview/RecyclerViewManager.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ export class RecyclerViewManager<T> {
117117
} else {
118118
this.initialDrawBatchSize = (props.numColumns ?? 1) * 2;
119119
}
120+
this.initialDrawBatchSize =
121+
this.propsRef.overrideProps?.initialDrawBatchSize ??
122+
this.initialDrawBatchSize;
120123
}
121124

122125
/**

src/recyclerview/layout-managers/LayoutManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ export abstract class RVLayoutManager {
114114
/**
115115
* Gets indices of items currently visible in the viewport.
116116
* Uses binary search for efficient lookup.
117-
* @param unboundDimensionStart Start position of viewport
118-
* @param unboundDimensionEnd End position of viewport
117+
* @param unboundDimensionStart Start position of viewport (start X or start Y)
118+
* @param unboundDimensionEnd End position of viewport (end X or end Y)
119119
* @returns ConsecutiveNumbers containing visible indices
120120
*/
121121
getVisibleLayouts(

0 commit comments

Comments
 (0)