-
-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathissue-899-parallax-reverse-loop.test.tsx
More file actions
131 lines (106 loc) · 4.24 KB
/
Copy pathissue-899-parallax-reverse-loop.test.tsx
File metadata and controls
131 lines (106 loc) · 4.24 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React from "react";
import { StyleSheet } from "react-native";
import type { PanGesture } from "react-native-gesture-handler";
import { Gesture, State } from "react-native-gesture-handler";
import Animated, { useDerivedValue, useSharedValue } from "react-native-reanimated";
import { act, render, waitFor } from "@testing-library/react-native";
import { fireGestureHandler, getByGestureTestId } from "react-native-gesture-handler/jest-utils";
import Carousel from "./Carousel";
{
const cfg = (global as any).__reanimatedLoggerConfig as
| { logFunction: (data: { level: number; message: string }) => void }
| undefined;
if (cfg) {
const originalLog = cfg.logFunction;
cfg.logFunction = (data) => {
if (data.message.includes("measure() cannot be used with Jest")) return;
originalLog(data);
};
}
}
const slideWidth = 300;
const slideHeight = 200;
const gestureTestId = "rnrc-gesture-handler";
const realPan = Gesture.Pan();
jest.spyOn(Gesture, "Pan").mockImplementation(() => realPan.withTestId(gestureTestId));
describe("issue #899 parallax reverse loop regression", () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
act(() => {
jest.runOnlyPendingTimers();
});
jest.useRealTimers();
jest.clearAllTimers();
});
it("preserves renderItem styles and props when looping backward from index 0 in parallax mode", async () => {
const progress = { current: 0 };
const data = Array.from({ length: 30 }, (_, index) => ({
id: `item-${index}`,
color: index === 29 ? "#111111" : `rgb(${index}, ${index}, ${index})`,
}));
const Wrapper = () => {
const progressAnimVal = useSharedValue(progress.current);
useDerivedValue(() => {
progress.current = progressAnimVal.value;
}, [progressAnimVal]);
return (
<Carousel
data={data}
defaultIndex={0}
loop
mode="parallax"
windowSize={5}
style={{ width: slideWidth, height: slideHeight }}
onProgressChange={(_, absoluteProgress) => {
progressAnimVal.value = absoluteProgress;
}}
renderItem={({ item, index }) => (
<Animated.View
testID={`issue-899-item-${index}`}
accessibilityLabel={`issue-899-label-${index}`}
style={{
width: slideWidth,
height: slideHeight,
backgroundColor: item.color,
}}
/>
)}
/>
);
};
const { getByTestId, queryByTestId } = render(<Wrapper />);
await waitFor(() => {
expect(getByTestId("issue-899-item-0")).toBeTruthy();
});
const prerenderedWrappedItem = getByTestId("issue-899-item-29");
const prerenderedWrappedStyle = StyleSheet.flatten(prerenderedWrappedItem.props.style);
expect(prerenderedWrappedItem.props.accessibilityLabel).toBe("issue-899-label-29");
expect(prerenderedWrappedStyle.backgroundColor).toBe("#111111");
fireGestureHandler<PanGesture>(getByGestureTestId(gestureTestId), [
{ state: State.BEGAN, translationX: 0, velocityX: slideWidth },
]);
fireGestureHandler<PanGesture>(getByGestureTestId(gestureTestId), [
{ state: State.ACTIVE, translationX: slideWidth * 0.7, velocityX: slideWidth },
]);
expect(queryByTestId("issue-899-item-29")).toBeTruthy();
await waitFor(() => {
expect(queryByTestId("issue-899-item-29")).toBeTruthy();
});
const activeWrappedItem = getByTestId("issue-899-item-29");
const activeWrappedStyle = StyleSheet.flatten(activeWrappedItem.props.style);
expect(activeWrappedItem.props.accessibilityLabel).toBe("issue-899-label-29");
expect(activeWrappedStyle.backgroundColor).toBe("#111111");
fireGestureHandler<PanGesture>(getByGestureTestId(gestureTestId), [
{ state: State.END, translationX: slideWidth, velocityX: slideWidth },
]);
await waitFor(() => {
expect(progress.current).toBe(29);
});
const wrappedItem = getByTestId("issue-899-item-29");
const flattenedStyle = StyleSheet.flatten(wrappedItem.props.style);
expect(wrappedItem.props.accessibilityLabel).toBe("issue-899-label-29");
expect(flattenedStyle.backgroundColor).toBe("#111111");
});
});