Skip to content

Commit 1b1d6f5

Browse files
authored
RadioGroup - add render tests (#1687)
* RadioGroup - add render tests * Fix readability
1 parent e94d023 commit 1b1d6f5

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import React from 'react';
2+
import {render, fireEvent} from '@testing-library/react-native';
3+
import RadioGroup from '../index';
4+
import RadioButton from '../../radioButton/index';
5+
6+
const testIDs = {
7+
radioGroup: 'RadioGroup',
8+
radioButtonUp: 'radioButtonUp',
9+
radioButtonDown: 'radioButtonDown',
10+
radioButtonLeft: 'radioButtonLeft',
11+
radioButtonRight: 'radioButtonRight'
12+
};
13+
const onValueChangeMock = jest.fn();
14+
15+
const RadioGroupTestComponent = props => {
16+
const {shouldDisable, ...others} = props;
17+
return (
18+
<RadioGroup {...others} onValueChange={onValueChangeMock} testID={testIDs.radioGroup}>
19+
<RadioButton value={'up'} label={'Up'} disabled={shouldDisable} testID={testIDs.radioButtonUp}/>
20+
<RadioButton value={'down'} label={'Down'} testID={testIDs.radioButtonDown}/>
21+
<RadioButton value={'left'} label={'Left'} testID={testIDs.radioButtonLeft}/>
22+
<RadioButton value={'right'} label={'Right'} testID={testIDs.radioButtonRight}/>
23+
</RadioGroup>
24+
);
25+
};
26+
27+
describe('RadioGroup renderer test', () => {
28+
beforeEach(() => {
29+
jest.clearAllMocks();
30+
});
31+
32+
it('Press on radio button', async () => {
33+
const props = {onValueChangeMock};
34+
35+
const {getByTestId} = render(<RadioGroupTestComponent {...props}/>);
36+
37+
getByTestId(testIDs.radioGroup);
38+
39+
const radioButtonUp = getByTestId(testIDs.radioButtonUp);
40+
fireEvent.press(radioButtonUp);
41+
expect(onValueChangeMock).toHaveBeenCalledTimes(1);
42+
expect(onValueChangeMock).toHaveBeenCalledWith('up');
43+
});
44+
45+
it('Press on disabled radio button', async () => {
46+
const props = {onValueChangeMock, shouldDisable: true};
47+
48+
const {getByTestId} = render(<RadioGroupTestComponent {...props}/>);
49+
50+
getByTestId(testIDs.radioGroup);
51+
52+
const radioButtonUp = getByTestId(testIDs.radioButtonUp);
53+
fireEvent.press(radioButtonUp);
54+
expect(onValueChangeMock).not.toHaveBeenCalled();
55+
});
56+
57+
it('Press on selected radio button', async () => {
58+
const props = {onValueChangeMock, initialValue: 'up'};
59+
60+
const {getByTestId} = render(<RadioGroupTestComponent {...props}/>);
61+
62+
getByTestId(testIDs.radioGroup);
63+
64+
const radioButtonUp = getByTestId(testIDs.radioButtonUp);
65+
fireEvent.press(radioButtonUp);
66+
expect(onValueChangeMock).toHaveBeenCalledTimes(1);
67+
expect(onValueChangeMock).toHaveBeenCalledWith('up');
68+
});
69+
70+
it('Press multiple radio buttons', async () => {
71+
const props = {onValueChangeMock};
72+
73+
const {getByTestId} = render(<RadioGroupTestComponent {...props}/>);
74+
75+
getByTestId(testIDs.radioGroup);
76+
77+
const radioButtonUp = getByTestId(testIDs.radioButtonUp);
78+
fireEvent.press(radioButtonUp);
79+
expect(onValueChangeMock).toHaveBeenCalledTimes(1);
80+
expect(onValueChangeMock).toHaveBeenCalledWith('up');
81+
const radioButtonDown = getByTestId(testIDs.radioButtonDown);
82+
fireEvent.press(radioButtonDown);
83+
expect(onValueChangeMock).toHaveBeenCalledTimes(2);
84+
expect(onValueChangeMock.mock.calls).toEqual([['up'], ['down']]);
85+
const radioButtonLeft = getByTestId(testIDs.radioButtonLeft);
86+
fireEvent.press(radioButtonLeft);
87+
expect(onValueChangeMock).toHaveBeenCalledTimes(3);
88+
expect(onValueChangeMock.mock.calls).toEqual([['up'], ['down'], ['left']]);
89+
const radioButtonRight = getByTestId(testIDs.radioButtonRight);
90+
fireEvent.press(radioButtonRight);
91+
expect(onValueChangeMock).toHaveBeenCalledTimes(4);
92+
expect(onValueChangeMock.mock.calls).toEqual([['up'], ['down'], ['left'], ['right']]);
93+
});
94+
});

0 commit comments

Comments
 (0)