diff --git a/src/ui/components/Carousel/Carousel.native.test.tsx b/src/ui/components/Carousel/Carousel.native.test.tsx new file mode 100644 index 00000000000..b730dd8d5f0 --- /dev/null +++ b/src/ui/components/Carousel/Carousel.native.test.tsx @@ -0,0 +1,72 @@ +import React from 'react' +import Animated, { SharedValue } from 'react-native-reanimated' + +import { act, fireEvent, render, screen } from 'tests/utils' + +import { Carousel } from './Carousel' + +jest.mock('react-native-reanimated', () => { + const Reanimated = jest.requireActual('react-native-reanimated/mock') + return { + ...Reanimated, + useAnimatedScrollHandler: jest.fn(), + useSharedValue: jest.fn(() => ({ value: 0 })), + useAnimatedRef: jest.fn(), + } +}) + +const PROGRESS_VALUE = { value: 0 } as SharedValue +const MOCKED_CAROUSEL_WIDTH = 300 +const MOCKED_CAROUSEL_HEIGHT = 200 + +describe('Carousel Component', () => { + const mockSetIndex = jest.fn() + + const mockData = [ + { id: '1', text: 'Item 1' }, + { id: '2', text: 'Item 2' }, + { id: '3', text: 'Item 3' }, + ] + + it('should renders correctly', () => { + render( + {item.text}} + currentIndex={0} + setIndex={mockSetIndex} + width={300} + progressValue={PROGRESS_VALUE} + /> + ) + + expect(screen.getByTestId('carousel')).toBeOnTheScreen() + }) + + it('should calls setIndex when scrolling occurs', async () => { + render( + {item.text}} + currentIndex={0} + setIndex={mockSetIndex} + width={MOCKED_CAROUSEL_WIDTH} + progressValue={PROGRESS_VALUE} + /> + ) + + const carousel = await screen.findByTestId('carousel') + + await act(async () => { + fireEvent.scroll(carousel, { + nativeEvent: { + contentOffset: { x: MOCKED_CAROUSEL_WIDTH, y: 0 }, + layoutMeasurement: { width: MOCKED_CAROUSEL_WIDTH, height: MOCKED_CAROUSEL_HEIGHT }, + contentSize: { width: MOCKED_CAROUSEL_WIDTH * 3, height: MOCKED_CAROUSEL_HEIGHT }, + }, + }) + }) + + expect(mockSetIndex).toHaveBeenCalledTimes(1) + }) +})