Skip to content

Commit 2d271d4

Browse files
committed
[unit-testing] Added test cases
1 parent 8993f11 commit 2d271d4

File tree

20 files changed

+142
-54
lines changed

20 files changed

+142
-54
lines changed

jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ module.exports = {
3333

3434
coverageThreshold: {
3535
global: {
36-
statements: 80,
37-
branches: 79,
38-
functions: 80,
39-
lines: 80,
36+
statements: 85,
37+
branches: 80,
38+
functions: 85,
39+
lines: 85,
4040
},
4141
},
4242
};

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,16 @@ describe('Badge', () => {
247247
expect(badge.props.style).toEqual(expect.objectContaining({ backgroundColor: expectedStyle.color }));
248248
});
249249
});
250+
251+
it('should throw error if passing invalid type of content', () => {
252+
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
253+
expect(() => render(<Badge badgeContent={[] as any} />)).toThrow(new TypeError('Badge content must be a string or number'));
254+
consoleSpy.mockRestore();
255+
});
256+
257+
it('should render badge content if content type is string', () => {
258+
const { getByText } = render(<Badge badgeContent={'Save'} />);
259+
const content = getByText('Save');
260+
expect(content).toBeDefined();
261+
});
250262
});

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { render as testRenderer, waitFor } from '@testing-library/react-native';
22
import React from 'react';
3-
import { Text, View } from 'react-native';
3+
import { StyleSheet, Text, View } from 'react-native';
44
import { Button, gray, green, lightBlue, primary, red, secondary, ThemeProvider, yellow } from '../src';
55
import { fireEvent, render } from './test-utils';
66

@@ -458,4 +458,33 @@ describe('Button', () => {
458458
expect(text).toBeDefined();
459459
expect(text.props.style.color).toEqual(gray[900]);
460460
});
461+
462+
it('should apply side padding when start and end icon passed', () => {
463+
const { getByText } = render(<Button label="Save" startIcon={<Text>Icon_1</Text>} endIcon={<Text>Icon_2</Text>} />);
464+
465+
const text = getByText('Save');
466+
467+
const flattenStyles = StyleSheet.flatten(text.props.style);
468+
expect(flattenStyles).toEqual(expect.objectContaining({ paddingLeft: 5, paddingRight: 5 }));
469+
});
470+
471+
it('should apply left padding when start icon passed', () => {
472+
const { getByText } = render(<Button label="Save" startIcon={<Text>Icon_1</Text>} />);
473+
474+
const text = getByText('Save');
475+
476+
const flattenStyles = StyleSheet.flatten(text.props.style);
477+
expect(flattenStyles).toEqual(expect.objectContaining({ paddingLeft: 5, paddingRight: 10 }));
478+
});
479+
480+
it('should apply right padding when end icon passed', () => {
481+
const { getByText } = render(<Button label="Save" endIcon={<Text>Icon_1</Text>} />);
482+
483+
const text = getByText('Save');
484+
485+
const flattenStyles = StyleSheet.flatten(text.props.style);
486+
expect(flattenStyles).toEqual(expect.objectContaining({ paddingLeft: 10, paddingRight: 5 }));
487+
});
488+
489+
it('should render the gray loader indicator when theme mode is light', () => {});
461490
});

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,9 @@ describe('Chip Component', () => {
412412
const chip = getByTestId(chipMockTestId);
413413
expect(chip.props.style).not.toEqual(expect.objectContaining({ backgroundColor: 'red' }));
414414
});
415+
416+
it('should not render the label when label not passed', () => {
417+
const { getByTestId } = render(<Chip />);
418+
expect(getByTestId('chip-label-test-id').props.children).toBeUndefined();
419+
});
415420
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Outline } from '../src/components/TextField/InputOutline';
2+
import { render } from './test-utils';
3+
4+
describe('InputOutline component', () => {
5+
beforeEach(() => {
6+
jest.clearAllMocks();
7+
});
8+
9+
it('should render correctly with default props', () => {
10+
const { toJSON } = render(<Outline />);
11+
expect(toJSON()).toMatchSnapshot();
12+
});
13+
});

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ describe('OTP Component', () => {
3636
expect(mockOnChange).toHaveBeenCalledTimes(1);
3737
});
3838

39+
it('calls onChange with the correct OTP value 2', () => {
40+
const { getByTestId } = render(<OTPInput length={3} onChange={mockOnChange} />);
41+
42+
const firstInput = getByTestId('otp-input-0');
43+
fireEvent.changeText(firstInput, '1');
44+
45+
const secondInput = getByTestId('otp-input-1');
46+
fireEvent.changeText(secondInput, '2');
47+
48+
const thirdInput = getByTestId('otp-input-2');
49+
fireEvent.changeText(thirdInput, '3');
50+
51+
expect(mockOnChange).toHaveBeenCalledWith('123');
52+
expect(mockOnChange).toHaveBeenCalledTimes(3);
53+
});
54+
3955
it('focuses the next input when a digit is entered', () => {
4056
const { getByTestId } = render(<OTPInput length={3} />);
4157

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ describe('SegmentedControl component', () => {
9898
<SegmentedControl
9999
data={['First', 'Second']}
100100
selectedIndex={0}
101-
segmentItemStyles={{ backgroundColor: 'red' }}
101+
segmentItemStyles={{ backgroundColor: 'red', borderWidth: 1 }}
102102
segmentedControlItemTestId={mockSegmentedControllerTestId}
103103
applySegmentItemStyleIndex={0}
104104
/>,
105105
);
106106

107107
const segmentedFirstItem = getByTestId(`${mockSegmentedControllerTestId}-0`);
108108
const firstItemStyles = StyleSheet.flatten(segmentedFirstItem.props.style);
109-
expect(firstItemStyles).toEqual(expect.objectContaining({ backgroundColor: 'red' }));
109+
expect(firstItemStyles).toEqual(expect.objectContaining({ backgroundColor: 'red', borderWidth: 1 }));
110110
});
111111
});
112112

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ import { render } from './test-utils';
44

55
describe('snackbar utils', () => {
66
beforeEach(() => {
7+
jest.useFakeTimers();
78
jest.clearAllMocks();
89
});
910

11+
afterEach(() => {
12+
jest.clearAllTimers();
13+
});
14+
1015
it('should render with default props', () => {
1116
const { toJSON } = render(<Snackbar />);
1217
expect(toJSON()).toMatchSnapshot();

src/packages/react-native-material-elements/__test__/__snapshots__/Chip.test.tsx.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ exports[`Chip Component should match the snapshot with default props 1`] = `
8383
style={
8484
{
8585
"color": "#FAFAFA",
86-
"fontSize": 14,
86+
"fontSize": 12,
8787
"fontWeight": "500",
88+
"testID": "chip-label-test-id",
8889
}
8990
}
91+
testID="chip-label-test-id"
9092
/>
9193
</View>
9294
</View>
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[`InputOutline component should render correctly with default props 1`] = `
4+
<View
5+
style={
6+
{
7+
"alignItems": "center",
8+
"backgroundColor": "transparent",
9+
"borderColor": "#BDBDBD",
10+
"borderRadius": undefined,
11+
"borderWidth": 0.6,
12+
"display": "flex",
13+
"flexDirection": "row",
14+
"opacity": 0.6,
15+
"paddingHorizontal": 14,
16+
"position": "relative",
17+
"width": "100%",
18+
}
19+
}
20+
/>
21+
`;

0 commit comments

Comments
 (0)