Skip to content

Commit 65a27c8

Browse files
committed
feat: Add virtual list mock for test usage
1 parent c4e9bd5 commit 65a27c8

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/List.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const ScrollStyle = {
1919
overflowAnchor: 'none',
2020
};
2121

22-
type RenderFunc<T> = (
22+
export type RenderFunc<T> = (
2323
item: T,
2424
index: number,
2525
props: { style: React.CSSProperties },

src/mock.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as React from 'react';
2+
import OriginList from './List';
3+
import Filler from './Filler';
4+
5+
class List<T> extends OriginList<T> {
6+
render() {
7+
const {
8+
style,
9+
component: Component = 'div',
10+
height,
11+
itemHeight,
12+
data,
13+
children,
14+
itemKey,
15+
onSkipRender,
16+
...restProps
17+
} = this.props;
18+
19+
return (
20+
<Component style={{ ...style, height }} {...restProps}>
21+
<Filler height={height}>{this.renderChildren(data, 0, children)}</Filler>
22+
</Component>
23+
);
24+
}
25+
}
26+
27+
export default List;

src/utils/itemUtil.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ export function getElementScrollPercentage(element: HTMLElement | null) {
7979
* But if not provided, downgrade to `findDOMNode` to get the real dom element.
8080
*/
8181
export function getNodeHeight(node: HTMLElement) {
82-
if (!node) {
83-
return 0;
84-
}
85-
8682
return findDOMNode(node).offsetHeight;
8783
}
8884

tests/mock.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import MockList from '../src/mock';
4+
import Filler from '../src/Filler';
5+
6+
describe('MockList', () => {
7+
it('correct render', () => {
8+
const wrapper = mount(
9+
<MockList data={[0, 1, 2]} itemKey={id => id}>
10+
{id => <span>{id}</span>}
11+
</MockList>,
12+
);
13+
14+
expect(wrapper.find(Filler).length).toBeTruthy();
15+
16+
for (let i = 0; i < 3; i += 1) {
17+
expect(
18+
wrapper
19+
.find('span')
20+
.at(i)
21+
.key(),
22+
).toBe(String(i));
23+
}
24+
});
25+
});

0 commit comments

Comments
 (0)