Skip to content

Commit

Permalink
feat: create test for IconButton component
Browse files Browse the repository at this point in the history
  • Loading branch information
itsaladin committed Dec 21, 2022
1 parent c80a8ab commit 0d210c8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
"modulePathIgnorePatterns": [
"<rootDir>/example/node_modules",
"<rootDir>/lib/"
],
"transformIgnorePatterns": [
"node_modules/(?!(@react-native|react-native|react-native-vector-icons)/)"
]
},
"commitlint": {
Expand Down
25 changes: 25 additions & 0 deletions src/__tests__/iconButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import { cleanup, render, fireEvent } from '@testing-library/react-native';
import { IconButton } from '../components/IconButton';

const onPress = jest.fn();
const icon = 'rocket';
const iconButtonTestId = 'IconButtonTestId';
afterEach(cleanup);

describe('IconButton component', () => {
it('IconButton component should render', () => {
const { container } = render(<IconButton icon={icon} onPress={onPress} />);
expect(container).toBeTruthy();
});

it('IconButton component should call the onPress callback when pressed', () => {
const { getByTestId } = render(
<IconButton icon={icon} onPress={onPress} />
);
const button = getByTestId(iconButtonTestId);

fireEvent.press(button);
expect(onPress).toHaveBeenCalled();
});
});
2 changes: 2 additions & 0 deletions src/components/IconButton/IconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { IconButtonProps } from './types';

const IconButton: FCWithChildren<IconButtonProps> = ({
style,
testID,
icon,
color,
size,
Expand All @@ -21,6 +22,7 @@ const IconButton: FCWithChildren<IconButtonProps> = ({
return (
<TouchableWithoutFeedback
onPress={disabled ? undefined : onPress}
testID={testID || 'IconButtonTestId'}
style={style}
{...rest}
>
Expand Down
1 change: 1 addition & 0 deletions src/components/IconButton/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { GestureResponderEvent, StyleProp, TextStyle } from 'react-native';
export interface IconButtonProps {
style?: StyleProp<TextStyle>;
fontSize?: number;
testID?: string;
icon: string;
size?: number;
color?: string;
Expand Down

0 comments on commit 0d210c8

Please sign in to comment.