boxons/components/shared/__tests__/Card.test.tsx

75 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-10-13 11:29:36 +00:00
import React from 'react';
import { fireEvent } from '@testing-library/react-native';
import { render } from '@/components/testUtils';
2025-04-08 21:29:14 +00:00
import Card, { CardProps } from '@/components/shared/Card';
2024-10-13 11:29:36 +00:00
import { Text } from 'react-native';
import { theme } from '@/app/shared/theme';
2025-04-08 21:29:14 +00:00
type RenderCardProps = Omit<CardProps, 'children'> & { children?: React.ReactNode };
const renderComponent = ({ backgroundColor, onPress, children = 'content'}: RenderCardProps) => {
2024-10-13 11:29:36 +00:00
const base = render(
<Card backgroundColor={backgroundColor} onPress={onPress} testID='card'>
2025-04-08 21:29:14 +00:00
<Text>{children}</Text>
2024-10-13 11:29:36 +00:00
</Card>
);
return base;
};
describe('[Component] Card', () => {
test('renders correctly', () => {
const card = renderComponent({ backgroundColor: 'red' });
expect(card).toMatchSnapshot();
});
test('renders children correctly', () => {
2025-04-08 21:29:14 +00:00
const { getByText } = renderComponent({ backgroundColor: 'red', children: 'Test Content'});
2024-10-13 11:29:36 +00:00
expect(getByText('Test Content')).toBeTruthy();
});
test('applies the correct background color', () => {
const { getByTestId } = renderComponent({ backgroundColor: 'green'});
const card = getByTestId('card');
expect(card?.props.style[0].backgroundColor).toEqual(theme.colors.fixed.green);
2024-10-13 11:29:36 +00:00
});
test('does not render as TouchableOpacity when onPress is not provided', () => {
const { queryByTestId } = renderComponent({ backgroundColor: 'red' });
2025-04-08 21:29:14 +00:00
// TouchableOpacity should not exist
2024-10-13 11:29:36 +00:00
expect(queryByTestId('card').props.onPress).toBeUndefined();
});
2025-04-08 21:29:14 +00:00
test('handles undefined onPress gracefully when pressed', () => {
// Ce test vérifie que le comportement est correct quand onPress est undefined
const { getByTestId } = renderComponent({
backgroundColor: 'red',
onPress: undefined,
children: 'Press me safely'
});
// Vérifie qu'aucune erreur n'est levée lors de l'appui
expect(() => {
fireEvent.press(getByTestId('card'));
}).not.toThrow();
});
2024-10-13 11:29:36 +00:00
test('calls onPress when pressed', () => {
const onPressMock = jest.fn();
const { getByTestId } = renderComponent({
backgroundColor: 'green',
onPress: onPressMock,
2025-04-08 21:29:14 +00:00
children: 'Press me'
2024-10-13 11:29:36 +00:00
});
fireEvent.press(getByTestId('card'));
expect(onPressMock).toHaveBeenCalled();
});
});