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