Skip to content

Commit f67c60b

Browse files
committed
test: add more tests for coverage
1 parent 998d840 commit f67c60b

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

__tests__/Segment.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { Text } from 'react-native';
23
import { render } from '@testing-library/react-native';
34
import { SegmentedControl, Segment } from '../src';
45

@@ -27,4 +28,24 @@ describe('SegmentedControl', () => {
2728

2829
expect(getByText('Test')).toBeDefined();
2930
});
31+
32+
it('should render when content is a function', () => {
33+
const { getByText } = render(
34+
<SegmentedControl>
35+
<Segment name="Test" content={() => <Text>Function</Text>} />
36+
</SegmentedControl>,
37+
);
38+
39+
expect(getByText('Function')).toBeDefined();
40+
});
41+
42+
it('should render when content is an element', () => {
43+
const { getByText } = render(
44+
<SegmentedControl>
45+
<Segment name="Test" content={<Text>Element</Text>} />
46+
</SegmentedControl>,
47+
);
48+
49+
expect(getByText('Element')).toBeDefined();
50+
});
3051
});

__tests__/SegmentedControl.test.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React from 'react';
2-
import { View } from 'react-native';
1+
import React, { ReactElement } from 'react';
2+
import { Text, View } from 'react-native';
33
import { fireEvent, render } from '@testing-library/react-native';
44
import { SegmentedControl } from '../src/SegmentedControl';
5-
import { Segment } from '../src/Segment';
5+
import { Segment, SegmentContentProps } from '../src/Segment';
66

77
jest.mock('react-native-reanimated', () =>
88
require('react-native-reanimated/mock'),
@@ -45,14 +45,23 @@ describe('SegmentedControl', () => {
4545
});
4646

4747
it('should render initially with slider on `Second`', async () => {
48+
let activeSegment = null;
49+
50+
const SpyContent = ({ active }: SegmentContentProps): ReactElement => {
51+
activeSegment = active;
52+
53+
return <Text>Second</Text>;
54+
};
55+
4856
const { getByTestId } = render(
4957
<SegmentedControl initialSelectedName="second">
5058
<Segment name="first" content="First" />
51-
<Segment name="second" content="Second" />
59+
<Segment name="second" content={SpyContent} />
5260
</SegmentedControl>,
5361
);
5462

5563
expect(getByTestId('SegmentedControl_Slider')).toBeDefined();
64+
expect(activeSegment).toBe(true);
5665
});
5766

5867
it('should call onChangeValue when pressed on `Test`', async () => {

src/Segment/Segment.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { TouchableOpacity } from 'react-native-gesture-handler';
44
import { SegmentedContext } from '../SegmentedControl/SegmentedControl';
55
import styles from './SegmentStyles';
66

7+
export interface SegmentContentProps {
8+
activeTintColor: string;
9+
inactiveTintColor: string;
10+
active: boolean;
11+
}
12+
713
export interface SegmentProps {
814
activeTintColor?: string;
915
content: React.ReactNode;

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { SegmentedControl, SegmentedControlProps } from './SegmentedControl';
2-
export { Segment, SegmentProps } from './Segment';
2+
export { Segment, SegmentProps, SegmentContentProps } from './Segment';

0 commit comments

Comments
 (0)