Skip to content

Commit 2e3ec67

Browse files
ElenaBitireyjose
authored andcommitted
refactor: refactor tests
1 parent 35f7d66 commit 2e3ec67

File tree

4 files changed

+37
-43
lines changed

4 files changed

+37
-43
lines changed

src/ui/button.test.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ afterEach(cleanup);
99

1010
describe('Button component ', () => {
1111
it('should render correctly ', () => {
12-
render(<Button testID="button" />);
12+
render(<Button testID="button" disabled={false} />);
1313
expect(screen.getByTestId('button')).toBeOnTheScreen();
1414
});
1515
it('should render the label correctly', () => {
@@ -22,7 +22,7 @@ describe('Button component ', () => {
2222
expect(screen.getByTestId('button')).toBeOnTheScreen();
2323
expect(screen.getByTestId('button-activity-indicator')).toBeOnTheScreen();
2424
});
25-
it('should call onClick handler when clicked', async () => {
25+
it('should call onClick handler when clicked', () => {
2626
const onClick = jest.fn();
2727
render(
2828
<Button testID="button" label="Click the button" onPress={onClick} />
@@ -43,17 +43,13 @@ describe('Button component ', () => {
4343
);
4444
expect(screen.getByTestId('button')).toBeOnTheScreen();
4545
expect(screen.getByTestId('button-activity-indicator')).toBeOnTheScreen();
46-
expect(screen.getByTestId('button').props.accessibilityState.disabled).toBe(
47-
true
48-
);
46+
expect(screen.getByTestId('button')).toBeDisabled();
4947
fireEvent.press(screen.getByTestId('button'));
5048
expect(onClick).toHaveBeenCalledTimes(0);
5149
});
5250
it('should be disabled when disabled prop is true', () => {
5351
render(<Button testID="button" disabled={true} />);
54-
expect(screen.getByTestId('button').props.accessibilityState.disabled).toBe(
55-
true
56-
);
52+
expect(screen.getByTestId('button')).toBeDisabled();
5753
});
5854
it("shouldn't call onClick when disabled", () => {
5955
const onClick = jest.fn();
@@ -68,9 +64,9 @@ describe('Button component ', () => {
6864
);
6965
expect(screen.getByTestId('button')).toBeOnTheScreen();
7066
fireEvent.press(screen.getByTestId('button'));
71-
expect(screen.getByTestId('button').props.accessibilityState.disabled).toBe(
72-
true
73-
);
67+
68+
expect(screen.getByTestId('button')).toBeDisabled();
69+
7470
expect(onClick).toHaveBeenCalledTimes(0);
7571
});
7672
it('should apply correct styles based on size prop', () => {

src/ui/checkbox.test.tsx

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Checkbox, Radio, Switch } from './checkbox';
1010
afterEach(cleanup);
1111

1212
describe('Checkbox, Radio & Switch components ', () => {
13-
it('<Checkbox /> renders correctly and call on change on Press', async () => {
13+
it('<Checkbox /> renders correctly and call on change on Press', () => {
1414
const mockOnChange = jest.fn((checked) => checked);
1515
render(
1616
<Checkbox
@@ -22,9 +22,9 @@ describe('Checkbox, Radio & Switch components ', () => {
2222
);
2323
expect(screen.getByTestId('checkbox')).toBeOnTheScreen();
2424
expect(screen.queryByTestId('checkbox-label')).not.toBeOnTheScreen();
25-
expect(
26-
screen.getByTestId('checkbox').props.accessibilityState.checked
27-
).toBe(false);
25+
expect(screen.getByTestId('checkbox')).toBeEnabled();
26+
27+
expect(screen.getByTestId('checkbox')).not.toBeChecked();
2828
expect(screen.getByTestId('checkbox').props.accessibilityRole).toBe(
2929
'checkbox'
3030
);
@@ -37,7 +37,7 @@ describe('Checkbox, Radio & Switch components ', () => {
3737
expect(mockOnChange).toHaveBeenCalledWith(true);
3838
});
3939

40-
it("<CheckBox/> shouldn't change value while disabled", async () => {
40+
it("<CheckBox/> shouldn't change value while disabled", () => {
4141
const mockOnChange = jest.fn((checked) => checked);
4242
render(
4343
<Checkbox
@@ -49,11 +49,11 @@ describe('Checkbox, Radio & Switch components ', () => {
4949
/>
5050
);
5151
expect(screen.getByTestId('checkbox')).toBeOnTheScreen();
52-
52+
expect(screen.getByTestId('checkbox')).toBeDisabled();
5353
fireEvent.press(screen.getByTestId('checkbox'));
5454
expect(mockOnChange).toHaveBeenCalledTimes(0);
5555
});
56-
it('<CheckBox/> Should render the correct label', async () => {
56+
it('<CheckBox/> Should render the correct label', () => {
5757
const mockOnChange = jest.fn((checked) => checked);
5858
render(
5959
<Checkbox
@@ -73,18 +73,18 @@ describe('Checkbox, Radio & Switch components ', () => {
7373
expect(screen.getByTestId('checkbox').props.accessibilityRole).toBe(
7474
'checkbox'
7575
);
76+
7677
expect(screen.getByTestId('checkbox').props.accessibilityLabel).toBe(
7778
'agree'
7879
);
79-
80-
expect(screen.getByTestId('checkbox-label')?.props.children).toBe(
80+
expect(screen.getByTestId('checkbox-label')).toHaveTextContent(
8181
'I agree to terms and conditions'
8282
);
8383
fireEvent.press(screen.getByTestId('checkbox'));
8484
expect(mockOnChange).toHaveBeenCalledTimes(0);
8585
});
8686

87-
it('<Radio /> renders correctly and call on change on Press', async () => {
87+
it('<Radio /> renders correctly and call on change on Press', () => {
8888
const mockOnChange = jest.fn((checked) => checked);
8989
render(
9090
<Radio
@@ -96,18 +96,16 @@ describe('Checkbox, Radio & Switch components ', () => {
9696
);
9797
expect(screen.getByTestId('radio')).toBeOnTheScreen();
9898
expect(screen.queryByTestId('radio-label')).not.toBeOnTheScreen();
99-
100-
expect(screen.getByTestId('radio').props.accessibilityState.checked).toBe(
101-
false
102-
);
99+
expect(screen.getByTestId('radio')).toBeEnabled();
100+
expect(screen.getByTestId('radio')).not.toBeChecked();
103101
expect(screen.getByTestId('radio').props.accessibilityRole).toBe('radio');
104102
expect(screen.getByTestId('radio').props.accessibilityLabel).toBe('agree');
105103
fireEvent.press(screen.getByTestId('radio'));
106104
expect(mockOnChange).toHaveBeenCalledTimes(1);
107105
expect(mockOnChange).toHaveBeenCalledWith(true);
108106
});
109107

110-
it('<Radio /> should render the correct label', async () => {
108+
it('<Radio /> should render the correct label', () => {
111109
const mockOnChange = jest.fn((checked) => checked);
112110
render(
113111
<Radio
@@ -120,7 +118,7 @@ describe('Checkbox, Radio & Switch components ', () => {
120118
);
121119
expect(screen.getByTestId('radio')).toBeOnTheScreen();
122120
expect(screen.getByTestId('radio-label')).toBeOnTheScreen();
123-
expect(screen.getByTestId('radio-label')?.props.children).toBe(
121+
expect(screen.getByTestId('radio-label')).toHaveTextContent(
124122
'I agree to terms and conditions'
125123
);
126124

@@ -134,7 +132,7 @@ describe('Checkbox, Radio & Switch components ', () => {
134132
expect(mockOnChange).toHaveBeenCalledWith(true);
135133
});
136134

137-
it("<Radio/> shouldn't change value while disabled", async () => {
135+
it("<Radio/> shouldn't change value while disabled", () => {
138136
const mockOnChange = jest.fn((checked) => checked);
139137
render(
140138
<Radio
@@ -146,12 +144,12 @@ describe('Checkbox, Radio & Switch components ', () => {
146144
/>
147145
);
148146
expect(screen.getByTestId('radio')).toBeOnTheScreen();
149-
147+
expect(screen.getByTestId('radio')).toBeDisabled();
150148
fireEvent.press(screen.getByTestId('radio'));
151149
expect(mockOnChange).toHaveBeenCalledTimes(0);
152150
});
153151

154-
it('<Switch /> renders correctly and call on change on Press', async () => {
152+
it('<Switch /> renders correctly and call on change on Press', () => {
155153
const mockOnChange = jest.fn((checked) => checked);
156154
render(
157155
<Switch
@@ -163,7 +161,7 @@ describe('Checkbox, Radio & Switch components ', () => {
163161
);
164162
expect(screen.getByTestId('switch')).toBeOnTheScreen();
165163
expect(screen.queryByTestId('switch-label')).not.toBeOnTheScreen();
166-
164+
expect(screen.getByTestId('switch')).toBeEnabled();
167165
expect(screen.getByTestId('switch').props.accessibilityState.checked).toBe(
168166
false
169167
);
@@ -174,7 +172,7 @@ describe('Checkbox, Radio & Switch components ', () => {
174172
expect(mockOnChange).toHaveBeenCalledWith(true);
175173
});
176174

177-
it('<Switch /> should render the correct label', async () => {
175+
it('<Switch /> should render the correct label', () => {
178176
const mockOnChange = jest.fn((checked) => checked);
179177
render(
180178
<Switch
@@ -187,7 +185,7 @@ describe('Checkbox, Radio & Switch components ', () => {
187185
);
188186
expect(screen.getByTestId('switch')).toBeOnTheScreen();
189187
expect(screen.getByTestId('switch-label')).toBeOnTheScreen();
190-
expect(screen.getByTestId('switch-label')?.props.children).toBe(
188+
expect(screen.getByTestId('switch-label')).toHaveTextContent(
191189
'I agree to terms and conditions'
192190
);
193191
expect(screen.getByTestId('switch').props.accessibilityState.checked).toBe(
@@ -200,7 +198,7 @@ describe('Checkbox, Radio & Switch components ', () => {
200198
expect(mockOnChange).toHaveBeenCalledWith(true);
201199
});
202200

203-
it("<Switch/> shouldn't change value while disabled", async () => {
201+
it("<Switch/> shouldn't change value while disabled", () => {
204202
const mockOnChange = jest.fn((checked) => checked);
205203
render(
206204
<Switch

src/ui/input.test.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ describe('Input component ', () => {
2424
it('should render the label correctly ', () => {
2525
render(<Input testID="input" label="Username" />);
2626
expect(screen.getByTestId('input')).toBeOnTheScreen();
27-
expect(screen.getByTestId('input-label')).toBeOnTheScreen();
28-
expect(screen.getByTestId('input-label')?.props.children).toBe('Username');
27+
28+
expect(screen.getByTestId('input-label')).toHaveTextContent('Username');
2929
});
3030

3131
it('should render the error message correctly ', () => {
3232
render(<Input testID="input" error="This is an error message" />);
3333
expect(screen.getByTestId('input')).toBeOnTheScreen();
34-
expect(screen.getByTestId('input-error')).toBeOnTheScreen();
35-
expect(screen.getByTestId('input-error')?.props.children).toBe(
34+
35+
expect(screen.getByTestId('input-error')).toHaveTextContent(
3636
'This is an error message'
3737
);
3838
});
@@ -46,10 +46,10 @@ describe('Input component ', () => {
4646
/>
4747
);
4848
expect(screen.getByTestId('input')).toBeOnTheScreen();
49-
expect(screen.getByTestId('input-label')).toBeOnTheScreen();
50-
expect(screen.getByTestId('input-label')?.props.children).toBe('Username');
49+
50+
expect(screen.getByTestId('input-label')).toHaveTextContent('Username');
5151
expect(screen.getByTestId('input-error')).toBeOnTheScreen();
52-
expect(screen.getByTestId('input-error')?.props.children).toBe(
52+
expect(screen.getByTestId('input-error')).toHaveTextContent(
5353
'This is an error message'
5454
);
5555
expect(

src/ui/select.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('Select component ', () => {
4040
);
4141
expect(screen.getByTestId('select-trigger')).toBeOnTheScreen();
4242
expect(screen.getByTestId('select-label')).toBeOnTheScreen();
43-
expect(screen.getByTestId('select-label')?.props.children).toBe('Select');
43+
expect(screen.getByTestId('select-label')).toHaveTextContent('Select');
4444
});
4545

4646
it('should render the error correctly ', () => {
@@ -56,7 +56,7 @@ describe('Select component ', () => {
5656
);
5757
expect(screen.getByTestId('select-trigger')).toBeOnTheScreen();
5858
expect(screen.getByTestId('select-error')).toBeOnTheScreen();
59-
expect(screen.getByTestId('select-error')?.props.children).toBe(
59+
expect(screen.getByTestId('select-error')).toHaveTextContent(
6060
'Please select an option'
6161
);
6262
});

0 commit comments

Comments
 (0)