Skeleton.test.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { render, screen } from '@testing-library/react-native';
  2. import { Text } from 'react-native';
  3. import TestAppWrapper from '@/tests/TestAppWrapper';
  4. import SkeletonLoader from './Skeleton';
  5. const WAIT = 800;
  6. describe('SkeletonLoader', () => {
  7. beforeAll(() => {
  8. jest.useFakeTimers();
  9. });
  10. it('renders children when not loading', () => {
  11. render(
  12. <SkeletonLoader loading={false}>
  13. <Text>Loaded Content</Text>
  14. </SkeletonLoader>,
  15. {
  16. wrapper: TestAppWrapper,
  17. },
  18. );
  19. expect(screen.getByText('Loaded Content')).toBeTruthy();
  20. });
  21. it('renders skeleton when loading', () => {
  22. render(<SkeletonLoader loading />, {
  23. wrapper: TestAppWrapper,
  24. });
  25. const skeleton = screen.getByTestId('skeleton-loader');
  26. jest.advanceTimersByTime(WAIT);
  27. expect(skeleton).toBeTruthy();
  28. });
  29. it('applies correct height and width', () => {
  30. render(<SkeletonLoader height={50} loading width={100} />, {
  31. wrapper: TestAppWrapper,
  32. });
  33. const skeleton = screen.getByTestId('skeleton-loader');
  34. const animatedStyle: {
  35. value: { opacity: number };
  36. } = skeleton.props.jestAnimatedStyle as {
  37. value: { opacity: number };
  38. };
  39. // TODO: use toHaveAnimatedStyle for better API but for now there is an issue with the library
  40. // expect(skeleton).toHaveAnimatedStyle({
  41. // opacity: 0.2,
  42. // });
  43. expect(animatedStyle.value).toEqual({
  44. opacity: 0.2,
  45. });
  46. jest.advanceTimersByTime(WAIT);
  47. expect(animatedStyle.value).toEqual({
  48. opacity: 1,
  49. });
  50. // TODO: use toHaveAnimatedStyle for better API but for now there is an issue with the library
  51. // expect(skeleton).toHaveAnimatedStyle({
  52. // opacity: 1,
  53. // });
  54. expect(skeleton).toHaveStyle({
  55. backgroundColor: '#A1A1A1',
  56. borderRadius: 4,
  57. height: 50,
  58. width: 100,
  59. });
  60. });
  61. });