|
| 1 | +import { render, screen } from '@testing-library/react-native'; |
| 2 | +import React, { createRef } from 'react'; |
| 3 | +import { getAnimatedStyle } from 'react-native-reanimated'; |
| 4 | + |
| 5 | +import type { ProgressBarRef } from './progress-bar'; |
| 6 | +import { ProgressBar } from './progress-bar'; |
| 7 | + |
| 8 | +describe('ProgressBar component', () => { |
| 9 | + it('renders correctly', () => { |
| 10 | + render(<ProgressBar className="custom-class" />); |
| 11 | + expect(screen.getByTestId('progress-bar-container')).toBeTruthy(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('sets initial progress correctly', () => { |
| 15 | + render(<ProgressBar initialProgress={50} />); |
| 16 | + const progressBar = screen.getByTestId('progress-bar'); |
| 17 | + expect(progressBar.props.style).toEqual( |
| 18 | + expect.objectContaining({ width: '50%' }) |
| 19 | + ); |
| 20 | + }); |
| 21 | + |
| 22 | + it('setProgress function works correctly', async () => { |
| 23 | + const finalValue = 75; |
| 24 | + const progressAnimationDuration = 250; |
| 25 | + const ref = createRef<ProgressBarRef>(); |
| 26 | + render(<ProgressBar ref={ref} initialProgress={0} />); |
| 27 | + const progressBar = screen.getByTestId('progress-bar'); |
| 28 | + |
| 29 | + // Call setProgress and check the updated value |
| 30 | + if (ref.current) { |
| 31 | + expect(getAnimatedStyle(progressBar)).toMatchObject({ width: '0%' }); |
| 32 | + jest.useFakeTimers(); |
| 33 | + ref.current.setProgress(finalValue); |
| 34 | + jest.advanceTimersByTime(progressAnimationDuration); // Duration of the animation |
| 35 | + const updatedProgressBar = await screen.findByTestId('progress-bar'); |
| 36 | + expect(getAnimatedStyle(updatedProgressBar)).toMatchObject({ |
| 37 | + width: `${finalValue}%`, |
| 38 | + }); |
| 39 | + } |
| 40 | + }); |
| 41 | +}); |
0 commit comments