Skip to content

Commit

Permalink
test(progressbar): add-test
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandatoledo committed Jul 26, 2024
1 parent faca249 commit de9b9ef
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ jobs:
-Dsonar.projectKey=${{ secrets.SONAR_PROJECT }}
-Dsonar.sonar.sourceEncoding=UTF-8
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
-Dsonar.coverage.exclusions=**/storage/**,**/**.config.js,**/*.test.tsx, **/icons,
-Dsonar.coverage.exclusions=**/storage/**,**/**.config.js,**/*.test.tsx,**/icons/**
2 changes: 2 additions & 0 deletions src/constants/test-ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const PROGRESS_BAR = 'progress-bar';
export const PROGRESS_BAR_CONTAINER = 'progress-bar-container';
41 changes: 41 additions & 0 deletions src/ui/progress-bar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { render, screen } from '@testing-library/react-native';
import React, { createRef } from 'react';
import { getAnimatedStyle } from 'react-native-reanimated';

import { PROGRESS_BAR, PROGRESS_BAR_CONTAINER } from '@/constants/test-ids';

import type { ProgressBarRef } from './progress-bar';
import { ProgressBar } from './progress-bar';

describe('ProgressBar component', () => {
it('renders correctly', () => {
render(<ProgressBar className="custom-class" />);
expect(screen.getByTestId(PROGRESS_BAR_CONTAINER)).toBeTruthy();
});

it('sets initial progress correctly', () => {
render(<ProgressBar initialProgress={50} />);
const progressBar = screen.getByTestId(PROGRESS_BAR);
expect(progressBar.props.style).toEqual(
expect.objectContaining({ width: '50%' })
);
});

it('setProgress function works correctly', async () => {
const ref = createRef<ProgressBarRef>();
render(<ProgressBar ref={ref} initialProgress={0} />);
const progressBar = screen.getByTestId(PROGRESS_BAR);

// Call setProgress and check the updated value
if (ref.current) {
expect(getAnimatedStyle(progressBar)).toMatchObject({ width: '0%' });
jest.useFakeTimers();
ref.current.setProgress(75);
jest.advanceTimersByTime(250); // Duration of the animation
const updatedProgressBar = await screen.findByTestId(PROGRESS_BAR);
expect(getAnimatedStyle(updatedProgressBar)).toMatchObject({
width: '75%',
});
}
});
});
9 changes: 7 additions & 2 deletions src/ui/progress-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Animated, {
} from 'react-native-reanimated';
import { twMerge } from 'tailwind-merge';

import { PROGRESS_BAR, PROGRESS_BAR_CONTAINER } from '@/constants/test-ids';

type Props = {
initialProgress?: number;
className?: string;
Expand Down Expand Up @@ -43,8 +45,11 @@ export const ProgressBar = forwardRef<ProgressBarRef, Props>(
};
});
return (
<View className={twMerge(` bg-[#EAEAEA]`, className)}>
<Animated.View style={style} />
<View
testID={PROGRESS_BAR_CONTAINER}
className={twMerge(` bg-[#EAEAEA]`, className)}
>
<Animated.View testID={PROGRESS_BAR} style={style} />
</View>
);
}
Expand Down

0 comments on commit de9b9ef

Please sign in to comment.