Skip to content

Commit

Permalink
test: add carousel test
Browse files Browse the repository at this point in the history
  • Loading branch information
tprache-pass committed Feb 17, 2025
1 parent f08c2cc commit 8c34140
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/ui/components/Carousel/Carousel.native.test.tsx
Original file line number Diff line number Diff line change
@@ -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<number>
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(
<Carousel
data={mockData}
renderItem={({ item }) => <Animated.Text>{item.text}</Animated.Text>}
currentIndex={0}
setIndex={mockSetIndex}
width={300}
progressValue={PROGRESS_VALUE}
/>
)

expect(screen.getByTestId('carousel')).toBeOnTheScreen()
})

it('should calls setIndex when scrolling occurs', async () => {
render(
<Carousel
data={mockData}
renderItem={({ item }) => <Animated.Text>{item.text}</Animated.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)
})
})

0 comments on commit 8c34140

Please sign in to comment.