Skip to content

Commit 496bef0

Browse files
Merge pull request #13 from rootstrap/test/progress-bar
test: add test for progress bar
2 parents 618edcc + ade9373 commit 496bef0

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/ui/progress-bar.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
});

src/ui/progress-bar.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ export const ProgressBar = forwardRef<ProgressBarRef, Props>(
4343
};
4444
});
4545
return (
46-
<View className={twMerge(` bg-[#EAEAEA]`, className)}>
47-
<Animated.View style={style} />
46+
<View
47+
testID={'progress-bar-container'}
48+
className={twMerge(` bg-[#EAEAEA]`, className)}
49+
>
50+
<Animated.View testID={'progress-bar'} style={style} />
4851
</View>
4952
);
5053
}

0 commit comments

Comments
 (0)