-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathreact-native-animated.test.tsx
70 lines (59 loc) · 1.72 KB
/
react-native-animated.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import * as React from 'react';
import type { ViewStyle } from 'react-native';
import { Animated } from 'react-native';
import { act, render, screen } from '..';
type AnimatedViewProps = {
fadeInDuration?: number;
style?: ViewStyle;
children: React.ReactNode;
useNativeDriver?: boolean;
};
function AnimatedView(props: AnimatedViewProps) {
const fadeAnim = React.useRef(new Animated.Value(0)).current; // Initial value for opacity: 0
React.useEffect(() => {
Animated.timing(fadeAnim, {
toValue: 1,
duration: props.fadeInDuration ?? 250,
useNativeDriver: props.useNativeDriver ?? true,
}).start();
}, [fadeAnim, props.fadeInDuration, props.useNativeDriver]);
return (
<Animated.View
style={{
...props.style,
opacity: fadeAnim,
}}
>
{props.children}
</Animated.View>
);
}
describe('AnimatedView', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('should use native driver when useNativeDriver is true', () => {
render(
<AnimatedView fadeInDuration={250} useNativeDriver={true}>
Test
</AnimatedView>,
);
expect(screen.root).toHaveStyle({ opacity: 0 });
act(() => jest.advanceTimersByTime(250));
// This stopped working in tests in RN 0.77
// expect(screen.root).toHaveStyle({ opacity: 0 });
});
it('should not use native driver when useNativeDriver is false', () => {
render(
<AnimatedView fadeInDuration={250} useNativeDriver={false}>
Test
</AnimatedView>,
);
expect(screen.root).toHaveStyle({ opacity: 0 });
act(() => jest.advanceTimersByTime(250));
expect(screen.root).toHaveStyle({ opacity: 1 });
});
});