Skip to content

Commit d9725bf

Browse files
committed
Updated unit test for 100% coverage - [Galeel]
1 parent f262b7b commit d9725bf

7 files changed

+483
-61
lines changed

jest.config.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ module.exports = {
4040
// ],
4141

4242
// An object that configures minimum threshold enforcement for coverage results
43-
// coverageThreshold: null,
43+
coverageThreshold: {
44+
"global": {
45+
"statements": 100,
46+
"branches": 100,
47+
"functions": 100,
48+
"lines": 100
49+
}
50+
},
4451

4552
// Make calling deprecated APIs throw helpful error messages
4653
// errorOnDeprecated: false,

src/SegmentedControlTab.js

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { View, ViewPropTypes, StyleSheet, Text } from 'react-native'
33
import PropTypes from 'prop-types'
44
import TabOption from './TabOption'
55

6-
const handleTabPress = (index, selectedIndex, onTabPress) => {
7-
selectedIndex !== index && onTabPress(index)
6+
export const handleTabPress = (index, selectedIndex, onTabPress) => {
7+
selectedIndex !== index && onTabPress(index)
88
}
99

10-
const getAccessibilityLabelByIndex = (accessibilityLabels, index) => {
10+
export const getAccessibilityLabelByIndex = (accessibilityLabels, index) => {
1111
return accessibilityLabels && accessibilityLabels.length > 0 && accessibilityLabels[index] ? accessibilityLabels[index] : undefined
1212
}
1313

@@ -67,56 +67,60 @@ const SegmentedControlTab = ({
6767
}
6868

6969
SegmentedControlTab.propTypes = {
70-
accessibilityLabels: PropTypes.arrayOf(PropTypes.string),
71-
accessible: PropTypes.bool,
72-
activeTabBadgeContainerStyle: Text.propTypes.style,
73-
activeTabBadgeStyle: Text.propTypes.style,
74-
activeTabStyle: ViewPropTypes.style,
75-
activeTabTextStyle: Text.propTypes.style,
76-
allowFontScaling: PropTypes.bool,
77-
badges: PropTypes.arrayOf(PropTypes.number),
78-
borderRadius: PropTypes.number,
79-
onTabPress: PropTypes.func,
80-
selectedIndex: PropTypes.number,
81-
tabBadgeContainerStyle: Text.propTypes.style,
82-
tabBadgeStyle: Text.propTypes.style,
83-
tabStyle: ViewPropTypes.style,
84-
tabTextStyle: Text.propTypes.style,
85-
tabsContainerStyle: ViewPropTypes.style,
86-
testIDs: PropTypes.arrayOf(PropTypes.string),
87-
textNumberOfLines: PropTypes.number,
88-
values: PropTypes.arrayOf(PropTypes.string),
70+
accessibilityLabels: PropTypes.arrayOf(PropTypes.string),
71+
accessible: PropTypes.bool,
72+
activeTabBadgeContainerStyle: Text.propTypes.style,
73+
activeTabBadgeStyle: Text.propTypes.style,
74+
activeTabStyle: ViewPropTypes.style,
75+
activeTabTextStyle: Text.propTypes.style,
76+
allowFontScaling: PropTypes.bool,
77+
badges: PropTypes.arrayOf(
78+
PropTypes.oneOfType([
79+
PropTypes.string,
80+
PropTypes.number
81+
])),
82+
borderRadius: PropTypes.number,
83+
onTabPress: PropTypes.func,
84+
selectedIndex: PropTypes.number,
85+
tabBadgeContainerStyle: Text.propTypes.style,
86+
tabBadgeStyle: Text.propTypes.style,
87+
tabStyle: ViewPropTypes.style,
88+
tabTextStyle: Text.propTypes.style,
89+
tabsContainerStyle: ViewPropTypes.style,
90+
testIDs: PropTypes.arrayOf(PropTypes.string),
91+
textNumberOfLines: PropTypes.number,
92+
values: PropTypes.arrayOf(PropTypes.string),
8993
}
9094

9195
SegmentedControlTab.defaultProps = {
92-
values: ['One', 'Two', 'Three'],
93-
accessible: true,
94-
accessibilityLabels: [],
95-
testIDs: [],
96-
badges: [],
97-
selectedIndex: 0,
98-
onTabPress: () => { },
99-
tabsContainerStyle: {},
100-
tabStyle: {},
101-
activeTabStyle: {},
102-
tabTextStyle: {},
103-
activeTabTextStyle: {},
104-
tabBadgeContainerStyle: {},
105-
activeTabBadgeContainerStyle: {},
106-
tabBadgeStyle: {},
107-
activeTabBadgeStyle: {},
108-
borderRadius: 5,
109-
textNumberOfLines: 1,
110-
allowFontScaling: true,
96+
values: ['One', 'Two', 'Three'],
97+
accessible: true,
98+
accessibilityLabels: [],
99+
testIDs: [],
100+
badges: [],
101+
selectedIndex: 0,
102+
onTabPress: () => { },
103+
tabsContainerStyle: {},
104+
tabStyle: {},
105+
activeTabStyle: {},
106+
tabTextStyle: {},
107+
activeTabTextStyle: {},
108+
tabBadgeContainerStyle: {},
109+
activeTabBadgeContainerStyle: {},
110+
tabBadgeStyle: {},
111+
activeTabBadgeStyle: {},
112+
borderRadius: 5,
113+
textNumberOfLines: 1,
114+
allowFontScaling: true,
111115
}
112116

113117
const styles = StyleSheet.create({
114-
tabsContainerStyle: {
115-
backgroundColor: 'transparent',
116-
flexDirection: 'row',
117-
borderColor: '#007AFF',
118-
borderWidth: 1,
119-
},
118+
tabsContainerStyle: {
119+
backgroundColor: 'transparent',
120+
flexDirection: 'row',
121+
borderColor: '#007AFF',
122+
borderWidth: 1,
123+
},
120124
})
121125

122126
export default SegmentedControlTab

src/TabOption.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const TabOption = ({
4040
{text}
4141
</Text>
4242
{
43-
badge ?
43+
badge !== undefined &&
4444
<View style={[
4545
styles.tabBadgeContainerStyle,
4646
tabBadgeContainerStyle,
@@ -52,7 +52,7 @@ const TabOption = ({
5252
allowFontScaling={allowFontScaling}>
5353
{badge}
5454
</Text>
55-
</View> : false
55+
</View>
5656
}
5757
</View>
5858
</TouchableOpacity>
Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,75 @@
1-
// __tests__/Intro-test.js
21
import React from 'react';
3-
import SegmentedControlTab from '../SegmentedControlTab';
2+
import SegmentedControlTab, {handleTabPress, getAccessibilityLabelByIndex} from '../SegmentedControlTab';
43
jest.mock("../TabOption", () => 'TabOption')
54

65
import renderer from 'react-test-renderer';
76

8-
test('renders correctly', () => {
9-
const tree = renderer.create(<SegmentedControlTab />).toJSON();
10-
expect(tree).toMatchSnapshot();
7+
describe('SegmentedControlTab', () => {
8+
afterEach(() => {
9+
jest.resetAllMocks()
10+
});
11+
it('renders correctly', () => {
12+
const tree = renderer.create(<SegmentedControlTab />).toJSON();
13+
expect(tree).toMatchSnapshot();
14+
});
15+
16+
it('shoudl call defaultProps of onTabPress ', () => {
17+
const tree = renderer.create(<SegmentedControlTab selectedIndex={1} />).toJSON();
18+
tree.children[0].props.onTabPress()
19+
expect(tree).toMatchSnapshot();
20+
});
21+
22+
it('should call the given function when onTabPress is invoked ', () => {
23+
const tabIndex = 0
24+
const selectedIndex = 1
25+
const callbackFunc = jest.fn()
26+
const tree = renderer.create(<SegmentedControlTab selectedIndex={selectedIndex}
27+
onTabPress={callbackFunc} />).toJSON();
28+
tree.children[tabIndex].props.onTabPress(tabIndex)
29+
expect(callbackFunc).toHaveBeenCalledWith(tabIndex)
30+
});
31+
32+
it('should pass given testIDs to child component', () => {
33+
const tree = renderer.create(<SegmentedControlTab testIDs={['id-1','id-2','id-3']} />).toJSON();
34+
expect(tree.children[0].props.testID).toBe('id-1')
35+
expect(tree.children[1].props.testID).toBe('id-2')
36+
expect(tree.children[2].props.testID).toBe('id-3')
37+
});
38+
39+
it('should pass given AccessibilityLabels to child component', () => {
40+
const tree = renderer.create(<SegmentedControlTab accessibilityLabels={['label-1','label-2','label-3']} />).toJSON();
41+
expect(tree.children[0].props.accessibilityLabel).toBe('label-1')
42+
expect(tree.children[1].props.accessibilityLabel).toBe('label-2')
43+
expect(tree.children[2].props.accessibilityLabel).toBe('label-3')
44+
});
45+
46+
it('should pass given badges to child component', () => {
47+
const tree = renderer.create(<SegmentedControlTab badges={['1','2','3']} />).toJSON();
48+
expect(tree.children[0].props.badge).toBe('1')
49+
expect(tree.children[1].props.badge).toBe('2')
50+
expect(tree.children[2].props.badge).toBe('3')
51+
});
52+
53+
});
54+
55+
56+
describe('handleTabPress', () => {
57+
it('should call the callback when selectedIndex is different', () => {
58+
const callbackSpy = jest.fn()
59+
const tabIndex = 1
60+
const selectedIndex = 2
61+
handleTabPress(tabIndex, selectedIndex, callbackSpy)
62+
expect(callbackSpy).toHaveBeenCalledWith(tabIndex)
63+
});
64+
65+
});
66+
67+
describe('getAccessibilityLabelByIndex', () => {
68+
it('should return the AccessibilityLabel by given index', () => {
69+
70+
const AccessibilityLabels=['Label 1', 'Label 2']
71+
const result = getAccessibilityLabelByIndex(AccessibilityLabels, 1)
72+
expect(result).toBe(AccessibilityLabels[1])
73+
});
74+
1175
});

src/__tests__/TabOption.test.js

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,59 @@ import TabOption from '../TabOption';
44

55
import renderer from 'react-test-renderer';
66

7-
test('renders correctly', () => {
8-
const tree = renderer.create(<TabOption />).toJSON();
9-
expect(tree).toMatchSnapshot();
10-
});
7+
describe('TabOption', () => {
8+
afterEach(() => {
9+
jest.restoreAllMocks()
10+
});
11+
12+
it('renders correctly', () => {
13+
const tree = renderer.create(<TabOption />).toJSON();
14+
expect(tree).toMatchSnapshot();
15+
});
16+
17+
it('should apply active tab styles when isTabActive is set to true', () => {
18+
const activeTabStyle = {backgroundColor: '#0000FF'}
19+
const tree = renderer.create(<TabOption
20+
activeTabStyle={activeTabStyle}
21+
isTabActive />).toJSON();
22+
expect(tree.props.style.backgroundColor).toBe(activeTabStyle.backgroundColor)
23+
});
24+
25+
it('should call the onPress handler when tapped', () => {
26+
const tabIndex = 1
27+
const mockFn = jest.fn()
28+
const tree = renderer.create(<TabOption
29+
testID='tab'
30+
index={tabIndex}
31+
onTabPress={mockFn} />).toJSON()
32+
jest.mock('TouchableOpacity', () => {
33+
const jestReactNative = require('jest-react-native')
34+
return jestReactNative('TouchableOpacity')
35+
})
36+
tree.props.onPress();
37+
expect(mockFn).toHaveBeenCalledWith(tabIndex)
38+
39+
});
40+
41+
it('should render the badge along with tab text when badge is passed in props ', () => {
42+
const tree = renderer.create(<TabOption badge={5}/>).toJSON();
43+
expect(tree).toMatchSnapshot();
44+
});
45+
46+
it('should apply the passed badgeStyle when tab is active ', () => {
47+
const styles = {
48+
activeTabStyle : {backgroundColor: '#0000FF'},
49+
activeTabBadgeStyle : {color: '#CCCCCC'},
50+
activeTabBadgeContainerStyle : {backgroundColor: '#00FFFF'}
51+
}
52+
53+
const tree = renderer.create(<TabOption badge={5}
54+
activeTabStyle={styles.activeTabStyle}
55+
activeTabBadgeContainerStyle={styles.activeTabBadgeContainerStyle}
56+
activeTabBadgeStyle={styles.activeTabBadgeStyle}
57+
isTabActive />).toJSON();
58+
expect(tree).toMatchSnapshot();
59+
});
60+
})
61+
62+

0 commit comments

Comments
 (0)