Skip to content

Commit

Permalink
refactor: refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ElenaBitire authored and yjose committed Mar 13, 2024
1 parent 35f7d66 commit 2e3ec67
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
18 changes: 7 additions & 11 deletions src/ui/button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ afterEach(cleanup);

describe('Button component ', () => {
it('should render correctly ', () => {
render(<Button testID="button" />);
render(<Button testID="button" disabled={false} />);
expect(screen.getByTestId('button')).toBeOnTheScreen();
});
it('should render the label correctly', () => {
Expand All @@ -22,7 +22,7 @@ describe('Button component ', () => {
expect(screen.getByTestId('button')).toBeOnTheScreen();
expect(screen.getByTestId('button-activity-indicator')).toBeOnTheScreen();
});
it('should call onClick handler when clicked', async () => {
it('should call onClick handler when clicked', () => {
const onClick = jest.fn();
render(
<Button testID="button" label="Click the button" onPress={onClick} />
Expand All @@ -43,17 +43,13 @@ describe('Button component ', () => {
);
expect(screen.getByTestId('button')).toBeOnTheScreen();
expect(screen.getByTestId('button-activity-indicator')).toBeOnTheScreen();
expect(screen.getByTestId('button').props.accessibilityState.disabled).toBe(
true
);
expect(screen.getByTestId('button')).toBeDisabled();
fireEvent.press(screen.getByTestId('button'));
expect(onClick).toHaveBeenCalledTimes(0);
});
it('should be disabled when disabled prop is true', () => {
render(<Button testID="button" disabled={true} />);
expect(screen.getByTestId('button').props.accessibilityState.disabled).toBe(
true
);
expect(screen.getByTestId('button')).toBeDisabled();
});
it("shouldn't call onClick when disabled", () => {
const onClick = jest.fn();
Expand All @@ -68,9 +64,9 @@ describe('Button component ', () => {
);
expect(screen.getByTestId('button')).toBeOnTheScreen();
fireEvent.press(screen.getByTestId('button'));
expect(screen.getByTestId('button').props.accessibilityState.disabled).toBe(
true
);

expect(screen.getByTestId('button')).toBeDisabled();

expect(onClick).toHaveBeenCalledTimes(0);
});
it('should apply correct styles based on size prop', () => {
Expand Down
44 changes: 21 additions & 23 deletions src/ui/checkbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Checkbox, Radio, Switch } from './checkbox';
afterEach(cleanup);

describe('Checkbox, Radio & Switch components ', () => {
it('<Checkbox /> renders correctly and call on change on Press', async () => {
it('<Checkbox /> renders correctly and call on change on Press', () => {
const mockOnChange = jest.fn((checked) => checked);
render(
<Checkbox
Expand All @@ -22,9 +22,9 @@ describe('Checkbox, Radio & Switch components ', () => {
);
expect(screen.getByTestId('checkbox')).toBeOnTheScreen();
expect(screen.queryByTestId('checkbox-label')).not.toBeOnTheScreen();
expect(
screen.getByTestId('checkbox').props.accessibilityState.checked
).toBe(false);
expect(screen.getByTestId('checkbox')).toBeEnabled();

expect(screen.getByTestId('checkbox')).not.toBeChecked();
expect(screen.getByTestId('checkbox').props.accessibilityRole).toBe(
'checkbox'
);
Expand All @@ -37,7 +37,7 @@ describe('Checkbox, Radio & Switch components ', () => {
expect(mockOnChange).toHaveBeenCalledWith(true);
});

it("<CheckBox/> shouldn't change value while disabled", async () => {
it("<CheckBox/> shouldn't change value while disabled", () => {
const mockOnChange = jest.fn((checked) => checked);
render(
<Checkbox
Expand All @@ -49,11 +49,11 @@ describe('Checkbox, Radio & Switch components ', () => {
/>
);
expect(screen.getByTestId('checkbox')).toBeOnTheScreen();

expect(screen.getByTestId('checkbox')).toBeDisabled();
fireEvent.press(screen.getByTestId('checkbox'));
expect(mockOnChange).toHaveBeenCalledTimes(0);
});
it('<CheckBox/> Should render the correct label', async () => {
it('<CheckBox/> Should render the correct label', () => {
const mockOnChange = jest.fn((checked) => checked);
render(
<Checkbox
Expand All @@ -73,18 +73,18 @@ describe('Checkbox, Radio & Switch components ', () => {
expect(screen.getByTestId('checkbox').props.accessibilityRole).toBe(
'checkbox'
);

expect(screen.getByTestId('checkbox').props.accessibilityLabel).toBe(
'agree'
);

expect(screen.getByTestId('checkbox-label')?.props.children).toBe(
expect(screen.getByTestId('checkbox-label')).toHaveTextContent(
'I agree to terms and conditions'
);
fireEvent.press(screen.getByTestId('checkbox'));
expect(mockOnChange).toHaveBeenCalledTimes(0);
});

it('<Radio /> renders correctly and call on change on Press', async () => {
it('<Radio /> renders correctly and call on change on Press', () => {
const mockOnChange = jest.fn((checked) => checked);
render(
<Radio
Expand All @@ -96,18 +96,16 @@ describe('Checkbox, Radio & Switch components ', () => {
);
expect(screen.getByTestId('radio')).toBeOnTheScreen();
expect(screen.queryByTestId('radio-label')).not.toBeOnTheScreen();

expect(screen.getByTestId('radio').props.accessibilityState.checked).toBe(
false
);
expect(screen.getByTestId('radio')).toBeEnabled();
expect(screen.getByTestId('radio')).not.toBeChecked();
expect(screen.getByTestId('radio').props.accessibilityRole).toBe('radio');
expect(screen.getByTestId('radio').props.accessibilityLabel).toBe('agree');
fireEvent.press(screen.getByTestId('radio'));
expect(mockOnChange).toHaveBeenCalledTimes(1);
expect(mockOnChange).toHaveBeenCalledWith(true);
});

it('<Radio /> should render the correct label', async () => {
it('<Radio /> should render the correct label', () => {
const mockOnChange = jest.fn((checked) => checked);
render(
<Radio
Expand All @@ -120,7 +118,7 @@ describe('Checkbox, Radio & Switch components ', () => {
);
expect(screen.getByTestId('radio')).toBeOnTheScreen();
expect(screen.getByTestId('radio-label')).toBeOnTheScreen();
expect(screen.getByTestId('radio-label')?.props.children).toBe(
expect(screen.getByTestId('radio-label')).toHaveTextContent(
'I agree to terms and conditions'
);

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

it("<Radio/> shouldn't change value while disabled", async () => {
it("<Radio/> shouldn't change value while disabled", () => {
const mockOnChange = jest.fn((checked) => checked);
render(
<Radio
Expand All @@ -146,12 +144,12 @@ describe('Checkbox, Radio & Switch components ', () => {
/>
);
expect(screen.getByTestId('radio')).toBeOnTheScreen();

expect(screen.getByTestId('radio')).toBeDisabled();
fireEvent.press(screen.getByTestId('radio'));
expect(mockOnChange).toHaveBeenCalledTimes(0);
});

it('<Switch /> renders correctly and call on change on Press', async () => {
it('<Switch /> renders correctly and call on change on Press', () => {
const mockOnChange = jest.fn((checked) => checked);
render(
<Switch
Expand All @@ -163,7 +161,7 @@ describe('Checkbox, Radio & Switch components ', () => {
);
expect(screen.getByTestId('switch')).toBeOnTheScreen();
expect(screen.queryByTestId('switch-label')).not.toBeOnTheScreen();

expect(screen.getByTestId('switch')).toBeEnabled();
expect(screen.getByTestId('switch').props.accessibilityState.checked).toBe(
false
);
Expand All @@ -174,7 +172,7 @@ describe('Checkbox, Radio & Switch components ', () => {
expect(mockOnChange).toHaveBeenCalledWith(true);
});

it('<Switch /> should render the correct label', async () => {
it('<Switch /> should render the correct label', () => {
const mockOnChange = jest.fn((checked) => checked);
render(
<Switch
Expand All @@ -187,7 +185,7 @@ describe('Checkbox, Radio & Switch components ', () => {
);
expect(screen.getByTestId('switch')).toBeOnTheScreen();
expect(screen.getByTestId('switch-label')).toBeOnTheScreen();
expect(screen.getByTestId('switch-label')?.props.children).toBe(
expect(screen.getByTestId('switch-label')).toHaveTextContent(
'I agree to terms and conditions'
);
expect(screen.getByTestId('switch').props.accessibilityState.checked).toBe(
Expand All @@ -200,7 +198,7 @@ describe('Checkbox, Radio & Switch components ', () => {
expect(mockOnChange).toHaveBeenCalledWith(true);
});

it("<Switch/> shouldn't change value while disabled", async () => {
it("<Switch/> shouldn't change value while disabled", () => {
const mockOnChange = jest.fn((checked) => checked);
render(
<Switch
Expand Down
14 changes: 7 additions & 7 deletions src/ui/input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ describe('Input component ', () => {
it('should render the label correctly ', () => {
render(<Input testID="input" label="Username" />);
expect(screen.getByTestId('input')).toBeOnTheScreen();
expect(screen.getByTestId('input-label')).toBeOnTheScreen();
expect(screen.getByTestId('input-label')?.props.children).toBe('Username');

expect(screen.getByTestId('input-label')).toHaveTextContent('Username');
});

it('should render the error message correctly ', () => {
render(<Input testID="input" error="This is an error message" />);
expect(screen.getByTestId('input')).toBeOnTheScreen();
expect(screen.getByTestId('input-error')).toBeOnTheScreen();
expect(screen.getByTestId('input-error')?.props.children).toBe(

expect(screen.getByTestId('input-error')).toHaveTextContent(
'This is an error message'
);
});
Expand All @@ -46,10 +46,10 @@ describe('Input component ', () => {
/>
);
expect(screen.getByTestId('input')).toBeOnTheScreen();
expect(screen.getByTestId('input-label')).toBeOnTheScreen();
expect(screen.getByTestId('input-label')?.props.children).toBe('Username');

expect(screen.getByTestId('input-label')).toHaveTextContent('Username');
expect(screen.getByTestId('input-error')).toBeOnTheScreen();
expect(screen.getByTestId('input-error')?.props.children).toBe(
expect(screen.getByTestId('input-error')).toHaveTextContent(
'This is an error message'
);
expect(
Expand Down
4 changes: 2 additions & 2 deletions src/ui/select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Select component ', () => {
);
expect(screen.getByTestId('select-trigger')).toBeOnTheScreen();
expect(screen.getByTestId('select-label')).toBeOnTheScreen();
expect(screen.getByTestId('select-label')?.props.children).toBe('Select');
expect(screen.getByTestId('select-label')).toHaveTextContent('Select');
});

it('should render the error correctly ', () => {
Expand All @@ -56,7 +56,7 @@ describe('Select component ', () => {
);
expect(screen.getByTestId('select-trigger')).toBeOnTheScreen();
expect(screen.getByTestId('select-error')).toBeOnTheScreen();
expect(screen.getByTestId('select-error')?.props.children).toBe(
expect(screen.getByTestId('select-error')).toHaveTextContent(
'Please select an option'
);
});
Expand Down

0 comments on commit 2e3ec67

Please sign in to comment.