Skip to content

Commit 998d840

Browse files
committed
test: add more tests
1 parent 4ffc4a2 commit 998d840

File tree

4 files changed

+98
-51
lines changed

4 files changed

+98
-51
lines changed

__tests__/SegmentedControl.test.tsx

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
import * as React from 'react';
2-
import { fireEvent, render, wait } from '@testing-library/react-native';
1+
import React from 'react';
2+
import { View } from 'react-native';
3+
import { fireEvent, render } from '@testing-library/react-native';
34
import { SegmentedControl } from '../src/SegmentedControl';
45
import { Segment } from '../src/Segment';
56

67
jest.mock('react-native-reanimated', () =>
78
require('react-native-reanimated/mock'),
89
);
910

11+
jest.mock('react-native-gesture-handler', () => {
12+
const actual = jest.requireActual('react-native-gesture-handler');
13+
const { TouchableOpacity } = jest.requireActual('react-native');
14+
return {
15+
...actual,
16+
TouchableOpacity,
17+
};
18+
});
19+
1020
describe('SegmentedControl', () => {
1121
it('should render', () => {
1222
const { asJSON } = render(
@@ -18,6 +28,33 @@ describe('SegmentedControl', () => {
1828
expect(asJSON()).toMatchSnapshot();
1929
});
2030

31+
it('should render initially without slider, press on a segment and slider should appear', async () => {
32+
const { getByTestId, getByText } = render(
33+
<SegmentedControl>
34+
<Segment name="first" content="First" />
35+
<Segment name="second" content="Second" />
36+
</SegmentedControl>,
37+
);
38+
39+
expect(() => getByTestId('SegmentedControl_Slider')).toThrow();
40+
41+
const secondSegment = getByText('Second');
42+
fireEvent.press(secondSegment);
43+
44+
expect(getByTestId('SegmentedControl_Slider')).toBeDefined();
45+
});
46+
47+
it('should render initially with slider on `Second`', async () => {
48+
const { getByTestId } = render(
49+
<SegmentedControl initialSelectedName="second">
50+
<Segment name="first" content="First" />
51+
<Segment name="second" content="Second" />
52+
</SegmentedControl>,
53+
);
54+
55+
expect(getByTestId('SegmentedControl_Slider')).toBeDefined();
56+
});
57+
2158
it('should call onChangeValue when pressed on `Test`', async () => {
2259
const changeValueSpy = jest.fn();
2360
const { getByTestId } = render(
@@ -29,9 +66,36 @@ describe('SegmentedControl', () => {
2966
const button = getByTestId('Segment_Button');
3067
fireEvent.press(button);
3168

32-
await wait();
33-
34-
// TODO: Test not working.. button isnt being pressed from RNGH
3569
expect(changeValueSpy).toBeCalledWith('Test');
3670
});
71+
72+
it('should throw if a child component is not a Segment', () => {
73+
const errorSpy = jest.spyOn(console, 'error');
74+
errorSpy.mockImplementation();
75+
76+
expect(() => {
77+
render(
78+
<SegmentedControl>
79+
<View />
80+
</SegmentedControl>,
81+
);
82+
}).toThrow('SegmentedControl only accepts Segment as children.');
83+
84+
errorSpy.mockRestore();
85+
});
86+
87+
it('should throw if a Segment has no name', () => {
88+
const errorSpy = jest.spyOn(console, 'error');
89+
errorSpy.mockImplementation();
90+
91+
expect(() => {
92+
render(
93+
<SegmentedControl>
94+
<Segment name="" content="No Name" />
95+
</SegmentedControl>,
96+
);
97+
}).toThrow('Segment requires `name` to be defined.');
98+
99+
errorSpy.mockRestore();
100+
});
37101
});

__tests__/__snapshots__/SegmentedControl.test.tsx.snap

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -38,60 +38,42 @@ exports[`SegmentedControl should render 1`] = `
3838
]
3939
}
4040
>
41-
<RNGestureHandlerButton
42-
collapsable={false}
43-
rippleColor={0}
44-
style={
45-
Array [
46-
Object {
47-
"overflow": "hidden",
48-
},
49-
undefined,
50-
]
51-
}
41+
<TouchableOpacity
42+
activeOpacity={0.2}
43+
testID="Segment_Button"
5244
>
5345
<View
54-
accessible={true}
5546
style={
5647
Object {
57-
"opacity": 1,
48+
"alignItems": "center",
49+
"flex": 1,
50+
"flexDirection": "row",
51+
"justifyContent": "center",
5852
}
5953
}
60-
testID="Segment_Button"
6154
>
62-
<View
55+
<Text
56+
numberOfLines={1}
6357
style={
64-
Object {
65-
"alignItems": "center",
66-
"flex": 1,
67-
"flexDirection": "row",
68-
"justifyContent": "center",
69-
}
58+
Array [
59+
Object {
60+
"fontSize": 13,
61+
"paddingLeft": 2,
62+
"paddingRight": 2,
63+
"textAlign": "center",
64+
"width": "100%",
65+
},
66+
Object {
67+
"color": "#000000",
68+
},
69+
false,
70+
]
7071
}
7172
>
72-
<Text
73-
numberOfLines={1}
74-
style={
75-
Array [
76-
Object {
77-
"fontSize": 13,
78-
"paddingLeft": 2,
79-
"paddingRight": 2,
80-
"textAlign": "center",
81-
"width": "100%",
82-
},
83-
Object {
84-
"color": "#000000",
85-
},
86-
false,
87-
]
88-
}
89-
>
90-
Test
91-
</Text>
92-
</View>
73+
Test
74+
</Text>
9375
</View>
94-
</RNGestureHandlerButton>
76+
</TouchableOpacity>
9577
</View>
9678
</View>
9779
</View>

src/Segment/Segment.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useContext } from 'react';
1+
import React, { useContext, FC } from 'react';
22
import { StyleProp, Text, View, ViewStyle } from 'react-native';
33
import { TouchableOpacity } from 'react-native-gesture-handler';
44
import { SegmentedContext } from '../SegmentedControl/SegmentedControl';
@@ -12,13 +12,13 @@ export interface SegmentProps {
1212
style?: StyleProp<ViewStyle>;
1313
}
1414

15-
export const Segment = ({
15+
export const Segment: FC<SegmentProps> = ({
1616
activeTintColor,
1717
content,
1818
inactiveTintColor,
1919
name,
2020
style,
21-
}: SegmentProps): JSX.Element => {
21+
}: SegmentProps) => {
2222
const context = useContext(SegmentedContext);
2323

2424
if (!context) {

src/SegmentedControl/SegmentedControl.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ export const SegmentedControl = ({
139139
>
140140
{typeof _activeName !== 'undefined' && (
141141
<Animated.View
142+
testID="SegmentedControl_Slider"
142143
style={[
143144
styles.slider,
144145
{

0 commit comments

Comments
 (0)