react-native-reanimated-carousel 5.0.0-beta.7 → 5.0.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +32 -2
  2. package/lib/commonjs/components/CarouselLayout.js +1 -1
  3. package/lib/commonjs/components/CarouselLayout.js.map +1 -1
  4. package/lib/module/components/CarouselLayout.js +1 -1
  5. package/lib/module/components/CarouselLayout.js.map +1 -1
  6. package/lib/typescript/components/CarouselLayout.d.ts.map +1 -1
  7. package/package.json +4 -2
  8. package/src/components/CarouselLayout.tsx +3 -4
  9. package/src/components/Carousel.test.tsx +0 -1215
  10. package/src/components/LazyView.test.tsx +0 -61
  11. package/src/components/Pagination/Pagination.test.tsx +0 -178
  12. package/src/components/ScrollViewGesture.test.tsx +0 -155
  13. package/src/components/issue-899-parallax-reverse-loop.test.tsx +0 -131
  14. package/src/components/rnr-demo.test.tsx +0 -53
  15. package/src/hooks/useAutoPlay.test.ts +0 -194
  16. package/src/hooks/useCarouselController.test.tsx +0 -1158
  17. package/src/hooks/useCheckMounted.test.ts +0 -47
  18. package/src/hooks/useCommonVariables.test.tsx +0 -384
  19. package/src/hooks/useInitProps.test.tsx +0 -134
  20. package/src/hooks/useLayoutConfig.test.tsx +0 -247
  21. package/src/hooks/useOffsetX.test.ts +0 -54
  22. package/src/hooks/useOnProgressChange.test.tsx +0 -270
  23. package/src/hooks/usePanGestureProxy.test.tsx +0 -368
  24. package/src/hooks/usePropsErrorBoundary.test.tsx +0 -57
  25. package/src/hooks/useUpdateGestureConfig.test.ts +0 -89
  26. package/src/hooks/useVisibleRanges.test.tsx +0 -347
  27. package/src/layouts/parallax.test.ts +0 -239
  28. package/src/layouts/stack.test.ts +0 -252
  29. package/src/store/index.test.tsx +0 -314
  30. package/src/utils/compute-gesture-translation.test.ts +0 -70
  31. package/src/utils/compute-offset-if-data-changed.test.ts +0 -133
  32. package/src/utils/compute-offset-if-size-changed.test.ts +0 -78
  33. package/src/utils/computed-with-auto-fill-data.test.ts +0 -298
  34. package/src/utils/deal-with-animation.test.ts +0 -181
  35. package/src/utils/handleroffset-direction.test.ts +0 -124
  36. package/src/utils/index.test.ts +0 -90
  37. package/src/utils/log.test.ts +0 -134
  38. package/src/utils/sanitize-animation-style.test.ts +0 -40
@@ -1,247 +0,0 @@
1
- import React from "react";
2
- import { View } from "react-native";
3
-
4
- import { renderHook } from "@testing-library/react-hooks";
5
-
6
- import { useLayoutConfig } from "./useLayoutConfig";
7
-
8
- describe("useLayoutConfig", () => {
9
- const defaultProps = {
10
- size: 300,
11
- vertical: false,
12
- };
13
- it("should return normal layout by default", () => {
14
- const { result } = renderHook(() =>
15
- useLayoutConfig({
16
- ...defaultProps,
17
- data: [],
18
- renderItem: () => <View />,
19
- loop: false,
20
- autoFillData: false,
21
- defaultIndex: 0,
22
- autoPlayInterval: 0,
23
- scrollAnimationDuration: 0,
24
- width: 0,
25
- height: 0,
26
- rawData: [],
27
- dataLength: 0,
28
- rawDataLength: 0,
29
- })
30
- );
31
-
32
- expect(result.current).toBeDefined();
33
- expect(typeof result.current).toBe("function");
34
- });
35
-
36
- it("should handle parallax mode", () => {
37
- const props = {
38
- ...defaultProps,
39
- mode: "parallax" as const,
40
- modeConfig: {
41
- parallaxScrollingScale: 0.9,
42
- parallaxScrollingOffset: 50,
43
- parallaxAdjacentItemScale: 0.8,
44
- },
45
- };
46
-
47
- const { result } = renderHook(() =>
48
- useLayoutConfig({
49
- ...props,
50
- data: [],
51
- renderItem: () => <View />,
52
- loop: false,
53
- autoFillData: false,
54
- defaultIndex: 0,
55
- autoPlayInterval: 0,
56
- scrollAnimationDuration: 0,
57
- width: 0,
58
- height: 0,
59
- rawData: [],
60
- dataLength: 0,
61
- rawDataLength: 0,
62
- })
63
- );
64
- const style = result.current(0); // Test with offset 0
65
-
66
- expect(style.transform).toBeDefined();
67
- expect(style.transform).toContainEqual({ translateX: 0 });
68
- expect(style.transform).toContainEqual({ scale: 0.9 });
69
- });
70
-
71
- it("should handle horizontal-stack mode", () => {
72
- const props = {
73
- ...defaultProps,
74
- mode: "horizontal-stack" as const,
75
- modeConfig: {
76
- snapDirection: "left" as const,
77
- showLength: 3,
78
- },
79
- };
80
-
81
- const { result } = renderHook(() =>
82
- useLayoutConfig({
83
- ...props,
84
- data: [],
85
- renderItem: () => <View />,
86
- loop: false,
87
- autoFillData: false,
88
- defaultIndex: 0,
89
- autoPlayInterval: 0,
90
- scrollAnimationDuration: 0,
91
- width: 0,
92
- height: 0,
93
- rawData: [],
94
- dataLength: 0,
95
- rawDataLength: 0,
96
- })
97
- );
98
- const style = result.current(0); // Test with offset 0
99
-
100
- expect(style.transform).toBeDefined();
101
- expect(style.zIndex).toBeDefined();
102
- expect(style.opacity).toBeDefined();
103
- });
104
-
105
- it("should handle vertical-stack mode", () => {
106
- const props = {
107
- ...defaultProps,
108
- mode: "vertical-stack" as const,
109
- modeConfig: {
110
- snapDirection: "left" as const,
111
- showLength: 3,
112
- },
113
- };
114
-
115
- const { result } = renderHook(() =>
116
- useLayoutConfig({
117
- ...props,
118
- data: [],
119
- renderItem: () => <View />,
120
- loop: false,
121
- autoFillData: false,
122
- defaultIndex: 0,
123
- autoPlayInterval: 0,
124
- scrollAnimationDuration: 0,
125
- width: 0,
126
- height: 0,
127
- rawData: [],
128
- dataLength: 0,
129
- rawDataLength: 0,
130
- })
131
- );
132
- const style = result.current(0); // Test with offset 0
133
-
134
- expect(style.transform).toBeDefined();
135
- expect(style.zIndex).toBeDefined();
136
- expect(style.opacity).toBeDefined();
137
- });
138
-
139
- it("should handle vertical orientation", () => {
140
- const props = {
141
- ...defaultProps,
142
- vertical: true,
143
- };
144
-
145
- const { result } = renderHook(() =>
146
- useLayoutConfig({
147
- ...props,
148
- data: [],
149
- renderItem: () => <View />,
150
- loop: false,
151
- autoFillData: false,
152
- defaultIndex: 0,
153
- autoPlayInterval: 0,
154
- scrollAnimationDuration: 0,
155
- width: 0,
156
- height: 0,
157
- rawData: [],
158
- dataLength: 0,
159
- rawDataLength: 0,
160
- })
161
- );
162
- const style = result.current(0); // Test with offset 0
163
-
164
- expect(style.transform).toBeDefined();
165
- expect(style.transform).toContainEqual({ translateY: 0 });
166
- });
167
-
168
- it("should handle different offsets", () => {
169
- const { result } = renderHook(() =>
170
- useLayoutConfig({
171
- ...defaultProps,
172
- data: [],
173
- renderItem: () => <View />,
174
- loop: false,
175
- autoFillData: false,
176
- defaultIndex: 0,
177
- autoPlayInterval: 0,
178
- scrollAnimationDuration: 0,
179
- width: 0,
180
- height: 0,
181
- rawData: [],
182
- dataLength: 0,
183
- rawDataLength: 0,
184
- })
185
- );
186
-
187
- const style1 = result.current(-1); // Previous item
188
- const style2 = result.current(0); // Current item
189
- const style3 = result.current(1); // Next item
190
-
191
- expect(style1.transform).toContainEqual({ translateX: -300 });
192
- expect(style2.transform).toContainEqual({ translateX: 0 });
193
- expect(style3.transform).toContainEqual({ translateX: 300 });
194
- });
195
-
196
- it("should memoize layout function", () => {
197
- const { result, rerender } = renderHook(() =>
198
- useLayoutConfig({
199
- ...defaultProps,
200
- data: [],
201
- renderItem: () => <View />,
202
- loop: false,
203
- autoFillData: false,
204
- defaultIndex: 0,
205
- autoPlayInterval: 0,
206
- scrollAnimationDuration: 0,
207
- width: 0,
208
- height: 0,
209
- rawData: [],
210
- dataLength: 0,
211
- rawDataLength: 0,
212
- })
213
- );
214
- const firstResult = result.current;
215
-
216
- rerender();
217
- expect(result.current).toBe(firstResult);
218
- });
219
-
220
- it("should update layout when props change", () => {
221
- const { result, rerender } = renderHook(
222
- (props) =>
223
- useLayoutConfig({
224
- ...props,
225
- data: [],
226
- renderItem: () => <View />,
227
- loop: false,
228
- autoFillData: false,
229
- defaultIndex: 0,
230
- autoPlayInterval: 0,
231
- scrollAnimationDuration: 0,
232
- width: 0,
233
- height: 0,
234
- rawData: [],
235
- dataLength: 0,
236
- rawDataLength: 0,
237
- }),
238
- {
239
- initialProps: defaultProps,
240
- }
241
- );
242
- const firstResult = result.current;
243
-
244
- rerender({ ...defaultProps, size: 400 });
245
- expect(result.current).not.toBe(firstResult);
246
- });
247
- });
@@ -1,54 +0,0 @@
1
- import { useSharedValue } from "react-native-reanimated";
2
-
3
- import { renderHook } from "@testing-library/react-hooks";
4
-
5
- import type { IOpts } from "./useOffsetX";
6
- import { useOffsetX } from "./useOffsetX";
7
- import type { IVisibleRanges } from "./useVisibleRanges";
8
-
9
- describe("useSharedValue", () => {
10
- it("should return the correct values", async () => {
11
- const hook = renderHook(() => {
12
- const range = useSharedValue({
13
- negativeRange: [7, 9],
14
- positiveRange: [0, 3],
15
- }) as IVisibleRanges;
16
- const inputs: Array<{
17
- config: IOpts;
18
- range: IVisibleRanges;
19
- }> = Array.from({ length: 10 }).map((_, index) => ({
20
- config: {
21
- dataLength: 10,
22
- handlerOffset: useSharedValue(-0),
23
- index,
24
- loop: false,
25
- size: 393,
26
- },
27
- range,
28
- }));
29
-
30
- return inputs.map((input) => {
31
- const { config, range } = input;
32
-
33
- return useOffsetX(config, range);
34
- });
35
- });
36
-
37
- const expected = hook.result.current.map((v) => v.value).slice();
38
-
39
- expect(expected).toMatchInlineSnapshot(`
40
- [
41
- 0,
42
- 393,
43
- 786,
44
- 1179,
45
- 9007199254740991,
46
- 9007199254740991,
47
- 9007199254740991,
48
- 2751,
49
- 3144,
50
- 3537,
51
- ]
52
- `);
53
- });
54
- });
@@ -1,270 +0,0 @@
1
- import { useSharedValue } from "react-native-reanimated";
2
-
3
- import { renderHook } from "@testing-library/react-hooks";
4
-
5
- import { useOnProgressChange } from "./useOnProgressChange";
6
-
7
- // Mock Reanimated and Easing
8
- jest.mock("react-native-reanimated", () => {
9
- const React = jest.requireActual("react");
10
- let reactionCallback: ((value: any) => void) | null = null;
11
- let reactionRegistrationCount = 0;
12
-
13
- return {
14
- useSharedValue: jest.fn((initialValue) => ({
15
- value: initialValue,
16
- })),
17
- useAnimatedReaction: jest.fn((prepare, react, dependencies) => {
18
- React.useEffect(() => {
19
- reactionRegistrationCount += 1;
20
- reactionCallback = react;
21
- react(prepare());
22
- return () => {
23
- if (reactionCallback === react) reactionCallback = null;
24
- };
25
- }, dependencies);
26
- }),
27
- Easing: {
28
- bezier: () => ({
29
- factory: () => 0,
30
- }),
31
- },
32
- // Export the helper function for testing
33
- __triggerReaction: (value: any) => {
34
- if (reactionCallback) reactionCallback(value);
35
- },
36
- __getReactionRegistrationCount: () => reactionRegistrationCount,
37
- __resetReactionRegistrationCount: () => {
38
- reactionRegistrationCount = 0;
39
- },
40
- };
41
- });
42
-
43
- jest.mock("react-native-worklets", () => {
44
- const mockScheduleOnRN = jest.fn((fn, ...args) => fn(...args));
45
- return {
46
- scheduleOnRN: mockScheduleOnRN,
47
- mockScheduleOnRN,
48
- };
49
- });
50
-
51
- // Mock computedOffsetXValueWithAutoFillData
52
- jest.mock("../utils/computed-with-auto-fill-data", () => ({
53
- computedOffsetXValueWithAutoFillData: jest.fn(({ value }) => value),
54
- }));
55
-
56
- describe("useOnProgressChange", () => {
57
- const mockOffsetX = useSharedValue(0);
58
- const mockSizeReady = useSharedValue(true);
59
- const mockOnProgressChange = jest.fn();
60
- const { __getReactionRegistrationCount, __resetReactionRegistrationCount, __triggerReaction } =
61
- jest.requireMock("react-native-reanimated");
62
-
63
- beforeEach(() => {
64
- jest.clearAllMocks();
65
- __resetReactionRegistrationCount();
66
- mockOffsetX.value = 0;
67
- mockSizeReady.value = true;
68
- });
69
-
70
- it("should handle progress change with function callback", () => {
71
- renderHook(() =>
72
- useOnProgressChange({
73
- size: 300,
74
- sizeReady: mockSizeReady,
75
- autoFillData: false,
76
- loop: false,
77
- offsetX: mockOffsetX,
78
- rawDataLength: 5,
79
- onProgressChange: mockOnProgressChange,
80
- })
81
- );
82
-
83
- mockOffsetX.value = -300; // Move to next slide
84
- __triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
85
- expect(mockOnProgressChange).toHaveBeenCalledWith(-300, 1);
86
- });
87
-
88
- it("should handle progress change with shared value", () => {
89
- const progressValue = useSharedValue(0);
90
- renderHook(() =>
91
- useOnProgressChange({
92
- size: 300,
93
- sizeReady: mockSizeReady,
94
- autoFillData: false,
95
- loop: false,
96
- offsetX: mockOffsetX,
97
- rawDataLength: 5,
98
- onProgressChange: progressValue,
99
- })
100
- );
101
-
102
- mockOffsetX.value = -300; // Move to next slide
103
- __triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
104
- expect(progressValue.value).toBe(1);
105
- });
106
-
107
- it("should handle loop mode", () => {
108
- renderHook(() =>
109
- useOnProgressChange({
110
- size: 300,
111
- sizeReady: mockSizeReady,
112
- autoFillData: false,
113
- loop: true,
114
- offsetX: mockOffsetX,
115
- rawDataLength: 5,
116
- onProgressChange: mockOnProgressChange,
117
- })
118
- );
119
-
120
- mockOffsetX.value = -1500; // Move to last slide
121
- __triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
122
- expect(mockOnProgressChange).toHaveBeenCalledWith(-1500, 5);
123
- });
124
-
125
- it("should handle autoFillData mode", () => {
126
- renderHook(() =>
127
- useOnProgressChange({
128
- size: 300,
129
- sizeReady: mockSizeReady,
130
- autoFillData: true,
131
- loop: false,
132
- offsetX: mockOffsetX,
133
- rawDataLength: 3,
134
- onProgressChange: mockOnProgressChange,
135
- })
136
- );
137
-
138
- mockOffsetX.value = -300; // Move to next slide
139
- __triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
140
- expect(mockOnProgressChange).toHaveBeenCalledWith(-300, 1);
141
- });
142
-
143
- it("should clamp values when not in loop mode", () => {
144
- renderHook(() =>
145
- useOnProgressChange({
146
- size: 300,
147
- sizeReady: mockSizeReady,
148
- autoFillData: false,
149
- loop: false,
150
- offsetX: mockOffsetX,
151
- rawDataLength: 3,
152
- onProgressChange: mockOnProgressChange,
153
- })
154
- );
155
-
156
- mockOffsetX.value = 300; // Try to move before first slide
157
- __triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
158
- expect(mockOnProgressChange).toHaveBeenCalledWith(0, 0);
159
-
160
- mockOffsetX.value = -900; // Try to move after last slide
161
- __triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
162
- expect(mockOnProgressChange).toHaveBeenCalledWith(-600, 2);
163
- });
164
-
165
- it("should handle positive offset values", () => {
166
- renderHook(() =>
167
- useOnProgressChange({
168
- size: 300,
169
- sizeReady: mockSizeReady,
170
- autoFillData: false,
171
- loop: true,
172
- offsetX: mockOffsetX,
173
- rawDataLength: 5,
174
- onProgressChange: mockOnProgressChange,
175
- })
176
- );
177
-
178
- mockOffsetX.value = 300; // Move backwards
179
- __triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
180
- expect(mockOnProgressChange).toHaveBeenCalledWith(300, 4);
181
- });
182
-
183
- it("should not call onProgressChange if not provided", () => {
184
- renderHook(() =>
185
- useOnProgressChange({
186
- size: 300,
187
- sizeReady: mockSizeReady,
188
- autoFillData: false,
189
- loop: false,
190
- offsetX: mockOffsetX,
191
- rawDataLength: 5,
192
- })
193
- );
194
-
195
- mockOffsetX.value = -300; // Move to next slide
196
- __triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
197
- // Should not throw any errors or call any callbacks
198
- });
199
-
200
- it("should not trigger progress change when size is not ready", () => {
201
- const mockSizeNotReady = useSharedValue(false);
202
- renderHook(() =>
203
- useOnProgressChange({
204
- size: 300,
205
- sizeReady: mockSizeNotReady,
206
- autoFillData: false,
207
- loop: false,
208
- offsetX: mockOffsetX,
209
- rawDataLength: 5,
210
- onProgressChange: mockOnProgressChange,
211
- })
212
- );
213
-
214
- mockOffsetX.value = -300; // Move to next slide
215
- __triggerReaction({ offset: mockOffsetX.value, ready: mockSizeNotReady.value });
216
- expect(mockOnProgressChange).not.toHaveBeenCalled();
217
- });
218
-
219
- it("should re-register the reaction when the offset shared value changes", () => {
220
- const replacementOffsetX = useSharedValue(-300);
221
- const { rerender } = renderHook(
222
- ({ offsetX }) =>
223
- useOnProgressChange({
224
- size: 300,
225
- sizeReady: mockSizeReady,
226
- autoFillData: false,
227
- loop: false,
228
- offsetX,
229
- rawDataLength: 5,
230
- onProgressChange: mockOnProgressChange,
231
- }),
232
- { initialProps: { offsetX: mockOffsetX } }
233
- );
234
-
235
- const initialRegistrationCount = __getReactionRegistrationCount();
236
- rerender({ offsetX: mockOffsetX });
237
- expect(__getReactionRegistrationCount()).toBe(initialRegistrationCount);
238
-
239
- rerender({ offsetX: replacementOffsetX });
240
-
241
- expect(__getReactionRegistrationCount()).toBe(initialRegistrationCount + 1);
242
- expect(mockOnProgressChange).toHaveBeenLastCalledWith(-300, 1);
243
- });
244
-
245
- it("should re-register the reaction when the size-ready shared value changes", () => {
246
- const initialSizeReady = useSharedValue(false);
247
- const replacementSizeReady = useSharedValue(true);
248
- const { rerender } = renderHook(
249
- ({ sizeReady }) =>
250
- useOnProgressChange({
251
- size: 300,
252
- sizeReady,
253
- autoFillData: false,
254
- loop: false,
255
- offsetX: mockOffsetX,
256
- rawDataLength: 5,
257
- onProgressChange: mockOnProgressChange,
258
- }),
259
- { initialProps: { sizeReady: initialSizeReady } }
260
- );
261
-
262
- const initialRegistrationCount = __getReactionRegistrationCount();
263
- expect(mockOnProgressChange).not.toHaveBeenCalled();
264
-
265
- rerender({ sizeReady: replacementSizeReady });
266
-
267
- expect(__getReactionRegistrationCount()).toBe(initialRegistrationCount + 1);
268
- expect(mockOnProgressChange).toHaveBeenCalledWith(0, 0);
269
- });
270
- });