Skip to content

Commit

Permalink
add scroll test for carousel component
Browse files Browse the repository at this point in the history
  • Loading branch information
jiji14 committed Jun 4, 2024
1 parent 60ca15c commit 5cf378c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
30 changes: 29 additions & 1 deletion www/__tests__/Carousel.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render } from '@testing-library/react-native';
import { render, fireEvent } from '@testing-library/react-native';
import { View } from 'react-native';
import Carousel from '../js/components/Carousel';

Expand All @@ -23,4 +23,32 @@ describe('Carousel component', () => {
expect(renderedChild1).toBeTruthy();
expect(renderedChild2).toBeTruthy();
});

it('scrolls correctly to the next card', () => {
const { getByTestId } = render(
<Carousel cardWidth={cardWidth} cardMargin={cardMargin}>
{child1}
{child2}
</Carousel>,
);

const scrollView = getByTestId('carousel');
fireEvent.scroll(scrollView, {
// Scroll to the second card
nativeEvent: {
contentOffset: {
x: cardWidth + cardMargin,
},
contentSize: {
width: (cardWidth + cardMargin) * 2,
},
layoutMeasurement: {
width: cardWidth + cardMargin,
},
},
});

expect(scrollView.props.horizontal).toBe(true);
expect(scrollView.props.snapToInterval).toBe(cardWidth + cardMargin);
});
});
1 change: 1 addition & 0 deletions www/js/components/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Carousel = ({ children, cardWidth, cardMargin }: Props) => {
const numCards = React.Children.count(children);
return (
<ScrollView
testID="carousel"
horizontal={true}
decelerationRate={0}
snapToInterval={cardWidth + cardMargin}
Expand Down

0 comments on commit 5cf378c

Please sign in to comment.