Skip to content

Commit 1c3a4ba

Browse files
committed
[unit-testing] Working on unit testing
1 parent 434515b commit 1c3a4ba

File tree

14 files changed

+208
-36
lines changed

14 files changed

+208
-36
lines changed

App.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import { SafeAreaView, ScrollView } from 'react-native';
3-
import { Container, ThemeProvider } from './src/packages/react-native-material-elements';
3+
import { Container, SegmentedControl, ThemeProvider } from './src/packages/react-native-material-elements';
44

55
function App(): React.JSX.Element {
6+
const [index, setIndex] = useState<number>(0);
7+
68
return (
79
<ThemeProvider>
810
<SafeAreaView>
911
<ScrollView>
10-
<Container />
12+
<Container>
13+
<SegmentedControl
14+
data={['One', 'Two', 'Three', 'Four', 'Five']}
15+
selectedIndex={index}
16+
onChange={(_, _index) => setIndex(_index)}
17+
/>
18+
</Container>
1119
</ScrollView>
1220
</SafeAreaView>
1321
</ThemeProvider>

jest.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ module.exports = {
2222
'babel.config.js',
2323
],
2424

25+
testPathIgnorePatterns: ['/node_modules', '/e2e'],
26+
2527
coverageReporters: ['html', 'text', 'lcov', 'text-summary'],
2628

2729
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node', 'mjs', 'svg', 'png'],
@@ -32,7 +34,7 @@ module.exports = {
3234
coverageThreshold: {
3335
global: {
3436
statements: 80,
35-
branches: 75,
37+
branches: 80,
3638
functions: 80,
3739
lines: 80,
3840
},

src/packages/react-native-material-elements/__test__/DropDown.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('DropDown Component', () => {
99

1010
beforeEach(() => {
1111
jest.clearAllMocks();
12+
jest.clearAllTimers();
1213
});
1314

1415
it('should render correctly', async () => {
@@ -244,6 +245,7 @@ describe('DropDownListContainer component', () => {
244245
});
245246

246247
it('should call the onClose function when press on item', () => {
248+
jest.useFakeTimers();
247249
const { getByText } = render(
248250
<DropDownListContainer
249251
inputLayoutRectangle={{ x: 0, y: 0, width: 0, height: 0 }}

src/packages/react-native-material-elements/__test__/ModalContainer.test.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import React from 'react';
2-
import { render } from './test-utils';
2+
import { fireEvent, render } from './test-utils';
33
import { ModalContainer, Text } from '../src';
44

55
describe('ModalContainer', () => {
6+
const mockOnCloseHandler = jest.fn();
7+
8+
const mockTestId = 'mock-test-id';
9+
610
it('should render correctly with default props', () => {
711
const { toJSON } = render(
812
<ModalContainer>
@@ -21,4 +25,18 @@ describe('ModalContainer', () => {
2125
const element = getByText('Hello');
2226
expect(element).toBeDefined();
2327
});
28+
29+
it('should call the onClose function', () => {
30+
const { getByTestId } = render(
31+
<ModalContainer onClose={mockOnCloseHandler} rootWrapperTestID={mockTestId}>
32+
<></>
33+
</ModalContainer>,
34+
);
35+
36+
const item = getByTestId(mockTestId);
37+
38+
fireEvent(item, 'press', { nativeEvent: {} });
39+
expect(mockOnCloseHandler).toHaveBeenCalled();
40+
expect(mockOnCloseHandler).toHaveBeenCalledTimes(1);
41+
});
2442
});

src/packages/react-native-material-elements/__test__/Pagination.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,16 @@ describe('Pagination Component', () => {
157157
expect(flattenedStyle.backgroundColor).toEqual('red');
158158
expect(flattenedStyle.borderWidth).toEqual(2);
159159
});
160+
161+
it('should show all the pagination item if count is less than or equal to the max visible items, show all pages', () => {
162+
const { getByText } = render(<Pagination count={3} />);
163+
const firstItem = getByText('1');
164+
expect(firstItem).toBeDefined();
165+
166+
const secondItem = getByText('2');
167+
expect(secondItem).toBeDefined();
168+
169+
const thirdItem = getByText('3');
170+
expect(thirdItem).toBeDefined();
171+
});
160172
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ProgressBar } from '../src';
2+
import { render } from './test-utils';
3+
4+
describe('ProgressBar component', () => {
5+
beforeEach(() => {
6+
jest.clearAllMocks();
7+
});
8+
9+
it('should render correctly with default props', () => {
10+
const { toJSON } = render(<ProgressBar progress={0.2} />);
11+
expect(toJSON()).toMatchSnapshot();
12+
});
13+
});

src/packages/react-native-material-elements/__test__/Radio.test.tsx

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React from 'react';
22
import { fireEvent, render, waitFor } from './test-utils';
3-
import { Radio, Text } from '../src';
3+
import { Radio, RadioCircle, Text } from '../src';
44
import { View } from 'react-native';
5+
import { RADIO_LARGE, RADIO_MEDIUM, RADIO_SMALL } from '../src/components/Radio/constants';
56

67
describe('Radio Component', () => {
78
const mockRadioBaseButtonTestId = 'radio-base-button-test-id';
@@ -92,4 +93,55 @@ describe('Radio Component', () => {
9293
expect(queryByText('mock-label')).toBeNull();
9394
expect(queryByText('mock-description')).toBeNull();
9495
});
96+
97+
it('should render the divider component', () => {
98+
const { getByTestId } = render(<Radio showDivider dividerProps={{ testID: 'divider' }} />);
99+
100+
const divider = getByTestId('divider');
101+
expect(divider).toBeDefined();
102+
});
103+
});
104+
105+
describe('Radio Circle', () => {
106+
const testID = 'mock-test-id';
107+
108+
beforeEach(() => {
109+
jest.clearAllMocks();
110+
});
111+
112+
it('should render small divider component', () => {
113+
const { getByTestId } = render(<RadioCircle size="small" testID={testID} />);
114+
115+
const radio = getByTestId(testID);
116+
117+
expect(radio).toBeDefined();
118+
expect(radio.props.style.width).toEqual(RADIO_SMALL);
119+
});
120+
121+
it('should render medium radioCircle component', () => {
122+
const { getByTestId } = render(<RadioCircle size="medium" testID={testID} />);
123+
124+
const radio = getByTestId(testID);
125+
126+
expect(radio).toBeDefined();
127+
expect(radio.props.style.width).toEqual(RADIO_MEDIUM);
128+
});
129+
130+
it('should render large radioCircle component', () => {
131+
const { getByTestId } = render(<RadioCircle size="large" testID={testID} />);
132+
133+
const radio = getByTestId(testID);
134+
135+
expect(radio).toBeDefined();
136+
expect(radio.props.style.width).toEqual(RADIO_LARGE);
137+
});
138+
139+
it('should render small radioCircle component when invalid size passed', () => {
140+
const { getByTestId } = render(<RadioCircle size={'invalid' as any} testID={testID} />);
141+
142+
const radio = getByTestId(testID);
143+
144+
expect(radio).toBeDefined();
145+
expect(radio.props.style.width).toEqual(RADIO_SMALL);
146+
});
95147
});

src/packages/react-native-material-elements/__test__/SegmentedControl.test.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { SegmentedControlItem } from '../src/components/SegmentedControl/Segment
44
import { fireEvent, render } from './test-utils';
55

66
describe('SegmentedControl component', () => {
7+
const mockSegmentedControllerTestId = 'mock-segmented-item-test-id';
8+
9+
const mockOnPress = jest.fn();
10+
711
beforeEach(() => {
812
jest.clearAllMocks();
913
});
@@ -12,6 +16,57 @@ describe('SegmentedControl component', () => {
1216
const { toJSON } = render(<SegmentedControl data={['']} selectedIndex={0} />);
1317
expect(toJSON()).toMatchSnapshot();
1418
});
19+
20+
it('should call the onChange function', () => {
21+
const { getByTestId } = render(
22+
<SegmentedControl
23+
data={['First', 'Second']}
24+
selectedIndex={0}
25+
segmentedControlItemTestId={mockSegmentedControllerTestId}
26+
onChange={mockOnPress}
27+
/>,
28+
);
29+
30+
const firstItem = getByTestId(`${mockSegmentedControllerTestId}-0`);
31+
32+
fireEvent(firstItem, 'press', { nativeEvent: {} });
33+
34+
expect(firstItem).toBeDefined();
35+
expect(mockOnPress).toHaveBeenCalled();
36+
expect(mockOnPress).toHaveBeenCalledTimes(1);
37+
});
38+
39+
it('should apply the correct segmentTextStyle', () => {
40+
const { getByText } = render(
41+
<SegmentedControl data={['First', 'Second']} selectedIndex={0} segmentTextStyle={{ color: 'red' }} />,
42+
);
43+
44+
const firstItem = getByText('First');
45+
expect(firstItem).toBeDefined();
46+
47+
expect(firstItem.props.style.color).toEqual('red');
48+
49+
const secondItem = getByText('Second');
50+
expect(secondItem).toBeDefined();
51+
52+
expect(secondItem.props.style.color).toEqual('red');
53+
});
54+
55+
it('should apply the correct segmentTextStyle to specific item', () => {
56+
const { getByText } = render(
57+
<SegmentedControl
58+
data={['First', 'Second']}
59+
selectedIndex={0}
60+
segmentTextStyle={{ color: 'red' }}
61+
applySegmentItemTextStyleIndex={0}
62+
/>,
63+
);
64+
65+
const firstItem = getByText('First');
66+
expect(firstItem).toBeDefined();
67+
68+
expect(firstItem.props.style.color).toEqual('red');
69+
});
1570
});
1671

1772
describe('SegmentedControlContainer component', () => {

src/packages/react-native-material-elements/__test__/Switch.test.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
SWITCH_THUMB_WIDTH_MEDIUM,
2323
SWITCH_THUMB_WIDTH_SMALL,
2424
} from '../src';
25-
import { fireEvent, render, waitFor } from './test-utils';
25+
import { fireEvent, render } from './test-utils';
2626

2727
describe('Switch Component', () => {
2828
const switchMockTestId = 'switch-test-id';
@@ -33,11 +33,9 @@ describe('Switch Component', () => {
3333
jest.clearAllMocks();
3434
});
3535

36-
it('should match the snapshot with default props', async () => {
36+
it('should match the snapshot with default props', () => {
3737
const { toJSON } = render(<Switch />);
38-
await waitFor(() => {
39-
expect(toJSON()).toMatchSnapshot();
40-
});
38+
expect(toJSON()).toMatchSnapshot();
4139
});
4240

4341
it('should forward ref correctly', () => {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`ProgressBar component should render correctly with default props 1`] = `
4+
<View
5+
accessible={true}
6+
onLayout={[Function]}
7+
>
8+
<View
9+
collapsable={false}
10+
style={
11+
{
12+
"borderColor": "#03A9F4",
13+
"borderRadius": 3,
14+
"borderWidth": 0.6,
15+
"height": 9,
16+
"overflow": "hidden",
17+
}
18+
}
19+
/>
20+
</View>
21+
`;

0 commit comments

Comments
 (0)