react-native-reanimated-carousel 4.0.2 → 4.0.3

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 (80) hide show
  1. package/lib/commonjs/components/LazyView.test.js +2 -0
  2. package/lib/commonjs/components/LazyView.test.js.map +1 -0
  3. package/lib/commonjs/components/Pagination/Basic/PaginationItem.js +1 -1
  4. package/lib/commonjs/components/Pagination/Basic/PaginationItem.js.map +1 -1
  5. package/lib/commonjs/components/Pagination/Basic/index.js +1 -1
  6. package/lib/commonjs/components/Pagination/Basic/index.js.map +1 -1
  7. package/lib/commonjs/components/Pagination/Custom/PaginationItem.js +1 -1
  8. package/lib/commonjs/components/Pagination/Custom/PaginationItem.js.map +1 -1
  9. package/lib/commonjs/components/Pagination/Custom/index.js +1 -1
  10. package/lib/commonjs/components/Pagination/Custom/index.js.map +1 -1
  11. package/lib/commonjs/hooks/useCarouselController.test.js +1 -1
  12. package/lib/commonjs/hooks/useCarouselController.test.js.map +1 -1
  13. package/lib/commonjs/hooks/useCommonVariables.test.js +1 -1
  14. package/lib/commonjs/hooks/useCommonVariables.test.js.map +1 -1
  15. package/lib/commonjs/layouts/parallax.test.js +2 -0
  16. package/lib/commonjs/layouts/parallax.test.js.map +1 -0
  17. package/lib/commonjs/layouts/stack.test.js +1 -33
  18. package/lib/commonjs/layouts/stack.test.js.map +1 -1
  19. package/lib/commonjs/store/index.test.js +2 -0
  20. package/lib/commonjs/store/index.test.js.map +1 -0
  21. package/lib/commonjs/utils/compute-offset-if-data-changed.test.js +1 -1
  22. package/lib/commonjs/utils/compute-offset-if-data-changed.test.js.map +1 -1
  23. package/lib/commonjs/utils/computed-with-auto-fill-data.test.js +1 -1
  24. package/lib/commonjs/utils/computed-with-auto-fill-data.test.js.map +1 -1
  25. package/lib/commonjs/utils/deal-with-animation.test.js +1 -1
  26. package/lib/commonjs/utils/deal-with-animation.test.js.map +1 -1
  27. package/lib/commonjs/utils/handleroffset-direction.test.js +1 -1
  28. package/lib/commonjs/utils/handleroffset-direction.test.js.map +1 -1
  29. package/lib/commonjs/utils/log.test.js +1 -1
  30. package/lib/commonjs/utils/log.test.js.map +1 -1
  31. package/lib/module/components/LazyView.test.js +49 -0
  32. package/lib/module/components/LazyView.test.js.map +1 -0
  33. package/lib/module/components/Pagination/Basic/PaginationItem.js +11 -4
  34. package/lib/module/components/Pagination/Basic/PaginationItem.js.map +1 -1
  35. package/lib/module/components/Pagination/Basic/index.js +4 -2
  36. package/lib/module/components/Pagination/Basic/index.js.map +1 -1
  37. package/lib/module/components/Pagination/Custom/PaginationItem.js +11 -4
  38. package/lib/module/components/Pagination/Custom/PaginationItem.js.map +1 -1
  39. package/lib/module/components/Pagination/Custom/index.js +4 -2
  40. package/lib/module/components/Pagination/Custom/index.js.map +1 -1
  41. package/lib/module/hooks/useCarouselController.test.js +297 -0
  42. package/lib/module/hooks/useCarouselController.test.js.map +1 -1
  43. package/lib/module/hooks/useCommonVariables.test.js +161 -0
  44. package/lib/module/hooks/useCommonVariables.test.js.map +1 -1
  45. package/lib/module/layouts/parallax.test.js +210 -0
  46. package/lib/module/layouts/parallax.test.js.map +1 -0
  47. package/lib/module/layouts/stack.test.js +206 -35
  48. package/lib/module/layouts/stack.test.js.map +1 -1
  49. package/lib/module/store/index.test.js +293 -0
  50. package/lib/module/store/index.test.js.map +1 -0
  51. package/lib/module/utils/compute-offset-if-data-changed.test.js +94 -1
  52. package/lib/module/utils/compute-offset-if-data-changed.test.js.map +1 -1
  53. package/lib/module/utils/computed-with-auto-fill-data.test.js +72 -0
  54. package/lib/module/utils/computed-with-auto-fill-data.test.js.map +1 -1
  55. package/lib/module/utils/deal-with-animation.test.js +98 -0
  56. package/lib/module/utils/deal-with-animation.test.js.map +1 -1
  57. package/lib/module/utils/handleroffset-direction.test.js +56 -0
  58. package/lib/module/utils/handleroffset-direction.test.js.map +1 -1
  59. package/lib/module/utils/log.test.js +60 -0
  60. package/lib/module/utils/log.test.js.map +1 -1
  61. package/lib/typescript/components/Pagination/Basic/PaginationItem.d.ts +1 -0
  62. package/lib/typescript/components/Pagination/Basic/index.d.ts +1 -0
  63. package/lib/typescript/components/Pagination/Custom/PaginationItem.d.ts +1 -0
  64. package/lib/typescript/components/Pagination/Custom/index.d.ts +1 -0
  65. package/package.json +1 -1
  66. package/src/components/LazyView.test.tsx +61 -0
  67. package/src/components/Pagination/Basic/PaginationItem.tsx +22 -5
  68. package/src/components/Pagination/Basic/index.tsx +3 -0
  69. package/src/components/Pagination/Custom/PaginationItem.tsx +11 -3
  70. package/src/components/Pagination/Custom/index.tsx +3 -0
  71. package/src/hooks/useCarouselController.test.tsx +293 -0
  72. package/src/hooks/useCommonVariables.test.tsx +190 -0
  73. package/src/layouts/parallax.test.ts +239 -0
  74. package/src/layouts/stack.test.ts +235 -36
  75. package/src/store/index.test.tsx +306 -0
  76. package/src/utils/compute-offset-if-data-changed.test.ts +104 -1
  77. package/src/utils/computed-with-auto-fill-data.test.ts +86 -0
  78. package/src/utils/deal-with-animation.test.ts +90 -0
  79. package/src/utils/handleroffset-direction.test.ts +72 -0
  80. package/src/utils/log.test.ts +70 -0
@@ -1,6 +1,7 @@
1
1
  import { renderHook } from "@testing-library/react-hooks";
2
2
 
3
3
  import { useCommonVariables } from "./useCommonVariables";
4
+ import { TInitializeCarouselProps } from "./useInitProps";
4
5
 
5
6
  type UseCommonVariablesInput = Parameters<typeof useCommonVariables>[0];
6
7
 
@@ -17,6 +18,7 @@ const input = {
17
18
  autoPlay: false,
18
19
  autoPlayInterval: 2000,
19
20
  data: [0, 1, 2, 3],
21
+ renderItem: () => null,
20
22
  pagingEnabled: true,
21
23
  defaultIndex: 0,
22
24
  autoFillData: true,
@@ -36,4 +38,192 @@ describe("useCommonVariables", () => {
36
38
  expect(hook.result.current.validLength).toMatchInlineSnapshot("3");
37
39
  expect(hook.result.current.handlerOffset.value).toMatchInlineSnapshot("-0");
38
40
  });
41
+
42
+ it("should handle vertical orientation", () => {
43
+ const verticalInput = {
44
+ ...input,
45
+ vertical: true,
46
+ } as TInitializeCarouselProps<any>;
47
+
48
+ const hook = renderHook(() => useCommonVariables(verticalInput));
49
+
50
+ expect(hook.result.current.size).toBe(350); // Should use height instead of width
51
+ expect(hook.result.current.validLength).toBe(3);
52
+ });
53
+
54
+ it("should calculate defaultHandlerOffsetValue correctly with non-zero defaultIndex", () => {
55
+ const inputWithDefaultIndex = {
56
+ ...input,
57
+ defaultIndex: 2,
58
+ };
59
+
60
+ const hook = renderHook(() => useCommonVariables(inputWithDefaultIndex));
61
+
62
+ expect(hook.result.current.handlerOffset.value).toBe(-1400); // -2 * 700
63
+ });
64
+
65
+ it("should use custom defaultScrollOffsetValue when provided", () => {
66
+ const customOffset = {
67
+ value: -500,
68
+ addListener: jest.fn(),
69
+ removeListener: jest.fn(),
70
+ modify: jest.fn(),
71
+ };
72
+ const inputWithCustomOffset = {
73
+ ...input,
74
+ defaultScrollOffsetValue: customOffset,
75
+ };
76
+
77
+ const hook = renderHook(() => useCommonVariables(inputWithCustomOffset));
78
+
79
+ expect(hook.result.current.handlerOffset).toBe(customOffset);
80
+ expect(hook.result.current.handlerOffset.value).toBe(-500);
81
+ });
82
+
83
+ it("should handle single data item", () => {
84
+ const singleItemInput = {
85
+ ...input,
86
+ dataLength: 1,
87
+ data: [0],
88
+ };
89
+
90
+ const hook = renderHook(() => useCommonVariables(singleItemInput));
91
+
92
+ expect(hook.result.current.validLength).toBe(0); // dataLength - 1
93
+ });
94
+
95
+ it("should handle zero dataLength", () => {
96
+ const emptyInput = {
97
+ ...input,
98
+ dataLength: 0,
99
+ data: [],
100
+ };
101
+
102
+ const hook = renderHook(() => useCommonVariables(emptyInput));
103
+
104
+ expect(hook.result.current.validLength).toBe(-1); // dataLength - 1
105
+ });
106
+
107
+ it("should handle negative defaultIndex", () => {
108
+ const negativeIndexInput = {
109
+ ...input,
110
+ defaultIndex: -1,
111
+ };
112
+
113
+ const hook = renderHook(() => useCommonVariables(negativeIndexInput));
114
+
115
+ expect(hook.result.current.handlerOffset.value).toBe(-700); // -Math.abs(-1 * 700)
116
+ });
117
+
118
+ it("should handle loop disabled", () => {
119
+ const noLoopInput = {
120
+ ...input,
121
+ loop: false,
122
+ };
123
+
124
+ const hook = renderHook(() => useCommonVariables(noLoopInput));
125
+
126
+ expect(hook.result.current.size).toBe(700);
127
+ expect(hook.result.current.validLength).toBe(3);
128
+ });
129
+
130
+ it("should update when props change", () => {
131
+ const hook = renderHook(({ props }) => useCommonVariables(props), {
132
+ initialProps: { props: input },
133
+ });
134
+
135
+ expect(hook.result.current.size).toBe(700);
136
+
137
+ // Update width
138
+ const updatedInput = {
139
+ ...input,
140
+ width: 800,
141
+ };
142
+
143
+ hook.rerender({ props: updatedInput });
144
+ expect(hook.result.current.size).toBe(800);
145
+ });
146
+
147
+ it("should handle dataLength changes", () => {
148
+ const hook = renderHook(({ props }) => useCommonVariables(props), {
149
+ initialProps: { props: input },
150
+ });
151
+
152
+ expect(hook.result.current.validLength).toBe(3);
153
+
154
+ // Update dataLength
155
+ const updatedInput = {
156
+ ...input,
157
+ dataLength: 6,
158
+ };
159
+
160
+ hook.rerender({ props: updatedInput });
161
+ expect(hook.result.current.validLength).toBe(5);
162
+ });
163
+
164
+ it("should handle zero size (edge case)", () => {
165
+ const zeroSizeInput = {
166
+ ...input,
167
+ width: 0,
168
+ };
169
+
170
+ const hook = renderHook(() => useCommonVariables(zeroSizeInput));
171
+
172
+ expect(hook.result.current.size).toBe(0);
173
+ expect(hook.result.current.handlerOffset.value).toBe(-0); // -Math.abs(0 * 0)
174
+ });
175
+
176
+ it("should handle large defaultIndex", () => {
177
+ const largeIndexInput = {
178
+ ...input,
179
+ defaultIndex: 10,
180
+ };
181
+
182
+ const hook = renderHook(() => useCommonVariables(largeIndexInput));
183
+
184
+ expect(hook.result.current.handlerOffset.value).toBe(-7000); // -Math.abs(10 * 700)
185
+ });
186
+
187
+ it("should handle vertical with zero height", () => {
188
+ const verticalZeroHeightInput = {
189
+ ...input,
190
+ vertical: true,
191
+ height: 0,
192
+ } as TInitializeCarouselProps<any>;
193
+
194
+ const hook = renderHook(() => useCommonVariables(verticalZeroHeightInput));
195
+
196
+ expect(hook.result.current.size).toBe(0);
197
+ });
198
+
199
+ it("should handle floating point dimensions", () => {
200
+ const floatInput = {
201
+ ...input,
202
+ width: 700.5,
203
+ height: 350.25,
204
+ };
205
+
206
+ const hook = renderHook(() => useCommonVariables(floatInput));
207
+
208
+ expect(hook.result.current.size).toBe(700.5);
209
+ });
210
+
211
+ it("should calculate validLength correctly for different data lengths", () => {
212
+ const testCases = [
213
+ { dataLength: 0, expected: -1 },
214
+ { dataLength: 1, expected: 0 },
215
+ { dataLength: 5, expected: 4 },
216
+ { dataLength: 100, expected: 99 },
217
+ ];
218
+
219
+ for (const { dataLength, expected } of testCases) {
220
+ const testInput = {
221
+ ...input,
222
+ dataLength,
223
+ };
224
+
225
+ const hook = renderHook(() => useCommonVariables(testInput));
226
+ expect(hook.result.current.validLength).toBe(expected);
227
+ }
228
+ });
39
229
  });
@@ -0,0 +1,239 @@
1
+ import { parallaxLayout } from "./parallax";
2
+
3
+ // Mock react-native-reanimated
4
+ jest.mock("react-native-reanimated", () => ({
5
+ interpolate: jest.fn((value, inputRange, outputRange) => {
6
+ // Simple linear interpolation mock for testing
7
+ const inputMin = inputRange[0];
8
+ const inputMax = inputRange[inputRange.length - 1];
9
+ const outputMin = outputRange[0];
10
+ const outputMax = outputRange[outputRange.length - 1];
11
+
12
+ if (value <= inputMin) return outputMin;
13
+ if (value >= inputMax) return outputMax;
14
+
15
+ // Linear interpolation
16
+ const progress = (value - inputMin) / (inputMax - inputMin);
17
+ return outputMin + progress * (outputMax - outputMin);
18
+ }),
19
+ Extrapolation: {
20
+ CLAMP: "clamp",
21
+ },
22
+ }));
23
+
24
+ describe("parallaxLayout", () => {
25
+ const baseConfig = {
26
+ size: 300,
27
+ vertical: false,
28
+ };
29
+
30
+ it("should create layout function with default config", () => {
31
+ const layout = parallaxLayout(baseConfig);
32
+ expect(typeof layout).toBe("function");
33
+ });
34
+
35
+ it("should return transform object for horizontal layout", () => {
36
+ const layout = parallaxLayout(baseConfig);
37
+ const result = layout(0);
38
+
39
+ expect(result).toHaveProperty("transform");
40
+ expect(result).toHaveProperty("zIndex");
41
+ expect(result.transform).toHaveLength(2);
42
+ expect(result.transform[0]).toHaveProperty("translateX");
43
+ expect(result.transform[1]).toHaveProperty("scale");
44
+ });
45
+
46
+ it("should return transform object for vertical layout", () => {
47
+ const verticalConfig = { ...baseConfig, vertical: true };
48
+ const layout = parallaxLayout(verticalConfig);
49
+ const result = layout(0);
50
+
51
+ expect(result).toHaveProperty("transform");
52
+ expect(result).toHaveProperty("zIndex");
53
+ expect(result.transform).toHaveLength(2);
54
+ expect(result.transform[0]).toHaveProperty("translateY");
55
+ expect(result.transform[1]).toHaveProperty("scale");
56
+ });
57
+
58
+ it("should handle center position (value = 0)", () => {
59
+ const layout = parallaxLayout(baseConfig);
60
+ const result = layout(0);
61
+
62
+ // At center position, translateX should be 0
63
+ expect(result.transform[0].translateX).toBe(0);
64
+ // Scale should be a number (parallax logic applied)
65
+ expect(typeof result.transform[1].scale).toBe("number");
66
+ });
67
+
68
+ it("should handle left position (value = -1)", () => {
69
+ const layout = parallaxLayout(baseConfig);
70
+ const result = layout(-1);
71
+
72
+ // At left position, translateX should be -size + parallaxScrollingOffset
73
+ expect(result.transform[0].translateX).toBe(-200); // -300 + 100
74
+ // Scale should be a number
75
+ expect(typeof result.transform[1].scale).toBe("number");
76
+ });
77
+
78
+ it("should handle right position (value = 1)", () => {
79
+ const layout = parallaxLayout(baseConfig);
80
+ const result = layout(1);
81
+
82
+ // At right position, translateX should be size - parallaxScrollingOffset
83
+ expect(result.transform[0].translateX).toBe(200); // 300 - 100
84
+ // Scale should be a number
85
+ expect(typeof result.transform[1].scale).toBe("number");
86
+ });
87
+
88
+ it("should handle custom parallaxScrollingOffset", () => {
89
+ const modeConfig = { parallaxScrollingOffset: 50 };
90
+ const layout = parallaxLayout(baseConfig, modeConfig);
91
+
92
+ const leftResult = layout(-1);
93
+ const rightResult = layout(1);
94
+
95
+ expect(leftResult.transform[0].translateX).toBe(-250); // -300 + 50
96
+ expect(rightResult.transform[0].translateX).toBe(250); // 300 - 50
97
+ });
98
+
99
+ it("should handle custom parallaxScrollingScale", () => {
100
+ const modeConfig = { parallaxScrollingScale: 0.9 };
101
+ const layout = parallaxLayout(baseConfig, modeConfig);
102
+
103
+ const centerResult = layout(0);
104
+ const sideResult = layout(-1);
105
+
106
+ // Verify the structure and that scale values are calculated
107
+ expect(typeof centerResult.transform[1].scale).toBe("number");
108
+ expect(typeof sideResult.transform[1].scale).toBe("number");
109
+ expect(centerResult.transform[1].scale).toBeGreaterThan(0);
110
+ expect(sideResult.transform[1].scale).toBeGreaterThan(0);
111
+ });
112
+
113
+ it("should handle custom parallaxAdjacentItemScale", () => {
114
+ const modeConfig = {
115
+ parallaxScrollingScale: 0.9,
116
+ parallaxAdjacentItemScale: 0.7,
117
+ };
118
+ const layout = parallaxLayout(baseConfig, modeConfig);
119
+
120
+ const centerResult = layout(0);
121
+ const sideResult = layout(-1);
122
+
123
+ // Verify the structure and that scale values are calculated
124
+ expect(typeof centerResult.transform[1].scale).toBe("number");
125
+ expect(typeof sideResult.transform[1].scale).toBe("number");
126
+ expect(centerResult.transform[1].scale).toBeGreaterThan(0);
127
+ expect(sideResult.transform[1].scale).toBeGreaterThan(0);
128
+ });
129
+
130
+ it("should handle vertical layout with custom config", () => {
131
+ const verticalConfig = { ...baseConfig, vertical: true };
132
+ const modeConfig = { parallaxScrollingOffset: 80 };
133
+ const layout = parallaxLayout(verticalConfig, modeConfig);
134
+
135
+ const leftResult = layout(-1);
136
+ const centerResult = layout(0);
137
+ const rightResult = layout(1);
138
+
139
+ expect(leftResult.transform[0].translateY).toBe(-220); // -300 + 80
140
+ expect(centerResult.transform[0].translateY).toBe(0);
141
+ expect(rightResult.transform[0].translateY).toBe(220); // 300 - 80
142
+ });
143
+
144
+ it("should handle edge case values", () => {
145
+ const layout = parallaxLayout(baseConfig);
146
+
147
+ // Test with values beyond the normal range
148
+ const farLeftResult = layout(-2);
149
+ const farRightResult = layout(2);
150
+
151
+ expect(typeof farLeftResult.transform[0].translateX).toBe("number");
152
+ expect(typeof farLeftResult.transform[1].scale).toBe("number");
153
+ expect(typeof farRightResult.transform[0].translateX).toBe("number");
154
+ expect(typeof farRightResult.transform[1].scale).toBe("number");
155
+ });
156
+
157
+ it("should handle fractional values", () => {
158
+ const layout = parallaxLayout(baseConfig);
159
+
160
+ const halfwayResult = layout(0.5);
161
+ const quarterResult = layout(-0.25);
162
+
163
+ expect(typeof halfwayResult.transform[0].translateX).toBe("number");
164
+ expect(typeof halfwayResult.transform[1].scale).toBe("number");
165
+ expect(typeof quarterResult.transform[0].translateX).toBe("number");
166
+ expect(typeof quarterResult.transform[1].scale).toBe("number");
167
+ });
168
+
169
+ it("should handle zero size", () => {
170
+ const zeroSizeConfig = { ...baseConfig, size: 0 };
171
+ const layout = parallaxLayout(zeroSizeConfig);
172
+ const result = layout(0);
173
+
174
+ expect(result).toHaveProperty("transform");
175
+ expect(result).toHaveProperty("zIndex");
176
+ });
177
+
178
+ it("should handle negative size", () => {
179
+ const negativeSizeConfig = { ...baseConfig, size: -100 };
180
+ const layout = parallaxLayout(negativeSizeConfig);
181
+ const result = layout(0);
182
+
183
+ expect(result).toHaveProperty("transform");
184
+ expect(result).toHaveProperty("zIndex");
185
+ });
186
+
187
+ it("should handle all config properties at once", () => {
188
+ const fullModeConfig = {
189
+ parallaxScrollingOffset: 75,
190
+ parallaxScrollingScale: 0.85,
191
+ parallaxAdjacentItemScale: 0.6,
192
+ };
193
+ const layout = parallaxLayout(baseConfig, fullModeConfig);
194
+
195
+ const centerResult = layout(0);
196
+ const sideResult = layout(-1);
197
+
198
+ // Verify the structure and that values are calculated correctly
199
+ expect(typeof centerResult.transform[1].scale).toBe("number");
200
+ expect(typeof sideResult.transform[1].scale).toBe("number");
201
+ expect(sideResult.transform[0].translateX).toBe(-225); // -300 + 75
202
+ });
203
+
204
+ it("should handle empty mode config", () => {
205
+ const layout = parallaxLayout(baseConfig, {});
206
+ const result = layout(0);
207
+
208
+ // At center (value = 0), scale should be a number (default parallax behavior)
209
+ expect(typeof result.transform[1].scale).toBe("number");
210
+ expect(result.transform[1].scale).toBeGreaterThan(0);
211
+ expect(result.transform[1].scale).toBeLessThanOrEqual(1);
212
+ });
213
+
214
+ it("should handle undefined mode config", () => {
215
+ const layout = parallaxLayout(baseConfig, undefined);
216
+ const result = layout(0);
217
+
218
+ // At center (value = 0), scale should be a number (default parallax behavior)
219
+ expect(typeof result.transform[1].scale).toBe("number");
220
+ expect(result.transform[1].scale).toBeGreaterThan(0);
221
+ expect(result.transform[1].scale).toBeLessThanOrEqual(1);
222
+ });
223
+
224
+ it("should maintain consistent zIndex calculation", () => {
225
+ const layout = parallaxLayout(baseConfig);
226
+
227
+ const leftResult = layout(-1);
228
+ const centerResult = layout(0);
229
+ const rightResult = layout(1);
230
+
231
+ expect(typeof leftResult.zIndex).toBe("number");
232
+ expect(typeof centerResult.zIndex).toBe("number");
233
+ expect(typeof rightResult.zIndex).toBe("number");
234
+
235
+ // Center should have highest zIndex
236
+ expect(centerResult.zIndex).toBeGreaterThanOrEqual(leftResult.zIndex);
237
+ expect(centerResult.zIndex).toBeGreaterThanOrEqual(rightResult.zIndex);
238
+ });
239
+ });