Skip to content

Commit 58f878d

Browse files
authored
WheelPicker - render tests (#1688)
* add render tests * pr fixes * scrollViewHelper * PR fixes
1 parent a38dab3 commit 58f878d

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

jest-setup.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {NativeModules, AccessibilityInfo} from 'react-native';
22

3+
require('./node_modules/react-native-reanimated/src/reanimated2/jestUtils').setUpTests();
4+
35
NativeModules.StatusBarManager = {getHeight: jest.fn()};
46
jest.spyOn(AccessibilityInfo, 'isScreenReaderEnabled').mockImplementation(() => new Promise.resolve(false));
57

src/incubator/WheelPicker/Item.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const WheelPickerItem = memo(({
7474
>
7575
<AnimatedText
7676
text60R
77+
testID={`${testID}.text`}
7778
numberOfLines={1}
7879
style={[animatedColorStyle, style, fakeLabel ? styles.textWithLabelPadding : styles.textPadding]}
7980
>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import _ from 'lodash';
2+
import React from 'react';
3+
import {WheelPicker} from '../../../incubator';
4+
import {Colors} from 'style';
5+
import {render} from '@testing-library/react-native';
6+
import {fireOnMomentumScrollEnd} from '../../../uilib-test-renderer';
7+
8+
const ITEM_HEIGHT = 50;
9+
const NUM_OF_ROWS = 7;
10+
const onChangeMock = jest.fn();
11+
12+
const TestCase = props => {
13+
return (
14+
<WheelPicker
15+
testID={'wheel'}
16+
items={_.times(60, i => i).map(item => ({label: `${item}`, value: item}))}
17+
onChange={onChangeMock}
18+
numberOfVisibleRows={NUM_OF_ROWS}
19+
itemHeight={ITEM_HEIGHT}
20+
initialIndex={0}
21+
activeTextColor={Colors.red30}
22+
inactiveTextColor={Colors.blue30}
23+
{...props}
24+
/>
25+
);
26+
};
27+
28+
describe('WheelPicker', () => {
29+
describe('FlatList', () => {
30+
it('should call onChange callback after scrolling', () => {
31+
const {getByTestId} = render(<TestCase/>);
32+
const flatList = getByTestId('wheel.list');
33+
34+
fireOnMomentumScrollEnd(flatList, {y: 200});
35+
36+
expect(onChangeMock).toHaveBeenCalledWith(4, 4);
37+
38+
fireOnMomentumScrollEnd(flatList, {y: 330});
39+
40+
expect(onChangeMock).toHaveBeenCalledWith(7, 7);
41+
});
42+
43+
it('should present 7 rows', () => {
44+
const {getByTestId} = render(<TestCase/>);
45+
const flatList = getByTestId('wheel.list');
46+
47+
expect(flatList.props.height).toBe(NUM_OF_ROWS * ITEM_HEIGHT);
48+
});
49+
});
50+
51+
//TODO: make this test work
52+
// describe('Item', () => {
53+
// it('should set activeColor to the selected index', () => {
54+
// const {getByTestId} = render(<TestCase/>);
55+
// const flatList = getByTestId('wheel.list');
56+
// const item_0 = getByTestId('wheel.item_0.text');
57+
// const item_4 = getByTestId('wheel.item_4.text');
58+
// const activeStyle = {color: Colors.red30};
59+
// const inactiveStyle = {color: Colors.blue30};
60+
61+
// expect(item_0).toHaveAnimatedStyle(activeStyle);
62+
// expect(item_4).toHaveAnimatedStyle(inactiveStyle);
63+
64+
// fireEvent(flatList, 'onMomentumScrollEnd', {nativeEvent: {contentOffset: {y: 200}}});
65+
66+
// expect(item_0).toHaveAnimatedStyle(inactiveStyle);
67+
// expect(item_4).toHaveAnimatedStyle(activeStyle);
68+
// });
69+
// });
70+
});

src/uilib-test-renderer/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
export * from '@testing-library/react-native';
22
import {findStyle} from './helper';
3+
import {fireOnMomentumScrollEnd} from './scrollViewHelper';
34

45
export {
5-
findStyle
6+
findStyle,
7+
fireOnMomentumScrollEnd
68
};
79

810
/* Docs: https://callstack.github.io/react-native-testing-library/docs/api/ */
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {fireEvent} from '@testing-library/react-native';
2+
3+
const fireOnMomentumScrollEnd = (component: any, {x, y}: {x?: number; y?: number}) => {
4+
fireEvent(component, 'onMomentumScrollEnd', getNativeEvent({x, y}));
5+
};
6+
7+
const getNativeEvent = ({x, y}: {x?: number; y?: number}) => {
8+
return {nativeEvent: {contentOffset: {x, y}}};
9+
};
10+
11+
export {fireOnMomentumScrollEnd};

0 commit comments

Comments
 (0)