|
| 1 | +import * as React from 'react'; |
| 2 | +import { Animated, ViewStyle } from 'react-native'; |
| 3 | +import { act, render, screen } from '..'; |
| 4 | + |
| 5 | +type AnimatedViewProps = { |
| 6 | + fadeInDuration?: number; |
| 7 | + style?: ViewStyle; |
| 8 | + children: React.ReactNode; |
| 9 | + useNativeDriver?: boolean; |
| 10 | +}; |
| 11 | + |
| 12 | +function AnimatedView(props: AnimatedViewProps) { |
| 13 | + const fadeAnim = React.useRef(new Animated.Value(0)).current; // Initial value for opacity: 0 |
| 14 | + |
| 15 | + React.useEffect(() => { |
| 16 | + Animated.timing(fadeAnim, { |
| 17 | + toValue: 1, |
| 18 | + duration: props.fadeInDuration ?? 250, |
| 19 | + useNativeDriver: props.useNativeDriver ?? true, |
| 20 | + }).start(); |
| 21 | + }, [fadeAnim, props.fadeInDuration, props.useNativeDriver]); |
| 22 | + |
| 23 | + return ( |
| 24 | + <Animated.View |
| 25 | + style={{ |
| 26 | + ...props.style, |
| 27 | + opacity: fadeAnim, |
| 28 | + }} |
| 29 | + > |
| 30 | + {props.children} |
| 31 | + </Animated.View> |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +describe('AnimatedView', () => { |
| 36 | + beforeEach(() => { |
| 37 | + jest.useFakeTimers(); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + jest.useRealTimers(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should use native driver when useNativeDriver is true', async () => { |
| 45 | + render( |
| 46 | + <AnimatedView fadeInDuration={250} useNativeDriver={true}> |
| 47 | + Test |
| 48 | + </AnimatedView>, |
| 49 | + ); |
| 50 | + expect(screen.root).toHaveStyle({ opacity: 0 }); |
| 51 | + |
| 52 | + await act(() => jest.advanceTimersByTime(250)); |
| 53 | + expect(screen.root).toHaveStyle({ opacity: 1 }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should not use native driver when useNativeDriver is false', async () => { |
| 57 | + render( |
| 58 | + <AnimatedView fadeInDuration={250} useNativeDriver={false}> |
| 59 | + Test |
| 60 | + </AnimatedView>, |
| 61 | + ); |
| 62 | + expect(screen.root).toHaveStyle({ opacity: 0 }); |
| 63 | + |
| 64 | + await act(() => jest.advanceTimersByTime(250)); |
| 65 | + expect(screen.root).toHaveStyle({ opacity: 1 }); |
| 66 | + }); |
| 67 | +}); |
0 commit comments