Skip to content

Commit 2c8df3d

Browse files
authored
Merge pull request #80 from material-elements/unit-testing
[unit-testing] Working on unit testing
2 parents 434515b + 20a70c7 commit 2c8df3d

38 files changed

+427
-143
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ local.properties
3535
*.keystore
3636
!debug.keystore
3737

38+
# sonar scan
39+
.scannerwork
40+
3841
# node.js
3942
#
4043
node_modules/

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: 79,
3638
functions: 80,
3739
lines: 80,
3840
},

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"ios": "react-native run-ios",
88
"lint": "eslint .",
99
"start": "react-native start",
10-
"test": "jest --coverage"
10+
"test": "jest --coverage",
11+
"sonar-scanner": "sonar-scanner"
1112
},
1213
"dependencies": {
1314
"lodash": "^4.17.21",

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

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

77
describe('Button', () => {
@@ -427,4 +427,35 @@ describe('Button', () => {
427427
const labelText = getByText(mockLabel);
428428
expect(labelText.props.style).toEqual(expect.objectContaining({ color: 'red', fontWeight: 100 }));
429429
});
430+
431+
it('should show the dark gray color of the text label when button color is lightGray', () => {
432+
const { getByText } = render(<Button label="Save" buttonColor="lightGray" />);
433+
434+
const text = getByText('Save');
435+
expect(text).toBeDefined();
436+
expect(text.props.style.color).toEqual(gray[900]);
437+
});
438+
439+
it('should show the dark gray color of the text label when button color is lightGray and button variant outlined ', () => {
440+
const { getByText } = render(<Button label="Save" variation="outlined" buttonColor="lightGray" />);
441+
442+
const text = getByText('Save');
443+
expect(text).toBeDefined();
444+
expect(text.props.style.color).toEqual(gray[900]);
445+
});
446+
447+
it('should render the loader when loading is passed', () => {
448+
const { getByTestId } = render(<Button label="Save" loading />);
449+
450+
const loader = getByTestId('button-loader');
451+
expect(loader).toBeDefined();
452+
});
453+
454+
it('should show the dark color of the text label when button color is warning', () => {
455+
const { getByText } = render(<Button label="Save" buttonColor="warning" />);
456+
457+
const text = getByText('Save');
458+
expect(text).toBeDefined();
459+
expect(text.props.style.color).toEqual(gray[900]);
460+
});
430461
});

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
jest.useFakeTimers();
11+
const { toJSON } = render(<ProgressBar progress={0.2} />);
12+
expect(toJSON()).toMatchSnapshot();
13+
jest.clearAllTimers();
14+
});
15+
});

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

Lines changed: 108 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,110 @@ 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+
it('should call the onPress function when click on the label component', () => {
105+
const { getByText } = render(<Radio onPress={mockOnPress} actionType="root" label="label" />);
106+
107+
const label = getByText('label');
108+
expect(label).toBeDefined();
109+
110+
fireEvent(label, 'press', { nativeEvent: {} });
111+
112+
expect(mockOnPress).toHaveBeenCalled();
113+
expect(mockOnPress).toHaveBeenCalledTimes(1);
114+
});
115+
116+
it('should call the onPress function when click on the radio button', () => {
117+
const { getByTestId } = render(<Radio onPress={mockOnPress} radioBaseButtonTestId={mockRadioBaseButtonTestId} />);
118+
119+
const radio = getByTestId(mockRadioBaseButtonTestId);
120+
121+
expect(radio).toBeDefined();
122+
123+
fireEvent(radio, 'press', { nativeEvent: {} });
124+
125+
expect(mockOnPress).toHaveBeenCalled();
126+
expect(mockOnPress).toHaveBeenCalledTimes(1);
127+
});
128+
129+
it('should render the custom radio item if radio isActive', () => {
130+
const { getByText } = render(<Radio isActive radioItem={<Text>Hello</Text>} />);
131+
const text = getByText('Hello');
132+
expect(text).toBeDefined();
133+
});
134+
135+
it('should render the start adornment component', () => {
136+
const { getByText } = render(<Radio adornment={<Text>Hello</Text>} adornmentType="start" />);
137+
138+
const text = getByText('Hello');
139+
expect(text).toBeDefined();
140+
});
141+
142+
it('should render the start adornment divider component', () => {
143+
const { getByTestId } = render(
144+
<Radio showDivider dividerProps={{ testID: 'divider-test-id' }} adornment={<Text>Hello</Text>} adornmentType="start" />,
145+
);
146+
147+
const divider = getByTestId('divider-test-id');
148+
expect(divider).toBeDefined();
149+
});
150+
151+
it('should render active radio item when radio is active', () => {
152+
jest.useFakeTimers();
153+
const { getByTestId } = render(<Radio isActive />);
154+
const radioItem = getByTestId('radio-item-test-id');
155+
expect(radioItem).toBeDefined();
156+
jest.clearAllTimers();
157+
});
158+
});
159+
160+
describe('Radio Circle', () => {
161+
const testID = 'mock-test-id';
162+
163+
beforeEach(() => {
164+
jest.clearAllMocks();
165+
});
166+
167+
it('should render small divider component', () => {
168+
const { getByTestId } = render(<RadioCircle size="small" testID={testID} />);
169+
170+
const radio = getByTestId(testID);
171+
172+
expect(radio).toBeDefined();
173+
expect(radio.props.style.width).toEqual(RADIO_SMALL);
174+
});
175+
176+
it('should render medium radioCircle component', () => {
177+
const { getByTestId } = render(<RadioCircle size="medium" testID={testID} />);
178+
179+
const radio = getByTestId(testID);
180+
181+
expect(radio).toBeDefined();
182+
expect(radio.props.style.width).toEqual(RADIO_MEDIUM);
183+
});
184+
185+
it('should render large radioCircle component', () => {
186+
const { getByTestId } = render(<RadioCircle size="large" testID={testID} />);
187+
188+
const radio = getByTestId(testID);
189+
190+
expect(radio).toBeDefined();
191+
expect(radio.props.style.width).toEqual(RADIO_LARGE);
192+
});
193+
194+
it('should render small radioCircle component when invalid size passed', () => {
195+
const { getByTestId } = render(<RadioCircle size={'invalid' as any} testID={testID} />);
196+
197+
const radio = getByTestId(testID);
198+
199+
expect(radio).toBeDefined();
200+
expect(radio.props.style.width).toEqual(RADIO_SMALL);
201+
});
95202
});

0 commit comments

Comments
 (0)