vitest-native 0.2.1 → 0.4.0
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.
- package/CHANGELOG.md +86 -0
- package/README.md +268 -9
- package/dist/chunk-CZWwpsFl.cjs +43 -0
- package/dist/helpers.cjs +10 -0
- package/dist/helpers.d.cts +7 -1
- package/dist/helpers.d.mts +7 -1
- package/dist/helpers.mjs +10 -1
- package/dist/{index-CJqS-y_M.d.cts → index-CJIhukN_.d.mts} +58 -4
- package/dist/{index-h4vpIkQO.d.mts → index-DEB7OFaJ.d.cts} +60 -2
- package/dist/index.cjs +296 -35
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +292 -33
- package/dist/jest-compat/interop.mjs +30 -0
- package/dist/jest-compat/jest-globals.d.mts +3 -0
- package/dist/jest-compat/jest-globals.mjs +9 -0
- package/dist/jest-compat/noop.d.mts +3 -0
- package/dist/jest-compat/noop.mjs +7 -0
- package/dist/jest-compat/setup.d.mts +3 -0
- package/dist/jest-compat/setup.mjs +41 -0
- package/dist/jest-compat.cjs +109 -0
- package/dist/jest-compat.d.cts +38 -0
- package/dist/jest-compat.d.mts +38 -0
- package/dist/jest-compat.mjs +104 -0
- package/dist/matchers.cjs +69 -0
- package/dist/matchers.d.cts +51 -0
- package/dist/matchers.d.mts +51 -0
- package/dist/matchers.mjs +66 -0
- package/dist/native/boundary.mjs +225 -0
- package/dist/native/globals.mjs +112 -0
- package/dist/native/hooks.mjs +85 -0
- package/dist/native/loader.mjs +123 -0
- package/dist/native/match.mjs +9 -0
- package/dist/native/reset.mjs +170 -0
- package/dist/native/resolve.mjs +22 -0
- package/dist/native/runner.mjs +18 -0
- package/dist/native/setup.mjs +134 -0
- package/dist/native/transform.mjs +79 -0
- package/dist/native/worker.mjs +96 -0
- package/dist/pool-B4mWN3N5.cjs +60 -0
- package/dist/pool-oPTZiSXU.mjs +58 -0
- package/dist/{preset-map-Bt4dXkZO.cjs → preset-map-C3jxnkiX.cjs} +6 -1
- package/dist/{preset-map-D224CC5w.mjs → preset-map-OJm5IWuL.mjs} +6 -1
- package/dist/{presets-9Y4ssfiH.cjs → presets-2I68Ipyl.cjs} +768 -122
- package/dist/{presets-CWH0gtM0.mjs → presets-DOGXQzYT.mjs} +732 -87
- package/dist/presets.cjs +5 -1
- package/dist/presets.d.cts +2 -2
- package/dist/presets.d.mts +2 -2
- package/dist/presets.mjs +2 -2
- package/dist/setup.cjs +903 -121
- package/dist/setup.mjs +899 -118
- package/docs/migrating-from-jest.md +123 -0
- package/docs/migrating-from-vitest-react-native.md +109 -0
- package/package.json +67 -8
package/dist/setup.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { n as __require } from "./chunk-CjcI7cDX.mjs";
|
|
2
|
-
import { t as AUTO_DETECT_PRESETS } from "./preset-map-
|
|
3
|
-
import { t as presets_exports } from "./presets-
|
|
2
|
+
import { t as AUTO_DETECT_PRESETS } from "./preset-map-OJm5IWuL.mjs";
|
|
3
|
+
import { f as createPressableMock, p as buildPressableHostProps, t as presets_exports } from "./presets-DOGXQzYT.mjs";
|
|
4
4
|
import { serializer } from "./serializer.mjs";
|
|
5
|
+
import { animatedMatchers } from "./matchers.mjs";
|
|
5
6
|
import { createRequire } from "node:module";
|
|
6
7
|
import path from "node:path";
|
|
7
8
|
import { afterAll, expect, vi } from "vitest";
|
|
@@ -21,8 +22,15 @@ function createViewMock() {
|
|
|
21
22
|
//#region src/mocks/components/Text.ts
|
|
22
23
|
function createTextMock() {
|
|
23
24
|
const Text = React.forwardRef((props, ref) => {
|
|
25
|
+
const { onPress, onLongPress, disabled, accessibilityRole, ...rest } = props;
|
|
26
|
+
const resolvedRole = accessibilityRole ?? (!disabled && (onPress || onLongPress) ? "link" : void 0);
|
|
24
27
|
return React.createElement("Text", {
|
|
25
|
-
|
|
28
|
+
accessible: true,
|
|
29
|
+
...rest,
|
|
30
|
+
...onPress ? { onPress } : {},
|
|
31
|
+
...onLongPress ? { onLongPress } : {},
|
|
32
|
+
...disabled ? { disabled } : {},
|
|
33
|
+
...resolvedRole ? { accessibilityRole: resolvedRole } : {},
|
|
26
34
|
ref
|
|
27
35
|
});
|
|
28
36
|
});
|
|
@@ -81,6 +89,7 @@ function createTextInputMock() {
|
|
|
81
89
|
//#region src/mocks/components/ScrollView.ts
|
|
82
90
|
function createScrollViewMock() {
|
|
83
91
|
const ScrollView = React.forwardRef((props, ref) => {
|
|
92
|
+
const { children, refreshControl, ...rest } = props;
|
|
84
93
|
const instanceMethods = {
|
|
85
94
|
scrollTo: vi.fn(),
|
|
86
95
|
scrollToEnd: vi.fn(),
|
|
@@ -90,10 +99,10 @@ function createScrollViewMock() {
|
|
|
90
99
|
setNativeProps: vi.fn()
|
|
91
100
|
};
|
|
92
101
|
React.useImperativeHandle(ref, () => instanceMethods);
|
|
93
|
-
return React.createElement("
|
|
94
|
-
...
|
|
102
|
+
return React.createElement("RCTScrollView", {
|
|
103
|
+
...rest,
|
|
95
104
|
ref
|
|
96
|
-
});
|
|
105
|
+
}, refreshControl, React.createElement("View", null, children));
|
|
97
106
|
});
|
|
98
107
|
ScrollView.displayName = "ScrollView";
|
|
99
108
|
return ScrollView;
|
|
@@ -207,50 +216,23 @@ function createModalMock() {
|
|
|
207
216
|
return Modal;
|
|
208
217
|
}
|
|
209
218
|
//#endregion
|
|
210
|
-
//#region src/mocks/components/Pressable.ts
|
|
211
|
-
function createPressableMock() {
|
|
212
|
-
const Pressable = React.forwardRef((props, ref) => {
|
|
213
|
-
return React.createElement("Pressable", {
|
|
214
|
-
...props,
|
|
215
|
-
ref
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
Pressable.displayName = "Pressable";
|
|
219
|
-
return Pressable;
|
|
220
|
-
}
|
|
221
|
-
//#endregion
|
|
222
219
|
//#region src/mocks/components/TouchableOpacity.ts
|
|
223
220
|
function createTouchableOpacityMock() {
|
|
224
|
-
const TouchableOpacity = React.forwardRef((props, ref) =>
|
|
225
|
-
return React.createElement("TouchableOpacity", {
|
|
226
|
-
...props,
|
|
227
|
-
ref
|
|
228
|
-
});
|
|
229
|
-
});
|
|
221
|
+
const TouchableOpacity = React.forwardRef((props, ref) => React.createElement("TouchableOpacity", buildPressableHostProps(props, ref)));
|
|
230
222
|
TouchableOpacity.displayName = "TouchableOpacity";
|
|
231
223
|
return TouchableOpacity;
|
|
232
224
|
}
|
|
233
225
|
//#endregion
|
|
234
226
|
//#region src/mocks/components/TouchableHighlight.ts
|
|
235
227
|
function createTouchableHighlightMock() {
|
|
236
|
-
const TouchableHighlight = React.forwardRef((props, ref) =>
|
|
237
|
-
return React.createElement("TouchableHighlight", {
|
|
238
|
-
...props,
|
|
239
|
-
ref
|
|
240
|
-
});
|
|
241
|
-
});
|
|
228
|
+
const TouchableHighlight = React.forwardRef((props, ref) => React.createElement("TouchableHighlight", buildPressableHostProps(props, ref)));
|
|
242
229
|
TouchableHighlight.displayName = "TouchableHighlight";
|
|
243
230
|
return TouchableHighlight;
|
|
244
231
|
}
|
|
245
232
|
//#endregion
|
|
246
233
|
//#region src/mocks/components/TouchableWithoutFeedback.ts
|
|
247
234
|
function createTouchableWithoutFeedbackMock() {
|
|
248
|
-
const TouchableWithoutFeedback = React.forwardRef((props, ref) =>
|
|
249
|
-
return React.createElement("TouchableWithoutFeedback", {
|
|
250
|
-
...props,
|
|
251
|
-
ref
|
|
252
|
-
});
|
|
253
|
-
});
|
|
235
|
+
const TouchableWithoutFeedback = React.forwardRef((props, ref) => React.createElement("TouchableWithoutFeedback", buildPressableHostProps(props, ref)));
|
|
254
236
|
TouchableWithoutFeedback.displayName = "TouchableWithoutFeedback";
|
|
255
237
|
return TouchableWithoutFeedback;
|
|
256
238
|
}
|
|
@@ -258,8 +240,21 @@ function createTouchableWithoutFeedbackMock() {
|
|
|
258
240
|
//#region src/mocks/components/TouchableNativeFeedback.ts
|
|
259
241
|
function createTouchableNativeFeedbackMock() {
|
|
260
242
|
const TouchableNativeFeedback = React.forwardRef((props, ref) => {
|
|
243
|
+
const { disabled, accessibilityState, ...rest } = props;
|
|
244
|
+
const mergedA11yState = disabled || accessibilityState ? {
|
|
245
|
+
...accessibilityState,
|
|
246
|
+
...disabled ? { disabled: true } : {}
|
|
247
|
+
} : void 0;
|
|
248
|
+
if (disabled) {
|
|
249
|
+
delete rest.onPress;
|
|
250
|
+
delete rest.onPressIn;
|
|
251
|
+
delete rest.onPressOut;
|
|
252
|
+
delete rest.onLongPress;
|
|
253
|
+
}
|
|
261
254
|
return React.createElement("TouchableNativeFeedback", {
|
|
262
|
-
|
|
255
|
+
accessible: true,
|
|
256
|
+
...rest,
|
|
257
|
+
...mergedA11yState ? { accessibilityState: mergedA11yState } : {},
|
|
263
258
|
ref
|
|
264
259
|
});
|
|
265
260
|
});
|
|
@@ -282,16 +277,18 @@ function createActivityIndicatorMock() {
|
|
|
282
277
|
//#region src/mocks/components/Button.ts
|
|
283
278
|
function createButtonMock() {
|
|
284
279
|
function Button(props) {
|
|
285
|
-
const { title, onPress, disabled, color, accessibilityLabel,
|
|
286
|
-
|
|
280
|
+
const { title, onPress, disabled, color, accessibilityLabel, accessibilityState, ...rest } = props;
|
|
281
|
+
const hostProps = buildPressableHostProps({
|
|
287
282
|
...rest,
|
|
288
|
-
|
|
289
|
-
}, React.createElement("Text", {
|
|
290
|
-
onPress: disabled ? void 0 : onPress,
|
|
291
|
-
accessibilityLabel: accessibilityLabel || title,
|
|
292
|
-
accessibilityRole: "button",
|
|
283
|
+
onPress,
|
|
293
284
|
disabled,
|
|
294
|
-
|
|
285
|
+
accessibilityState,
|
|
286
|
+
accessibilityLabel: accessibilityLabel || title,
|
|
287
|
+
accessibilityRole: "button"
|
|
288
|
+
}, void 0);
|
|
289
|
+
return React.createElement("View", hostProps, React.createElement("Text", {
|
|
290
|
+
style: color ? { color } : void 0,
|
|
291
|
+
disabled
|
|
295
292
|
}, title));
|
|
296
293
|
}
|
|
297
294
|
Button.displayName = "Button";
|
|
@@ -301,7 +298,8 @@ function createButtonMock() {
|
|
|
301
298
|
//#region src/mocks/components/Switch.ts
|
|
302
299
|
function createSwitchMock() {
|
|
303
300
|
const Switch = React.forwardRef((props, ref) => {
|
|
304
|
-
return React.createElement("
|
|
301
|
+
return React.createElement("RCTSwitch", {
|
|
302
|
+
accessibilityRole: "switch",
|
|
305
303
|
...props,
|
|
306
304
|
ref
|
|
307
305
|
});
|
|
@@ -457,7 +455,7 @@ function createPlatformMock(os = "ios") {
|
|
|
457
455
|
isVision: false,
|
|
458
456
|
isTesting: true,
|
|
459
457
|
select: vi.fn((specifics) => {
|
|
460
|
-
return specifics[os] ?? specifics.default;
|
|
458
|
+
return specifics[os] ?? specifics.native ?? specifics.default;
|
|
461
459
|
}),
|
|
462
460
|
constants: {
|
|
463
461
|
reactNativeVersion: {
|
|
@@ -515,12 +513,26 @@ function createDimensionsMock() {
|
|
|
515
513
|
}
|
|
516
514
|
//#endregion
|
|
517
515
|
//#region src/mocks/apis/StyleSheet.ts
|
|
518
|
-
function
|
|
519
|
-
return
|
|
516
|
+
function flattenImpl(style) {
|
|
517
|
+
if (style == null || style === false) return void 0;
|
|
518
|
+
if (!Array.isArray(style)) return style;
|
|
519
|
+
const result = {};
|
|
520
|
+
let hasAny = false;
|
|
521
|
+
for (const item of style) {
|
|
522
|
+
const flattened = flattenImpl(item);
|
|
523
|
+
if (flattened != null) {
|
|
524
|
+
hasAny = true;
|
|
525
|
+
Object.assign(result, flattened);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return hasAny ? result : void 0;
|
|
529
|
+
}
|
|
530
|
+
function createStyleSheetMock(getScale = () => 3) {
|
|
531
|
+
const sheet = {
|
|
520
532
|
create: vi.fn((styles) => {
|
|
521
533
|
return styles;
|
|
522
534
|
}),
|
|
523
|
-
flatten: vi.fn((
|
|
535
|
+
flatten: vi.fn((style) => flattenImpl(style)),
|
|
524
536
|
compose: vi.fn((a, b) => [a, b]),
|
|
525
537
|
absoluteFill: {
|
|
526
538
|
position: "absolute",
|
|
@@ -536,16 +548,234 @@ function createStyleSheetMock() {
|
|
|
536
548
|
top: 0,
|
|
537
549
|
bottom: 0
|
|
538
550
|
},
|
|
539
|
-
hairlineWidth: .5,
|
|
540
551
|
setStyleAttributePreprocessor: vi.fn()
|
|
541
552
|
};
|
|
553
|
+
Object.defineProperty(sheet, "hairlineWidth", {
|
|
554
|
+
enumerable: true,
|
|
555
|
+
configurable: true,
|
|
556
|
+
get() {
|
|
557
|
+
const ratio = getScale();
|
|
558
|
+
const width = Math.round(.4 * ratio) / ratio;
|
|
559
|
+
return width === 0 ? 1 / ratio : width;
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
return sheet;
|
|
542
563
|
}
|
|
543
564
|
//#endregion
|
|
544
565
|
//#region src/mocks/apis/Animated.ts
|
|
566
|
+
const IDENTITY_EASING = (t) => t;
|
|
567
|
+
function interpolateSegment(input, inputMin, inputMax, outputMin, outputMax, easing, extrapolateLeft, extrapolateRight) {
|
|
568
|
+
let result = input;
|
|
569
|
+
if (result < inputMin) {
|
|
570
|
+
if (extrapolateLeft === "identity") return result;
|
|
571
|
+
else if (extrapolateLeft === "clamp") result = inputMin;
|
|
572
|
+
}
|
|
573
|
+
if (result > inputMax) {
|
|
574
|
+
if (extrapolateRight === "identity") return result;
|
|
575
|
+
else if (extrapolateRight === "clamp") result = inputMax;
|
|
576
|
+
}
|
|
577
|
+
if (outputMin === outputMax) return outputMin;
|
|
578
|
+
if (inputMin === inputMax) return input <= inputMin ? outputMin : outputMax;
|
|
579
|
+
if (inputMin === -Infinity) result = -result;
|
|
580
|
+
else if (inputMax === Infinity) result = result - inputMin;
|
|
581
|
+
else result = (result - inputMin) / (inputMax - inputMin);
|
|
582
|
+
result = easing(result);
|
|
583
|
+
if (outputMin === -Infinity) result = -result;
|
|
584
|
+
else if (outputMax === Infinity) result = result + outputMin;
|
|
585
|
+
else result = result * (outputMax - outputMin) + outputMin;
|
|
586
|
+
return result;
|
|
587
|
+
}
|
|
588
|
+
function findRangeIndex(input, inputRange) {
|
|
589
|
+
let i;
|
|
590
|
+
for (i = 1; i < inputRange.length - 1; ++i) if (inputRange[i] >= input) break;
|
|
591
|
+
return i - 1;
|
|
592
|
+
}
|
|
593
|
+
function interpolateNumeric(value, inputRange, outputRange, extrapolate, extrapolateLeft, extrapolateRight, easing) {
|
|
594
|
+
const i = findRangeIndex(value, inputRange);
|
|
595
|
+
return interpolateSegment(value, inputRange[i], inputRange[i + 1], outputRange[i], outputRange[i + 1], easing, extrapolateLeft ?? extrapolate, extrapolateRight ?? extrapolate);
|
|
596
|
+
}
|
|
597
|
+
const NAMED_COLORS = {
|
|
598
|
+
red: [
|
|
599
|
+
255,
|
|
600
|
+
0,
|
|
601
|
+
0,
|
|
602
|
+
1
|
|
603
|
+
],
|
|
604
|
+
green: [
|
|
605
|
+
0,
|
|
606
|
+
128,
|
|
607
|
+
0,
|
|
608
|
+
1
|
|
609
|
+
],
|
|
610
|
+
blue: [
|
|
611
|
+
0,
|
|
612
|
+
0,
|
|
613
|
+
255,
|
|
614
|
+
1
|
|
615
|
+
],
|
|
616
|
+
white: [
|
|
617
|
+
255,
|
|
618
|
+
255,
|
|
619
|
+
255,
|
|
620
|
+
1
|
|
621
|
+
],
|
|
622
|
+
black: [
|
|
623
|
+
0,
|
|
624
|
+
0,
|
|
625
|
+
0,
|
|
626
|
+
1
|
|
627
|
+
],
|
|
628
|
+
transparent: [
|
|
629
|
+
0,
|
|
630
|
+
0,
|
|
631
|
+
0,
|
|
632
|
+
0
|
|
633
|
+
],
|
|
634
|
+
yellow: [
|
|
635
|
+
255,
|
|
636
|
+
255,
|
|
637
|
+
0,
|
|
638
|
+
1
|
|
639
|
+
],
|
|
640
|
+
cyan: [
|
|
641
|
+
0,
|
|
642
|
+
255,
|
|
643
|
+
255,
|
|
644
|
+
1
|
|
645
|
+
],
|
|
646
|
+
magenta: [
|
|
647
|
+
255,
|
|
648
|
+
0,
|
|
649
|
+
255,
|
|
650
|
+
1
|
|
651
|
+
],
|
|
652
|
+
orange: [
|
|
653
|
+
255,
|
|
654
|
+
165,
|
|
655
|
+
0,
|
|
656
|
+
1
|
|
657
|
+
],
|
|
658
|
+
purple: [
|
|
659
|
+
128,
|
|
660
|
+
0,
|
|
661
|
+
128,
|
|
662
|
+
1
|
|
663
|
+
],
|
|
664
|
+
gray: [
|
|
665
|
+
128,
|
|
666
|
+
128,
|
|
667
|
+
128,
|
|
668
|
+
1
|
|
669
|
+
],
|
|
670
|
+
grey: [
|
|
671
|
+
128,
|
|
672
|
+
128,
|
|
673
|
+
128,
|
|
674
|
+
1
|
|
675
|
+
]
|
|
676
|
+
};
|
|
677
|
+
function colorToRgba(input) {
|
|
678
|
+
const s = input.toLowerCase().trim();
|
|
679
|
+
if (NAMED_COLORS[s]) return [...NAMED_COLORS[s]];
|
|
680
|
+
let m;
|
|
681
|
+
if (m = s.match(/^#([0-9a-f]{3})$/)) {
|
|
682
|
+
const [r, g, b] = m[1].split("");
|
|
683
|
+
return [
|
|
684
|
+
parseInt(r + r, 16),
|
|
685
|
+
parseInt(g + g, 16),
|
|
686
|
+
parseInt(b + b, 16),
|
|
687
|
+
1
|
|
688
|
+
];
|
|
689
|
+
}
|
|
690
|
+
if (m = s.match(/^#([0-9a-f]{6})$/)) {
|
|
691
|
+
const n = m[1];
|
|
692
|
+
return [
|
|
693
|
+
parseInt(n.slice(0, 2), 16),
|
|
694
|
+
parseInt(n.slice(2, 4), 16),
|
|
695
|
+
parseInt(n.slice(4, 6), 16),
|
|
696
|
+
1
|
|
697
|
+
];
|
|
698
|
+
}
|
|
699
|
+
if (m = s.match(/^#([0-9a-f]{8})$/)) {
|
|
700
|
+
const n = m[1];
|
|
701
|
+
return [
|
|
702
|
+
parseInt(n.slice(0, 2), 16),
|
|
703
|
+
parseInt(n.slice(2, 4), 16),
|
|
704
|
+
parseInt(n.slice(4, 6), 16),
|
|
705
|
+
parseInt(n.slice(6, 8), 16) / 255
|
|
706
|
+
];
|
|
707
|
+
}
|
|
708
|
+
if (m = s.match(/^rgb\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)$/)) return [
|
|
709
|
+
+m[1],
|
|
710
|
+
+m[2],
|
|
711
|
+
+m[3],
|
|
712
|
+
1
|
|
713
|
+
];
|
|
714
|
+
if (m = s.match(/^rgba\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)$/)) return [
|
|
715
|
+
+m[1],
|
|
716
|
+
+m[2],
|
|
717
|
+
+m[3],
|
|
718
|
+
+m[4]
|
|
719
|
+
];
|
|
720
|
+
return null;
|
|
721
|
+
}
|
|
722
|
+
const NUMERIC_COMPONENT_RE = /[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/g;
|
|
723
|
+
function mapStringToComponents(input) {
|
|
724
|
+
const rgba = colorToRgba(input);
|
|
725
|
+
if (rgba) return {
|
|
726
|
+
isColor: true,
|
|
727
|
+
components: rgba
|
|
728
|
+
};
|
|
729
|
+
const components = [];
|
|
730
|
+
let lastMatchEnd = 0;
|
|
731
|
+
let match;
|
|
732
|
+
NUMERIC_COMPONENT_RE.lastIndex = 0;
|
|
733
|
+
while ((match = NUMERIC_COMPONENT_RE.exec(input)) != null) {
|
|
734
|
+
if (match.index > lastMatchEnd) components.push(input.substring(lastMatchEnd, match.index));
|
|
735
|
+
components.push(parseFloat(match[0]));
|
|
736
|
+
lastMatchEnd = match.index + match[0].length;
|
|
737
|
+
}
|
|
738
|
+
if (components.length === 0) throw new Error("outputRange must contain color or value with numeric component");
|
|
739
|
+
if (lastMatchEnd < input.length) components.push(input.substring(lastMatchEnd, input.length));
|
|
740
|
+
return {
|
|
741
|
+
isColor: false,
|
|
742
|
+
components
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
function prepareStringInterpolation(outputRange) {
|
|
746
|
+
const mapped = outputRange.map(mapStringToComponents);
|
|
747
|
+
const isColor = mapped[0].isColor;
|
|
748
|
+
if (!mapped.every((o) => o.isColor === isColor)) throw new Error("All elements of output range should either be a color or a string with numeric components");
|
|
749
|
+
const first = mapped[0].components;
|
|
750
|
+
if (!mapped.every((o) => o.components.length === first.length)) throw new Error("All elements of output range should have the same number of components");
|
|
751
|
+
if (!mapped.every((o) => o.components.every((c, i) => typeof c === "number" || c === first[i]))) throw new Error("All elements of output range should have the same non-numeric components");
|
|
752
|
+
const numericComponents = mapped.map((o) => isColor ? o.components : o.components.filter((c) => typeof c === "number"));
|
|
753
|
+
return {
|
|
754
|
+
isColor,
|
|
755
|
+
template: first,
|
|
756
|
+
perSlotRanges: numericComponents[0].map((_, i) => numericComponents.map((c) => c[i]))
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
function interpolateString(prepared, value, inputRange, extrapolate, extrapolateLeft, extrapolateRight, easing) {
|
|
760
|
+
const slots = prepared.perSlotRanges.map((range) => interpolateNumeric(value, inputRange, range, extrapolate, extrapolateLeft, extrapolateRight, easing));
|
|
761
|
+
if (prepared.isColor) {
|
|
762
|
+
const r = slots.map((v, i) => i < 3 ? Math.round(v) : Math.round(v * 1e3) / 1e3);
|
|
763
|
+
return `rgba(${r[0]}, ${r[1]}, ${r[2]}, ${r[3]})`;
|
|
764
|
+
}
|
|
765
|
+
let i = 0;
|
|
766
|
+
return prepared.template.map((c) => typeof c === "number" ? slots[i++] : c).join("");
|
|
767
|
+
}
|
|
768
|
+
function checkValidRanges(inputRange, outputRange) {
|
|
769
|
+
if (inputRange.length === 2 && inputRange[0] === -Infinity && inputRange[1] === Infinity) throw new Error("inputRange cannot be [-Infinity, Infinity]");
|
|
770
|
+
if (inputRange.length < 2) throw new Error("inputRange must have at least 2 elements");
|
|
771
|
+
for (let i = 1; i < inputRange.length; ++i) if (!(inputRange[i] >= inputRange[i - 1])) throw new Error("inputRange must be monotonically non-decreasing " + String(inputRange));
|
|
772
|
+
if (inputRange.length !== outputRange.length) throw new Error("inputRange (" + inputRange.length + ") and outputRange (" + outputRange.length + ") must have the same length");
|
|
773
|
+
}
|
|
545
774
|
var AnimatedValue = class AnimatedValue {
|
|
546
775
|
_value;
|
|
547
776
|
_listeners = /* @__PURE__ */ new Map();
|
|
548
777
|
_listenerIdCounter = 0;
|
|
778
|
+
_tracking = null;
|
|
549
779
|
constructor(value = 0) {
|
|
550
780
|
this._value = value;
|
|
551
781
|
}
|
|
@@ -568,16 +798,27 @@ var AnimatedValue = class AnimatedValue {
|
|
|
568
798
|
this._listeners.clear();
|
|
569
799
|
}
|
|
570
800
|
interpolate(config) {
|
|
571
|
-
const { inputRange, outputRange } = config || {};
|
|
801
|
+
const { inputRange, outputRange, extrapolate = "extend", extrapolateLeft, extrapolateRight, easing = IDENTITY_EASING } = config || {};
|
|
572
802
|
if (!inputRange || !outputRange || inputRange.length < 2 || outputRange.length < 2) return new AnimatedValue(this._value);
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
803
|
+
checkValidRanges(inputRange, outputRange);
|
|
804
|
+
if (typeof outputRange[0] === "string") {
|
|
805
|
+
const prepared = prepareStringInterpolation(outputRange);
|
|
806
|
+
const compute = () => interpolateString(prepared, this._value, inputRange, extrapolate, extrapolateLeft, extrapolateRight, easing);
|
|
807
|
+
return {
|
|
808
|
+
getValue: compute,
|
|
809
|
+
__getValue: compute,
|
|
810
|
+
toJSON: compute,
|
|
811
|
+
interpolate: () => {
|
|
812
|
+
throw new Error("Cannot chain an interpolation off a string-valued interpolation");
|
|
813
|
+
},
|
|
814
|
+
addListener: () => "0",
|
|
815
|
+
removeListener: () => {},
|
|
816
|
+
removeAllListeners: () => {},
|
|
817
|
+
stopAnimation: (cb) => cb?.(compute()),
|
|
818
|
+
resetAnimation: (cb) => cb?.(compute())
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
return new AnimatedValue(interpolateNumeric(this._value, inputRange, outputRange, extrapolate, extrapolateLeft, extrapolateRight, easing));
|
|
581
822
|
}
|
|
582
823
|
stopAnimation(callback) {
|
|
583
824
|
callback?.(this._value);
|
|
@@ -585,6 +826,9 @@ var AnimatedValue = class AnimatedValue {
|
|
|
585
826
|
resetAnimation(callback) {
|
|
586
827
|
callback?.(this._value);
|
|
587
828
|
}
|
|
829
|
+
toJSON() {
|
|
830
|
+
return this._value;
|
|
831
|
+
}
|
|
588
832
|
setOffset(_offset) {}
|
|
589
833
|
flattenOffset() {}
|
|
590
834
|
extractOffset() {}
|
|
@@ -635,33 +879,178 @@ var AnimatedValueXY = class {
|
|
|
635
879
|
return [{ translateX: this.x }, { translateY: this.y }];
|
|
636
880
|
}
|
|
637
881
|
};
|
|
882
|
+
const namedColorMap = {
|
|
883
|
+
red: [
|
|
884
|
+
255,
|
|
885
|
+
0,
|
|
886
|
+
0,
|
|
887
|
+
1
|
|
888
|
+
],
|
|
889
|
+
green: [
|
|
890
|
+
0,
|
|
891
|
+
128,
|
|
892
|
+
0,
|
|
893
|
+
1
|
|
894
|
+
],
|
|
895
|
+
blue: [
|
|
896
|
+
0,
|
|
897
|
+
0,
|
|
898
|
+
255,
|
|
899
|
+
1
|
|
900
|
+
],
|
|
901
|
+
white: [
|
|
902
|
+
255,
|
|
903
|
+
255,
|
|
904
|
+
255,
|
|
905
|
+
1
|
|
906
|
+
],
|
|
907
|
+
black: [
|
|
908
|
+
0,
|
|
909
|
+
0,
|
|
910
|
+
0,
|
|
911
|
+
1
|
|
912
|
+
],
|
|
913
|
+
transparent: [
|
|
914
|
+
0,
|
|
915
|
+
0,
|
|
916
|
+
0,
|
|
917
|
+
0
|
|
918
|
+
],
|
|
919
|
+
yellow: [
|
|
920
|
+
255,
|
|
921
|
+
255,
|
|
922
|
+
0,
|
|
923
|
+
1
|
|
924
|
+
],
|
|
925
|
+
cyan: [
|
|
926
|
+
0,
|
|
927
|
+
255,
|
|
928
|
+
255,
|
|
929
|
+
1
|
|
930
|
+
],
|
|
931
|
+
magenta: [
|
|
932
|
+
255,
|
|
933
|
+
0,
|
|
934
|
+
255,
|
|
935
|
+
1
|
|
936
|
+
]
|
|
937
|
+
};
|
|
938
|
+
function parseColorString(color) {
|
|
939
|
+
const rgba = color.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+))?\s*\)$/);
|
|
940
|
+
if (rgba) return [
|
|
941
|
+
parseInt(rgba[1]),
|
|
942
|
+
parseInt(rgba[2]),
|
|
943
|
+
parseInt(rgba[3]),
|
|
944
|
+
rgba[4] != null ? parseFloat(rgba[4]) : 1
|
|
945
|
+
];
|
|
946
|
+
const hex8 = color.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);
|
|
947
|
+
if (hex8) return [
|
|
948
|
+
parseInt(hex8[1], 16),
|
|
949
|
+
parseInt(hex8[2], 16),
|
|
950
|
+
parseInt(hex8[3], 16),
|
|
951
|
+
parseInt(hex8[4], 16) / 255
|
|
952
|
+
];
|
|
953
|
+
const hex6 = color.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);
|
|
954
|
+
if (hex6) return [
|
|
955
|
+
parseInt(hex6[1], 16),
|
|
956
|
+
parseInt(hex6[2], 16),
|
|
957
|
+
parseInt(hex6[3], 16),
|
|
958
|
+
1
|
|
959
|
+
];
|
|
960
|
+
const named = namedColorMap[color.toLowerCase()];
|
|
961
|
+
if (named) return named;
|
|
962
|
+
return [
|
|
963
|
+
0,
|
|
964
|
+
0,
|
|
965
|
+
0,
|
|
966
|
+
1
|
|
967
|
+
];
|
|
968
|
+
}
|
|
638
969
|
var AnimatedColor = class {
|
|
639
970
|
r;
|
|
640
971
|
g;
|
|
641
972
|
b;
|
|
642
973
|
a;
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
974
|
+
_listeners = /* @__PURE__ */ new Map();
|
|
975
|
+
_listenerIdCounter = 0;
|
|
976
|
+
constructor(color) {
|
|
977
|
+
if (typeof color === "string") {
|
|
978
|
+
const [r, g, b, a] = parseColorString(color);
|
|
979
|
+
this.r = new AnimatedValue(r);
|
|
980
|
+
this.g = new AnimatedValue(g);
|
|
981
|
+
this.b = new AnimatedValue(b);
|
|
982
|
+
this.a = new AnimatedValue(a);
|
|
983
|
+
} else if (color && typeof color === "object") if (color.r instanceof AnimatedValue) {
|
|
984
|
+
this.r = color.r;
|
|
985
|
+
this.g = color.g;
|
|
986
|
+
this.b = color.b;
|
|
987
|
+
this.a = color.a;
|
|
988
|
+
} else if (typeof color.r === "number") {
|
|
989
|
+
this.r = new AnimatedValue(color.r);
|
|
990
|
+
this.g = new AnimatedValue(color.g ?? 0);
|
|
991
|
+
this.b = new AnimatedValue(color.b ?? 0);
|
|
992
|
+
this.a = new AnimatedValue(color.a ?? 1);
|
|
993
|
+
} else {
|
|
994
|
+
this.r = new AnimatedValue(0);
|
|
995
|
+
this.g = new AnimatedValue(0);
|
|
996
|
+
this.b = new AnimatedValue(0);
|
|
997
|
+
this.a = new AnimatedValue(1);
|
|
998
|
+
}
|
|
999
|
+
else {
|
|
1000
|
+
this.r = new AnimatedValue(0);
|
|
1001
|
+
this.g = new AnimatedValue(0);
|
|
1002
|
+
this.b = new AnimatedValue(0);
|
|
1003
|
+
this.a = new AnimatedValue(1);
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
setValue(value) {
|
|
1007
|
+
if (typeof value === "string") {
|
|
1008
|
+
const [r, g, b, a] = parseColorString(value);
|
|
1009
|
+
this.r.setValue(r);
|
|
1010
|
+
this.g.setValue(g);
|
|
1011
|
+
this.b.setValue(b);
|
|
1012
|
+
this.a.setValue(a);
|
|
1013
|
+
} else if (value && typeof value === "object" && typeof value.r === "number") {
|
|
1014
|
+
this.r.setValue(value.r);
|
|
1015
|
+
this.g.setValue(value.g ?? 0);
|
|
1016
|
+
this.b.setValue(value.b ?? 0);
|
|
1017
|
+
this.a.setValue(value.a ?? 1);
|
|
1018
|
+
}
|
|
1019
|
+
const colorStr = this.__getValue();
|
|
1020
|
+
this._listeners.forEach((fn) => fn({ value: colorStr }));
|
|
1021
|
+
}
|
|
1022
|
+
__getValue() {
|
|
1023
|
+
return `rgba(${Math.round(this.r.getValue())}, ${Math.round(this.g.getValue())}, ${Math.round(this.b.getValue())}, ${this.a.getValue()})`;
|
|
1024
|
+
}
|
|
1025
|
+
addListener(callback) {
|
|
1026
|
+
const id = String(++this._listenerIdCounter);
|
|
1027
|
+
this._listeners.set(id, callback);
|
|
1028
|
+
return id;
|
|
1029
|
+
}
|
|
1030
|
+
removeListener(id) {
|
|
1031
|
+
this._listeners.delete(id);
|
|
1032
|
+
}
|
|
1033
|
+
removeAllListeners() {
|
|
1034
|
+
this._listeners.clear();
|
|
648
1035
|
}
|
|
649
|
-
setValue(_value) {}
|
|
650
1036
|
setOffset(_offset) {}
|
|
651
1037
|
flattenOffset() {}
|
|
652
1038
|
stopAnimation(callback) {
|
|
653
|
-
callback?.(
|
|
1039
|
+
callback?.(this.__getValue());
|
|
654
1040
|
}
|
|
655
1041
|
resetAnimation(callback) {
|
|
656
|
-
callback?.(
|
|
1042
|
+
callback?.(this.__getValue());
|
|
657
1043
|
}
|
|
658
1044
|
};
|
|
659
|
-
function createAnimation() {
|
|
1045
|
+
function createAnimation(onStart) {
|
|
660
1046
|
return {
|
|
661
1047
|
start: vi.fn((callback) => {
|
|
1048
|
+
onStart?.();
|
|
662
1049
|
callback?.({ finished: true });
|
|
663
1050
|
}),
|
|
664
|
-
stop: vi.fn()
|
|
1051
|
+
stop: vi.fn((callback) => {
|
|
1052
|
+
callback?.({ finished: false });
|
|
1053
|
+
}),
|
|
665
1054
|
reset: vi.fn()
|
|
666
1055
|
};
|
|
667
1056
|
}
|
|
@@ -675,19 +1064,188 @@ function createAnimatedWrapper(displayName) {
|
|
|
675
1064
|
Component.displayName = displayName;
|
|
676
1065
|
return Component;
|
|
677
1066
|
}
|
|
1067
|
+
function stopTracking(value) {
|
|
1068
|
+
if (value._tracking) {
|
|
1069
|
+
value._tracking.source.removeListener(value._tracking.listenerId);
|
|
1070
|
+
value._tracking = null;
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
function startTracking(value, toValue) {
|
|
1074
|
+
stopTracking(value);
|
|
1075
|
+
if (toValue instanceof AnimatedValue) {
|
|
1076
|
+
value.setValue(toValue.getValue());
|
|
1077
|
+
value._tracking = {
|
|
1078
|
+
source: toValue,
|
|
1079
|
+
listenerId: toValue.addListener(({ value: v }) => {
|
|
1080
|
+
value.setValue(v);
|
|
1081
|
+
})
|
|
1082
|
+
};
|
|
1083
|
+
} else value.setValue(toValue);
|
|
1084
|
+
}
|
|
678
1085
|
function createAnimatedMock() {
|
|
679
1086
|
return {
|
|
680
1087
|
Value: AnimatedValue,
|
|
681
1088
|
ValueXY: AnimatedValueXY,
|
|
682
1089
|
Color: AnimatedColor,
|
|
683
|
-
timing: vi.fn((
|
|
684
|
-
|
|
1090
|
+
timing: vi.fn((value, config) => {
|
|
1091
|
+
return createAnimation(() => {
|
|
1092
|
+
if (value instanceof AnimatedValue && config?.toValue != null) startTracking(value, config.toValue);
|
|
1093
|
+
});
|
|
1094
|
+
}),
|
|
1095
|
+
spring: vi.fn((value, config) => {
|
|
1096
|
+
return createAnimation(() => {
|
|
1097
|
+
if (value instanceof AnimatedValue && config?.toValue != null) startTracking(value, config.toValue);
|
|
1098
|
+
});
|
|
1099
|
+
}),
|
|
685
1100
|
decay: vi.fn((_value, _config) => createAnimation()),
|
|
686
|
-
sequence: vi.fn((
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
1101
|
+
sequence: vi.fn((animations) => {
|
|
1102
|
+
let current = 0;
|
|
1103
|
+
let cb;
|
|
1104
|
+
return {
|
|
1105
|
+
start: vi.fn((endCallback) => {
|
|
1106
|
+
cb = endCallback;
|
|
1107
|
+
if (animations.length === 0) {
|
|
1108
|
+
cb?.({ finished: true });
|
|
1109
|
+
return;
|
|
1110
|
+
}
|
|
1111
|
+
if (current >= animations.length) current = 0;
|
|
1112
|
+
const runNext = (result) => {
|
|
1113
|
+
if (!result.finished) {
|
|
1114
|
+
cb?.({ finished: false });
|
|
1115
|
+
return;
|
|
1116
|
+
}
|
|
1117
|
+
current++;
|
|
1118
|
+
if (current >= animations.length) {
|
|
1119
|
+
cb?.({ finished: true });
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
animations[current]?.start(runNext);
|
|
1123
|
+
};
|
|
1124
|
+
animations[current]?.start(runNext);
|
|
1125
|
+
}),
|
|
1126
|
+
stop: vi.fn(() => {
|
|
1127
|
+
if (current < animations.length) animations[current]?.stop?.();
|
|
1128
|
+
}),
|
|
1129
|
+
reset: vi.fn(() => {
|
|
1130
|
+
current = 0;
|
|
1131
|
+
for (const anim of animations) anim?.reset?.();
|
|
1132
|
+
})
|
|
1133
|
+
};
|
|
1134
|
+
}),
|
|
1135
|
+
parallel: vi.fn((animations) => {
|
|
1136
|
+
let doneCount = 0;
|
|
1137
|
+
let hasBeenStopped = false;
|
|
1138
|
+
let cb;
|
|
1139
|
+
const validAnims = animations.filter(Boolean);
|
|
1140
|
+
return {
|
|
1141
|
+
start: vi.fn((endCallback) => {
|
|
1142
|
+
cb = endCallback;
|
|
1143
|
+
doneCount = 0;
|
|
1144
|
+
hasBeenStopped = false;
|
|
1145
|
+
if (validAnims.length === 0) {
|
|
1146
|
+
cb?.({ finished: true });
|
|
1147
|
+
return;
|
|
1148
|
+
}
|
|
1149
|
+
validAnims.forEach((anim) => {
|
|
1150
|
+
anim.start((result) => {
|
|
1151
|
+
doneCount++;
|
|
1152
|
+
if (!result.finished && !hasBeenStopped) {
|
|
1153
|
+
hasBeenStopped = true;
|
|
1154
|
+
validAnims.forEach((other) => {
|
|
1155
|
+
if (other !== anim) other.stop?.();
|
|
1156
|
+
});
|
|
1157
|
+
}
|
|
1158
|
+
if (doneCount >= validAnims.length) cb?.(result);
|
|
1159
|
+
});
|
|
1160
|
+
});
|
|
1161
|
+
}),
|
|
1162
|
+
stop: vi.fn(() => {
|
|
1163
|
+
hasBeenStopped = true;
|
|
1164
|
+
validAnims.forEach((anim) => {
|
|
1165
|
+
anim.stop?.();
|
|
1166
|
+
});
|
|
1167
|
+
}),
|
|
1168
|
+
reset: vi.fn(() => {
|
|
1169
|
+
for (const anim of validAnims) anim?.reset?.();
|
|
1170
|
+
})
|
|
1171
|
+
};
|
|
1172
|
+
}),
|
|
1173
|
+
stagger: vi.fn((_time, animations) => {
|
|
1174
|
+
const validAnims = animations.filter(Boolean);
|
|
1175
|
+
let doneCount = 0;
|
|
1176
|
+
let hasBeenStopped = false;
|
|
1177
|
+
return {
|
|
1178
|
+
start: vi.fn((endCallback) => {
|
|
1179
|
+
doneCount = 0;
|
|
1180
|
+
hasBeenStopped = false;
|
|
1181
|
+
if (validAnims.length === 0) {
|
|
1182
|
+
endCallback?.({ finished: true });
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
validAnims.forEach((anim) => {
|
|
1186
|
+
anim.start?.((result) => {
|
|
1187
|
+
doneCount++;
|
|
1188
|
+
if (!result.finished && !hasBeenStopped) {
|
|
1189
|
+
hasBeenStopped = true;
|
|
1190
|
+
validAnims.forEach((other) => {
|
|
1191
|
+
if (other !== anim) other.stop?.();
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
if (doneCount >= validAnims.length) endCallback?.(result);
|
|
1195
|
+
});
|
|
1196
|
+
});
|
|
1197
|
+
}),
|
|
1198
|
+
stop: vi.fn(() => {
|
|
1199
|
+
hasBeenStopped = true;
|
|
1200
|
+
validAnims.forEach((anim) => anim.stop?.());
|
|
1201
|
+
}),
|
|
1202
|
+
reset: vi.fn(() => {
|
|
1203
|
+
validAnims.forEach((anim) => anim.reset?.());
|
|
1204
|
+
})
|
|
1205
|
+
};
|
|
1206
|
+
}),
|
|
1207
|
+
loop: vi.fn((animation, config) => {
|
|
1208
|
+
const iterations = config?.iterations;
|
|
1209
|
+
const resetBeforeIteration = config?.resetBeforeIteration !== false;
|
|
1210
|
+
let stopped = false;
|
|
1211
|
+
let startCallback;
|
|
1212
|
+
return {
|
|
1213
|
+
start: vi.fn((endCallback) => {
|
|
1214
|
+
stopped = false;
|
|
1215
|
+
startCallback = endCallback;
|
|
1216
|
+
if (iterations === 0) {
|
|
1217
|
+
endCallback?.({ finished: true });
|
|
1218
|
+
return;
|
|
1219
|
+
}
|
|
1220
|
+
let iterationCount = 0;
|
|
1221
|
+
const onIteration = (result) => {
|
|
1222
|
+
if (stopped || !result.finished) {
|
|
1223
|
+
startCallback?.(result);
|
|
1224
|
+
return;
|
|
1225
|
+
}
|
|
1226
|
+
iterationCount++;
|
|
1227
|
+
if (iterations != null && iterations > 0 && iterationCount >= iterations) {
|
|
1228
|
+
startCallback?.({ finished: true });
|
|
1229
|
+
return;
|
|
1230
|
+
}
|
|
1231
|
+
if (resetBeforeIteration) animation.reset?.();
|
|
1232
|
+
animation.start(onIteration);
|
|
1233
|
+
};
|
|
1234
|
+
if (resetBeforeIteration) animation.reset?.();
|
|
1235
|
+
animation.start(onIteration);
|
|
1236
|
+
}),
|
|
1237
|
+
stop: vi.fn(() => {
|
|
1238
|
+
stopped = true;
|
|
1239
|
+
animation.stop?.();
|
|
1240
|
+
}),
|
|
1241
|
+
reset: vi.fn(() => {
|
|
1242
|
+
animation.reset?.();
|
|
1243
|
+
})
|
|
1244
|
+
};
|
|
1245
|
+
}),
|
|
1246
|
+
delay: vi.fn((_time) => {
|
|
1247
|
+
return createAnimation();
|
|
1248
|
+
}),
|
|
691
1249
|
add: vi.fn((a, b) => {
|
|
692
1250
|
return new AnimatedValue((a instanceof AnimatedValue ? a.getValue() : typeof a === "number" ? a : 0) + (b instanceof AnimatedValue ? b.getValue() : typeof b === "number" ? b : 0));
|
|
693
1251
|
}),
|
|
@@ -706,11 +1264,44 @@ function createAnimatedMock() {
|
|
|
706
1264
|
return new AnimatedValue(((a instanceof AnimatedValue ? a.getValue() : typeof a === "number" ? a : 0) % modulus + modulus) % modulus);
|
|
707
1265
|
}),
|
|
708
1266
|
diffClamp: vi.fn((a, min, max) => {
|
|
709
|
-
const
|
|
710
|
-
|
|
1267
|
+
const result = new AnimatedValue(Math.min(Math.max(a instanceof AnimatedValue ? a.getValue() : 0, min), max));
|
|
1268
|
+
if (a instanceof AnimatedValue) {
|
|
1269
|
+
let lastInput = a.getValue();
|
|
1270
|
+
let current = result.getValue();
|
|
1271
|
+
a.addListener(({ value }) => {
|
|
1272
|
+
const diff = value - lastInput;
|
|
1273
|
+
lastInput = value;
|
|
1274
|
+
current = Math.min(Math.max(current + diff, min), max);
|
|
1275
|
+
result.setValue(current);
|
|
1276
|
+
});
|
|
1277
|
+
}
|
|
1278
|
+
return result;
|
|
1279
|
+
}),
|
|
1280
|
+
event: vi.fn((argMapping, config) => {
|
|
1281
|
+
const handler = vi.fn((...args) => {
|
|
1282
|
+
argMapping.forEach((mapping, index) => {
|
|
1283
|
+
if (mapping && args[index]) traverseMapping(mapping, args[index]);
|
|
1284
|
+
});
|
|
1285
|
+
config?.listener?.(...args);
|
|
1286
|
+
});
|
|
1287
|
+
function traverseMapping(mapping, value) {
|
|
1288
|
+
if (mapping instanceof AnimatedValue && typeof value === "number") {
|
|
1289
|
+
mapping.setValue(value);
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
if (typeof mapping === "object" && mapping !== null && typeof value === "object" && value !== null) {
|
|
1293
|
+
for (const key of Object.keys(mapping)) if (key in value) traverseMapping(mapping[key], value[key]);
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
return handler;
|
|
1297
|
+
}),
|
|
1298
|
+
forkEvent: vi.fn((handler, listener) => {
|
|
1299
|
+
return (...args) => {
|
|
1300
|
+
if (typeof handler === "function") handler(...args);
|
|
1301
|
+
else if (handler && handler.__isNative) {}
|
|
1302
|
+
listener(...args);
|
|
1303
|
+
};
|
|
711
1304
|
}),
|
|
712
|
-
event: vi.fn((_argMapping, _config) => vi.fn()),
|
|
713
|
-
forkEvent: vi.fn(),
|
|
714
1305
|
unforkEvent: vi.fn(),
|
|
715
1306
|
createAnimatedComponent: vi.fn((component) => {
|
|
716
1307
|
const Wrapper = React.forwardRef((props, ref) => {
|
|
@@ -934,12 +1525,17 @@ function createAppearanceMock() {
|
|
|
934
1525
|
}
|
|
935
1526
|
//#endregion
|
|
936
1527
|
//#region src/mocks/apis/PixelRatio.ts
|
|
937
|
-
function createPixelRatioMock() {
|
|
1528
|
+
function createPixelRatioMock(getDimensions) {
|
|
1529
|
+
const getScale = () => getDimensions ? getDimensions().scale : 3;
|
|
1530
|
+
const getFontScale = () => getDimensions ? getDimensions().fontScale : 1;
|
|
938
1531
|
return {
|
|
939
|
-
get: vi.fn(() =>
|
|
940
|
-
getFontScale: vi.fn(() =>
|
|
941
|
-
getPixelSizeForLayoutSize: vi.fn((layoutSize) => Math.round(layoutSize *
|
|
942
|
-
roundToNearestPixel: vi.fn((layoutSize) =>
|
|
1532
|
+
get: vi.fn(() => getScale()),
|
|
1533
|
+
getFontScale: vi.fn(() => getFontScale()),
|
|
1534
|
+
getPixelSizeForLayoutSize: vi.fn((layoutSize) => Math.round(layoutSize * getScale())),
|
|
1535
|
+
roundToNearestPixel: vi.fn((layoutSize) => {
|
|
1536
|
+
const ratio = getScale();
|
|
1537
|
+
return Math.round(layoutSize * ratio) / ratio;
|
|
1538
|
+
})
|
|
943
1539
|
};
|
|
944
1540
|
}
|
|
945
1541
|
//#endregion
|
|
@@ -1125,10 +1721,79 @@ function createLogBoxMock() {
|
|
|
1125
1721
|
}
|
|
1126
1722
|
//#endregion
|
|
1127
1723
|
//#region src/mocks/apis/Easing.ts
|
|
1724
|
+
const NEWTON_ITERATIONS = 4;
|
|
1725
|
+
const NEWTON_MIN_SLOPE = .001;
|
|
1726
|
+
const SUBDIVISION_PRECISION = 1e-7;
|
|
1727
|
+
const SUBDIVISION_MAX_ITERATIONS = 10;
|
|
1728
|
+
const kSplineTableSize = 11;
|
|
1729
|
+
const kSampleStepSize = 1 / (kSplineTableSize - 1);
|
|
1730
|
+
function A(aA1, aA2) {
|
|
1731
|
+
return 1 - 3 * aA2 + 3 * aA1;
|
|
1732
|
+
}
|
|
1733
|
+
function B(aA1, aA2) {
|
|
1734
|
+
return 3 * aA2 - 6 * aA1;
|
|
1735
|
+
}
|
|
1736
|
+
function C(aA1) {
|
|
1737
|
+
return 3 * aA1;
|
|
1738
|
+
}
|
|
1739
|
+
function calcBezier(aT, aA1, aA2) {
|
|
1740
|
+
return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT;
|
|
1741
|
+
}
|
|
1742
|
+
function getSlope(aT, aA1, aA2) {
|
|
1743
|
+
return 3 * A(aA1, aA2) * aT * aT + 2 * B(aA1, aA2) * aT + C(aA1);
|
|
1744
|
+
}
|
|
1745
|
+
function binarySubdivide(aX, aA, aB, mX1, mX2) {
|
|
1746
|
+
let currentX;
|
|
1747
|
+
let currentT;
|
|
1748
|
+
let i = 0;
|
|
1749
|
+
let low = aA;
|
|
1750
|
+
let high = aB;
|
|
1751
|
+
do {
|
|
1752
|
+
currentT = low + (high - low) / 2;
|
|
1753
|
+
currentX = calcBezier(currentT, mX1, mX2) - aX;
|
|
1754
|
+
if (currentX > 0) high = currentT;
|
|
1755
|
+
else low = currentT;
|
|
1756
|
+
} while (Math.abs(currentX) > SUBDIVISION_PRECISION && ++i < SUBDIVISION_MAX_ITERATIONS);
|
|
1757
|
+
return currentT;
|
|
1758
|
+
}
|
|
1759
|
+
function newtonRaphsonIterate(aX, aGuessT, mX1, mX2) {
|
|
1760
|
+
let guessT = aGuessT;
|
|
1761
|
+
for (let i = 0; i < NEWTON_ITERATIONS; ++i) {
|
|
1762
|
+
const currentSlope = getSlope(guessT, mX1, mX2);
|
|
1763
|
+
if (currentSlope === 0) return guessT;
|
|
1764
|
+
const currentX = calcBezier(guessT, mX1, mX2) - aX;
|
|
1765
|
+
guessT -= currentX / currentSlope;
|
|
1766
|
+
}
|
|
1767
|
+
return guessT;
|
|
1768
|
+
}
|
|
1769
|
+
function bezier(mX1, mY1, mX2, mY2) {
|
|
1770
|
+
if (!(mX1 >= 0 && mX1 <= 1 && mX2 >= 0 && mX2 <= 1)) throw new Error("bezier x values must be in [0, 1] range");
|
|
1771
|
+
if (mX1 === mY1 && mX2 === mY2) return (x) => x;
|
|
1772
|
+
const sampleValues = new Float32Array(kSplineTableSize);
|
|
1773
|
+
for (let i = 0; i < kSplineTableSize; ++i) sampleValues[i] = calcBezier(i * kSampleStepSize, mX1, mX2);
|
|
1774
|
+
function getTForX(aX) {
|
|
1775
|
+
let intervalStart = 0;
|
|
1776
|
+
let currentSample = 1;
|
|
1777
|
+
const lastSample = kSplineTableSize - 1;
|
|
1778
|
+
for (; currentSample !== lastSample && sampleValues[currentSample] <= aX; ++currentSample) intervalStart += kSampleStepSize;
|
|
1779
|
+
--currentSample;
|
|
1780
|
+
const dist = (aX - sampleValues[currentSample]) / (sampleValues[currentSample + 1] - sampleValues[currentSample]);
|
|
1781
|
+
const guessForT = intervalStart + dist * kSampleStepSize;
|
|
1782
|
+
const initialSlope = getSlope(guessForT, mX1, mX2);
|
|
1783
|
+
if (initialSlope >= NEWTON_MIN_SLOPE) return newtonRaphsonIterate(aX, guessForT, mX1, mX2);
|
|
1784
|
+
else if (initialSlope === 0) return guessForT;
|
|
1785
|
+
else return binarySubdivide(aX, intervalStart, intervalStart + kSampleStepSize, mX1, mX2);
|
|
1786
|
+
}
|
|
1787
|
+
return function(x) {
|
|
1788
|
+
if (x === 0 || x === 1) return x;
|
|
1789
|
+
return calcBezier(getTForX(x), mY1, mY2);
|
|
1790
|
+
};
|
|
1791
|
+
}
|
|
1128
1792
|
function createEasingMock() {
|
|
1793
|
+
const easeFn = bezier(.42, 0, 1, 1);
|
|
1129
1794
|
return {
|
|
1130
1795
|
linear: vi.fn((t) => t),
|
|
1131
|
-
ease: vi.fn((t) =>
|
|
1796
|
+
ease: vi.fn((t) => easeFn(t)),
|
|
1132
1797
|
quad: vi.fn((t) => t * t),
|
|
1133
1798
|
cubic: vi.fn((t) => t * t * t),
|
|
1134
1799
|
poly: vi.fn((n) => (t) => Math.pow(t, n)),
|
|
@@ -1143,21 +1808,17 @@ function createEasingMock() {
|
|
|
1143
1808
|
bounce: vi.fn((t) => {
|
|
1144
1809
|
if (t < 1 / 2.75) return 7.5625 * t * t;
|
|
1145
1810
|
if (t < 2 / 2.75) {
|
|
1146
|
-
t
|
|
1147
|
-
return 7.5625 *
|
|
1811
|
+
const t2 = t - 1.5 / 2.75;
|
|
1812
|
+
return 7.5625 * t2 * t2 + .75;
|
|
1148
1813
|
}
|
|
1149
1814
|
if (t < 2.5 / 2.75) {
|
|
1150
|
-
t
|
|
1151
|
-
return 7.5625 *
|
|
1815
|
+
const t2 = t - 2.25 / 2.75;
|
|
1816
|
+
return 7.5625 * t2 * t2 + .9375;
|
|
1152
1817
|
}
|
|
1153
|
-
t
|
|
1154
|
-
return 7.5625 *
|
|
1155
|
-
}),
|
|
1156
|
-
bezier: vi.fn((x1, _y1, x2, _y2) => (t) => {
|
|
1157
|
-
const cx = 3 * x1;
|
|
1158
|
-
const bx = 3 * (x2 - x1) - cx;
|
|
1159
|
-
return (((1 - cx - bx) * t + bx) * t + cx) * t;
|
|
1818
|
+
const t2 = t - 2.625 / 2.75;
|
|
1819
|
+
return 7.5625 * t2 * t2 + .984375;
|
|
1160
1820
|
}),
|
|
1821
|
+
bezier: vi.fn((x1, y1, x2, y2) => bezier(x1, y1, x2, y2)),
|
|
1161
1822
|
in: vi.fn((easing) => easing),
|
|
1162
1823
|
out: vi.fn((easing) => (t) => 1 - easing(1 - t)),
|
|
1163
1824
|
inOut: vi.fn((easing) => (t) => {
|
|
@@ -1288,27 +1949,42 @@ function createUseWindowDimensionsMock() {
|
|
|
1288
1949
|
//#endregion
|
|
1289
1950
|
//#region src/mocks/native/NativeModules.ts
|
|
1290
1951
|
/**
|
|
1291
|
-
* Creates a
|
|
1292
|
-
*
|
|
1293
|
-
*
|
|
1294
|
-
*
|
|
1295
|
-
* Property accesses are cached so that NativeModules.Foo === NativeModules.Foo
|
|
1296
|
-
* holds true, matching real React Native identity semantics.
|
|
1952
|
+
* Creates a callable proxy — used for module methods.
|
|
1953
|
+
* Calling it returns undefined; accessing properties returns another
|
|
1954
|
+
* callable proxy (supporting arbitrary depth like Foo.bar.baz()).
|
|
1297
1955
|
*/
|
|
1298
|
-
function
|
|
1956
|
+
function createCallableProxy() {
|
|
1299
1957
|
const cache = /* @__PURE__ */ new Map();
|
|
1300
1958
|
const noop = () => {};
|
|
1301
1959
|
return new Proxy(noop, {
|
|
1302
1960
|
get(_target, prop) {
|
|
1303
1961
|
if (typeof prop === "symbol") return void 0;
|
|
1304
1962
|
if (cache.has(prop)) return cache.get(prop);
|
|
1305
|
-
const child =
|
|
1963
|
+
const child = createCallableProxy();
|
|
1306
1964
|
cache.set(prop, child);
|
|
1307
1965
|
return child;
|
|
1308
1966
|
},
|
|
1309
1967
|
apply() {}
|
|
1310
1968
|
});
|
|
1311
1969
|
}
|
|
1970
|
+
/**
|
|
1971
|
+
* Creates an object proxy for a native module.
|
|
1972
|
+
* typeof returns "object" (matching real RN), property access returns
|
|
1973
|
+
* callable proxies so NativeModules.Foo.bar() works without throwing.
|
|
1974
|
+
*
|
|
1975
|
+
* Property accesses are cached so that NativeModules.Foo === NativeModules.Foo
|
|
1976
|
+
* holds true, matching real React Native identity semantics.
|
|
1977
|
+
*/
|
|
1978
|
+
function createModuleProxy$1() {
|
|
1979
|
+
const cache = /* @__PURE__ */ new Map();
|
|
1980
|
+
return new Proxy({}, { get(_target, prop) {
|
|
1981
|
+
if (typeof prop === "symbol") return void 0;
|
|
1982
|
+
if (cache.has(prop)) return cache.get(prop);
|
|
1983
|
+
const child = createCallableProxy();
|
|
1984
|
+
cache.set(prop, child);
|
|
1985
|
+
return child;
|
|
1986
|
+
} });
|
|
1987
|
+
}
|
|
1312
1988
|
function createNativeModulesMock() {
|
|
1313
1989
|
const cache = /* @__PURE__ */ new Map();
|
|
1314
1990
|
return new Proxy({}, { get(_target, prop) {
|
|
@@ -1440,10 +2116,96 @@ function createAppRegistryMock() {
|
|
|
1440
2116
|
cancelHeadlessTask: vi.fn()
|
|
1441
2117
|
};
|
|
1442
2118
|
}
|
|
2119
|
+
function hslToRgb(h, s, l) {
|
|
2120
|
+
const sNorm = s / 100;
|
|
2121
|
+
const lNorm = l / 100;
|
|
2122
|
+
const c = (1 - Math.abs(2 * lNorm - 1)) * sNorm;
|
|
2123
|
+
const hPrime = h / 60;
|
|
2124
|
+
const x = c * (1 - Math.abs(hPrime % 2 - 1));
|
|
2125
|
+
let r1 = 0, g1 = 0, b1 = 0;
|
|
2126
|
+
if (hPrime < 1) {
|
|
2127
|
+
r1 = c;
|
|
2128
|
+
g1 = x;
|
|
2129
|
+
} else if (hPrime < 2) {
|
|
2130
|
+
r1 = x;
|
|
2131
|
+
g1 = c;
|
|
2132
|
+
} else if (hPrime < 3) {
|
|
2133
|
+
g1 = c;
|
|
2134
|
+
b1 = x;
|
|
2135
|
+
} else if (hPrime < 4) {
|
|
2136
|
+
g1 = x;
|
|
2137
|
+
b1 = c;
|
|
2138
|
+
} else if (hPrime < 5) {
|
|
2139
|
+
r1 = x;
|
|
2140
|
+
b1 = c;
|
|
2141
|
+
} else {
|
|
2142
|
+
r1 = c;
|
|
2143
|
+
b1 = x;
|
|
2144
|
+
}
|
|
2145
|
+
const m = lNorm - c / 2;
|
|
2146
|
+
return [
|
|
2147
|
+
Math.round((r1 + m) * 255),
|
|
2148
|
+
Math.round((g1 + m) * 255),
|
|
2149
|
+
Math.round((b1 + m) * 255)
|
|
2150
|
+
];
|
|
2151
|
+
}
|
|
1443
2152
|
function createProcessColorMock() {
|
|
2153
|
+
const namedColors = {
|
|
2154
|
+
red: 4294901760,
|
|
2155
|
+
green: 4278222848,
|
|
2156
|
+
blue: 4278190335,
|
|
2157
|
+
white: 4294967295,
|
|
2158
|
+
black: 4278190080,
|
|
2159
|
+
transparent: 0,
|
|
2160
|
+
yellow: 4294967040,
|
|
2161
|
+
cyan: 4278255615,
|
|
2162
|
+
magenta: 4294902015,
|
|
2163
|
+
orange: 4294944e3,
|
|
2164
|
+
purple: 4286578816,
|
|
2165
|
+
gray: 4286611584,
|
|
2166
|
+
grey: 4286611584
|
|
2167
|
+
};
|
|
1444
2168
|
return vi.fn((color) => {
|
|
1445
2169
|
if (color == null) return null;
|
|
1446
2170
|
if (typeof color === "number") return color;
|
|
2171
|
+
if (typeof color === "string") {
|
|
2172
|
+
const lower = color.toLowerCase().trim();
|
|
2173
|
+
if (namedColors[lower] != null) return namedColors[lower];
|
|
2174
|
+
const hex6 = lower.match(/^#([0-9a-f]{6})$/);
|
|
2175
|
+
if (hex6) return 4278190080 + parseInt(hex6[1], 16) >>> 0;
|
|
2176
|
+
const hex3 = lower.match(/^#([0-9a-f]{3})$/);
|
|
2177
|
+
if (hex3) {
|
|
2178
|
+
const [r, g, b] = hex3[1].split("");
|
|
2179
|
+
return 4278190080 + parseInt(`${r}${r}${g}${g}${b}${b}`, 16) >>> 0;
|
|
2180
|
+
}
|
|
2181
|
+
const hex8 = lower.match(/^#([0-9a-f]{8})$/);
|
|
2182
|
+
if (hex8) {
|
|
2183
|
+
const rgb = parseInt(hex8[1].slice(0, 6), 16);
|
|
2184
|
+
return (parseInt(hex8[1].slice(6, 8), 16) << 24) + rgb >>> 0;
|
|
2185
|
+
}
|
|
2186
|
+
const rgbMatch = lower.match(/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/);
|
|
2187
|
+
if (rgbMatch) {
|
|
2188
|
+
const [, r, g, b] = rgbMatch;
|
|
2189
|
+
return (255 << 24) + (parseInt(r) << 16) + (parseInt(g) << 8) + parseInt(b) >>> 0;
|
|
2190
|
+
}
|
|
2191
|
+
const rgbaMatch = lower.match(/^rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*([\d.]+)\s*\)$/);
|
|
2192
|
+
if (rgbaMatch) {
|
|
2193
|
+
const [, r, g, b, a] = rgbaMatch;
|
|
2194
|
+
return (Math.round(parseFloat(a) * 255) << 24) + (parseInt(r) << 16) + (parseInt(g) << 8) + parseInt(b) >>> 0;
|
|
2195
|
+
}
|
|
2196
|
+
const hslMatch = lower.match(/^hsl\(\s*([\d.]+)\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%\s*\)$/);
|
|
2197
|
+
if (hslMatch) {
|
|
2198
|
+
const [, h, s, l] = hslMatch;
|
|
2199
|
+
const [r, g, b] = hslToRgb(parseFloat(h), parseFloat(s), parseFloat(l));
|
|
2200
|
+
return (255 << 24) + (r << 16) + (g << 8) + b >>> 0;
|
|
2201
|
+
}
|
|
2202
|
+
const hslaMatch = lower.match(/^hsla\(\s*([\d.]+)\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%\s*,\s*([\d.]+)\s*\)$/);
|
|
2203
|
+
if (hslaMatch) {
|
|
2204
|
+
const [, h, s, l, a] = hslaMatch;
|
|
2205
|
+
const [r, g, b] = hslToRgb(parseFloat(h), parseFloat(s), parseFloat(l));
|
|
2206
|
+
return (Math.round(parseFloat(a) * 255) << 24) + (r << 16) + (g << 8) + b >>> 0;
|
|
2207
|
+
}
|
|
2208
|
+
}
|
|
1447
2209
|
return 4278190080;
|
|
1448
2210
|
});
|
|
1449
2211
|
}
|
|
@@ -1545,7 +2307,7 @@ function buildReactNativeMock(platform = "ios") {
|
|
|
1545
2307
|
DrawerLayoutAndroid: createDrawerLayoutAndroidMock(),
|
|
1546
2308
|
Platform: createPlatformMock(platform),
|
|
1547
2309
|
Dimensions: createDimensionsMock(),
|
|
1548
|
-
StyleSheet: createStyleSheetMock(),
|
|
2310
|
+
StyleSheet: createStyleSheetMock(() => mock.Dimensions.get("window").scale),
|
|
1549
2311
|
Animated: createAnimatedMock(),
|
|
1550
2312
|
Alert: createAlertMock(),
|
|
1551
2313
|
Linking: createLinkingMock(),
|
|
@@ -1555,7 +2317,7 @@ function buildReactNativeMock(platform = "ios") {
|
|
|
1555
2317
|
Vibration: createVibrationMock(),
|
|
1556
2318
|
PermissionsAndroid: createPermissionsAndroidMock(),
|
|
1557
2319
|
Appearance: createAppearanceMock(),
|
|
1558
|
-
PixelRatio:
|
|
2320
|
+
PixelRatio: void 0,
|
|
1559
2321
|
LayoutAnimation: createLayoutAnimationMock(),
|
|
1560
2322
|
Clipboard: createClipboardMock(),
|
|
1561
2323
|
Share: createShareMock(),
|
|
@@ -1645,6 +2407,17 @@ function buildReactNativeMock(platform = "ios") {
|
|
|
1645
2407
|
usePressability: vi.fn(() => ({})),
|
|
1646
2408
|
default: void 0
|
|
1647
2409
|
};
|
|
2410
|
+
mock.PixelRatio = createPixelRatioMock(() => mock.Dimensions.get("window"));
|
|
2411
|
+
const origSetColorScheme = mock.Appearance.setColorScheme;
|
|
2412
|
+
mock.Appearance.setColorScheme = vi.fn((scheme) => {
|
|
2413
|
+
origSetColorScheme(scheme);
|
|
2414
|
+
mock.useColorScheme._setScheme(scheme);
|
|
2415
|
+
});
|
|
2416
|
+
const origDimensionsSet = mock.Dimensions.set;
|
|
2417
|
+
mock.Dimensions.set = vi.fn((dims) => {
|
|
2418
|
+
origDimensionsSet(dims);
|
|
2419
|
+
if (dims.window) mock.useWindowDimensions._setDimensions(dims.window);
|
|
2420
|
+
});
|
|
1648
2421
|
mock.default = mock;
|
|
1649
2422
|
return mock;
|
|
1650
2423
|
}
|
|
@@ -1668,6 +2441,7 @@ function buildReactNativeMock(platform = "ios") {
|
|
|
1668
2441
|
const VIRTUAL_RN_PATH = "\0vitest-native:react-native";
|
|
1669
2442
|
let originalResolveFilename = null;
|
|
1670
2443
|
let installed = false;
|
|
2444
|
+
const presetRedirects = /* @__PURE__ */ new Map();
|
|
1671
2445
|
function installCjsBridge(mockObject) {
|
|
1672
2446
|
if (installed) return;
|
|
1673
2447
|
try {
|
|
@@ -1680,8 +2454,15 @@ function installCjsBridge(mockObject) {
|
|
|
1680
2454
|
Module._cache[VIRTUAL_RN_PATH] = syntheticModule;
|
|
1681
2455
|
originalResolveFilename = Module._resolveFilename;
|
|
1682
2456
|
Module._resolveFilename = function(request, parent, isMain, options) {
|
|
1683
|
-
if (request === "react-native") return VIRTUAL_RN_PATH;
|
|
1684
|
-
|
|
2457
|
+
if (request === "react-native" || request.startsWith("react-native/")) return VIRTUAL_RN_PATH;
|
|
2458
|
+
const exactMatch = presetRedirects.get(request);
|
|
2459
|
+
if (exactMatch) return exactMatch;
|
|
2460
|
+
const slashIdx = request.indexOf("/", request.startsWith("@") ? request.indexOf("/") + 1 : 0);
|
|
2461
|
+
if (slashIdx !== -1) {
|
|
2462
|
+
const pkg = request.slice(0, slashIdx);
|
|
2463
|
+
const subpathMatch = presetRedirects.get(pkg);
|
|
2464
|
+
if (subpathMatch) return subpathMatch;
|
|
2465
|
+
}
|
|
1685
2466
|
return originalResolveFilename.call(this, request, parent, isMain, options);
|
|
1686
2467
|
};
|
|
1687
2468
|
installed = true;
|
|
@@ -1698,6 +2479,8 @@ function uninstallCjsBridge() {
|
|
|
1698
2479
|
originalResolveFilename = null;
|
|
1699
2480
|
}
|
|
1700
2481
|
delete Module._cache[VIRTUAL_RN_PATH];
|
|
2482
|
+
for (const virtualPath of presetRedirects.values()) delete Module._cache[virtualPath];
|
|
2483
|
+
presetRedirects.clear();
|
|
1701
2484
|
installed = false;
|
|
1702
2485
|
} catch (e) {
|
|
1703
2486
|
if (process.env.VITEST_NATIVE_DIAGNOSTICS === "true") console.warn(`[vitest-native] CJS bridge uninstall failed: ${e?.message}`);
|
|
@@ -1713,11 +2496,7 @@ function installPresetCjsBridge(moduleName, mockObject) {
|
|
|
1713
2496
|
syntheticModule.loaded = true;
|
|
1714
2497
|
syntheticModule.exports = mockObject;
|
|
1715
2498
|
Module._cache[virtualPath] = syntheticModule;
|
|
1716
|
-
|
|
1717
|
-
Module._resolveFilename = function(request, parent, isMain, options) {
|
|
1718
|
-
if (request === moduleName || request.startsWith(`${moduleName}/`)) return virtualPath;
|
|
1719
|
-
return currentResolve.call(this, request, parent, isMain, options);
|
|
1720
|
-
};
|
|
2499
|
+
presetRedirects.set(moduleName, virtualPath);
|
|
1721
2500
|
} catch (e) {
|
|
1722
2501
|
if (process.env.VITEST_NATIVE_DIAGNOSTICS === "true") console.warn(`[vitest-native] Preset CJS bridge for ${moduleName} failed: ${e?.message}`);
|
|
1723
2502
|
}
|
|
@@ -1802,9 +2581,9 @@ function loadPresetFactory(exportName) {
|
|
|
1802
2581
|
return typeof factory === "function" ? factory : null;
|
|
1803
2582
|
}
|
|
1804
2583
|
function autoDetectPresets() {
|
|
1805
|
-
const detected = [];
|
|
1806
2584
|
const projectRoot = process.env.VITEST_NATIVE_PROJECT_ROOT || process.cwd();
|
|
1807
2585
|
const projectReq = createRequire(path.join(projectRoot, "package.json"));
|
|
2586
|
+
const detected = [];
|
|
1808
2587
|
for (const [pkgName, exportName] of Object.entries(AUTO_DETECT_PRESETS)) try {
|
|
1809
2588
|
projectReq.resolve(pkgName);
|
|
1810
2589
|
const factory = loadPresetFactory(exportName);
|
|
@@ -1836,8 +2615,8 @@ try {
|
|
|
1836
2615
|
if (rntl?.configure) rntl.configure({ hostComponentNames: {
|
|
1837
2616
|
text: "Text",
|
|
1838
2617
|
textInput: "TextInput",
|
|
1839
|
-
switch: "
|
|
1840
|
-
scrollView: "
|
|
2618
|
+
switch: "RCTSwitch",
|
|
2619
|
+
scrollView: "RCTScrollView",
|
|
1841
2620
|
modal: "Modal",
|
|
1842
2621
|
image: "Image",
|
|
1843
2622
|
button: "Button",
|
|
@@ -1861,6 +2640,8 @@ try {
|
|
|
1861
2640
|
} catch (e) {
|
|
1862
2641
|
if (diagnostics) console.log(`[vitest-native] Could not load RNTL matchers: ${e?.message}`);
|
|
1863
2642
|
}
|
|
2643
|
+
expect.extend(animatedMatchers);
|
|
2644
|
+
if (diagnostics) console.log(`[vitest-native] Registered ${Object.keys(animatedMatchers).length} animated matchers: ${Object.keys(animatedMatchers).join(", ")}`);
|
|
1864
2645
|
expect.addSnapshotSerializer(serializer);
|
|
1865
2646
|
afterAll(() => {
|
|
1866
2647
|
uninstallCjsBridge();
|