vitest-native 0.2.1 → 0.3.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 +14 -0
- package/README.md +31 -1
- 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.cjs +58 -28
- package/dist/index.mjs +57 -28
- package/dist/{preset-map-D224CC5w.mjs → preset-map-BTpoNH3G.mjs} +1 -0
- package/dist/{preset-map-Bt4dXkZO.cjs → preset-map-DxU6UgYS.cjs} +1 -0
- package/dist/{presets-9Y4ssfiH.cjs → presets-B-spxZz7.cjs} +331 -81
- package/dist/{presets-CWH0gtM0.mjs → presets-DvrcfEPz.mjs} +331 -81
- package/dist/presets.cjs +1 -1
- package/dist/presets.mjs +1 -1
- package/dist/setup.cjs +719 -85
- package/dist/setup.mjs +719 -85
- package/package.json +4 -1
package/dist/setup.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const require_presets = require("./presets-
|
|
2
|
-
const require_preset_map = require("./preset-map-
|
|
1
|
+
const require_presets = require("./presets-B-spxZz7.cjs");
|
|
2
|
+
const require_preset_map = require("./preset-map-DxU6UgYS.cjs");
|
|
3
3
|
const require_serializer = require("./serializer.cjs");
|
|
4
4
|
let node_path = require("node:path");
|
|
5
5
|
node_path = require_presets.__toESM(node_path);
|
|
@@ -22,8 +22,15 @@ function createViewMock() {
|
|
|
22
22
|
//#region src/mocks/components/Text.ts
|
|
23
23
|
function createTextMock() {
|
|
24
24
|
const Text = react.default.forwardRef((props, ref) => {
|
|
25
|
+
const { onPress, onLongPress, disabled, accessibilityRole, ...rest } = props;
|
|
26
|
+
const resolvedRole = accessibilityRole ?? (!disabled && (onPress || onLongPress) ? "link" : void 0);
|
|
25
27
|
return react.default.createElement("Text", {
|
|
26
|
-
|
|
28
|
+
accessible: true,
|
|
29
|
+
...rest,
|
|
30
|
+
...onPress ? { onPress } : {},
|
|
31
|
+
...onLongPress ? { onLongPress } : {},
|
|
32
|
+
...disabled ? { disabled } : {},
|
|
33
|
+
...resolvedRole ? { accessibilityRole: resolvedRole } : {},
|
|
27
34
|
ref
|
|
28
35
|
});
|
|
29
36
|
});
|
|
@@ -82,6 +89,7 @@ function createTextInputMock() {
|
|
|
82
89
|
//#region src/mocks/components/ScrollView.ts
|
|
83
90
|
function createScrollViewMock() {
|
|
84
91
|
const ScrollView = react.default.forwardRef((props, ref) => {
|
|
92
|
+
const { children, refreshControl, ...rest } = props;
|
|
85
93
|
const instanceMethods = {
|
|
86
94
|
scrollTo: vitest.vi.fn(),
|
|
87
95
|
scrollToEnd: vitest.vi.fn(),
|
|
@@ -91,10 +99,10 @@ function createScrollViewMock() {
|
|
|
91
99
|
setNativeProps: vitest.vi.fn()
|
|
92
100
|
};
|
|
93
101
|
react.default.useImperativeHandle(ref, () => instanceMethods);
|
|
94
|
-
return react.default.createElement("
|
|
95
|
-
...
|
|
102
|
+
return react.default.createElement("RCTScrollView", {
|
|
103
|
+
...rest,
|
|
96
104
|
ref
|
|
97
|
-
});
|
|
105
|
+
}, refreshControl, react.default.createElement("View", null, children));
|
|
98
106
|
});
|
|
99
107
|
ScrollView.displayName = "ScrollView";
|
|
100
108
|
return ScrollView;
|
|
@@ -211,8 +219,15 @@ function createModalMock() {
|
|
|
211
219
|
//#region src/mocks/components/Pressable.ts
|
|
212
220
|
function createPressableMock() {
|
|
213
221
|
const Pressable = react.default.forwardRef((props, ref) => {
|
|
222
|
+
const { disabled, accessibilityState, ...rest } = props;
|
|
223
|
+
const mergedA11yState = disabled || accessibilityState ? {
|
|
224
|
+
...accessibilityState,
|
|
225
|
+
...disabled ? { disabled: true } : {}
|
|
226
|
+
} : void 0;
|
|
214
227
|
return react.default.createElement("Pressable", {
|
|
215
|
-
|
|
228
|
+
accessible: true,
|
|
229
|
+
...rest,
|
|
230
|
+
...mergedA11yState ? { accessibilityState: mergedA11yState } : {},
|
|
216
231
|
ref
|
|
217
232
|
});
|
|
218
233
|
});
|
|
@@ -223,8 +238,15 @@ function createPressableMock() {
|
|
|
223
238
|
//#region src/mocks/components/TouchableOpacity.ts
|
|
224
239
|
function createTouchableOpacityMock() {
|
|
225
240
|
const TouchableOpacity = react.default.forwardRef((props, ref) => {
|
|
241
|
+
const { disabled, accessibilityState, ...rest } = props;
|
|
242
|
+
const mergedA11yState = disabled || accessibilityState ? {
|
|
243
|
+
...accessibilityState,
|
|
244
|
+
...disabled ? { disabled: true } : {}
|
|
245
|
+
} : void 0;
|
|
226
246
|
return react.default.createElement("TouchableOpacity", {
|
|
227
|
-
|
|
247
|
+
accessible: true,
|
|
248
|
+
...rest,
|
|
249
|
+
...mergedA11yState ? { accessibilityState: mergedA11yState } : {},
|
|
228
250
|
ref
|
|
229
251
|
});
|
|
230
252
|
});
|
|
@@ -235,8 +257,15 @@ function createTouchableOpacityMock() {
|
|
|
235
257
|
//#region src/mocks/components/TouchableHighlight.ts
|
|
236
258
|
function createTouchableHighlightMock() {
|
|
237
259
|
const TouchableHighlight = react.default.forwardRef((props, ref) => {
|
|
260
|
+
const { disabled, accessibilityState, ...rest } = props;
|
|
261
|
+
const mergedA11yState = disabled || accessibilityState ? {
|
|
262
|
+
...accessibilityState,
|
|
263
|
+
...disabled ? { disabled: true } : {}
|
|
264
|
+
} : void 0;
|
|
238
265
|
return react.default.createElement("TouchableHighlight", {
|
|
239
|
-
|
|
266
|
+
accessible: true,
|
|
267
|
+
...rest,
|
|
268
|
+
...mergedA11yState ? { accessibilityState: mergedA11yState } : {},
|
|
240
269
|
ref
|
|
241
270
|
});
|
|
242
271
|
});
|
|
@@ -247,8 +276,15 @@ function createTouchableHighlightMock() {
|
|
|
247
276
|
//#region src/mocks/components/TouchableWithoutFeedback.ts
|
|
248
277
|
function createTouchableWithoutFeedbackMock() {
|
|
249
278
|
const TouchableWithoutFeedback = react.default.forwardRef((props, ref) => {
|
|
279
|
+
const { disabled, accessibilityState, ...rest } = props;
|
|
280
|
+
const mergedA11yState = disabled || accessibilityState ? {
|
|
281
|
+
...accessibilityState,
|
|
282
|
+
...disabled ? { disabled: true } : {}
|
|
283
|
+
} : void 0;
|
|
250
284
|
return react.default.createElement("TouchableWithoutFeedback", {
|
|
251
|
-
|
|
285
|
+
accessible: true,
|
|
286
|
+
...rest,
|
|
287
|
+
...mergedA11yState ? { accessibilityState: mergedA11yState } : {},
|
|
252
288
|
ref
|
|
253
289
|
});
|
|
254
290
|
});
|
|
@@ -259,8 +295,15 @@ function createTouchableWithoutFeedbackMock() {
|
|
|
259
295
|
//#region src/mocks/components/TouchableNativeFeedback.ts
|
|
260
296
|
function createTouchableNativeFeedbackMock() {
|
|
261
297
|
const TouchableNativeFeedback = react.default.forwardRef((props, ref) => {
|
|
298
|
+
const { disabled, accessibilityState, ...rest } = props;
|
|
299
|
+
const mergedA11yState = disabled || accessibilityState ? {
|
|
300
|
+
...accessibilityState,
|
|
301
|
+
...disabled ? { disabled: true } : {}
|
|
302
|
+
} : void 0;
|
|
262
303
|
return react.default.createElement("TouchableNativeFeedback", {
|
|
263
|
-
|
|
304
|
+
accessible: true,
|
|
305
|
+
...rest,
|
|
306
|
+
...mergedA11yState ? { accessibilityState: mergedA11yState } : {},
|
|
264
307
|
ref
|
|
265
308
|
});
|
|
266
309
|
});
|
|
@@ -283,16 +326,22 @@ function createActivityIndicatorMock() {
|
|
|
283
326
|
//#region src/mocks/components/Button.ts
|
|
284
327
|
function createButtonMock() {
|
|
285
328
|
function Button(props) {
|
|
286
|
-
const { title, onPress, disabled, color, accessibilityLabel, testID, ...rest } = props;
|
|
329
|
+
const { title, onPress, disabled, color, accessibilityLabel, accessibilityState, testID, ...rest } = props;
|
|
287
330
|
return react.default.createElement("View", {
|
|
288
331
|
...rest,
|
|
289
|
-
testID
|
|
290
|
-
}, react.default.createElement("Text", {
|
|
332
|
+
testID,
|
|
291
333
|
onPress: disabled ? void 0 : onPress,
|
|
292
334
|
accessibilityLabel: accessibilityLabel || title,
|
|
293
335
|
accessibilityRole: "button",
|
|
294
|
-
disabled
|
|
295
|
-
|
|
336
|
+
accessibilityState: disabled ? {
|
|
337
|
+
...accessibilityState,
|
|
338
|
+
disabled: true
|
|
339
|
+
} : accessibilityState,
|
|
340
|
+
accessible: true,
|
|
341
|
+
disabled
|
|
342
|
+
}, react.default.createElement("Text", {
|
|
343
|
+
style: color ? { color } : void 0,
|
|
344
|
+
disabled
|
|
296
345
|
}, title));
|
|
297
346
|
}
|
|
298
347
|
Button.displayName = "Button";
|
|
@@ -302,7 +351,7 @@ function createButtonMock() {
|
|
|
302
351
|
//#region src/mocks/components/Switch.ts
|
|
303
352
|
function createSwitchMock() {
|
|
304
353
|
const Switch = react.default.forwardRef((props, ref) => {
|
|
305
|
-
return react.default.createElement("
|
|
354
|
+
return react.default.createElement("RCTSwitch", {
|
|
306
355
|
...props,
|
|
307
356
|
ref
|
|
308
357
|
});
|
|
@@ -458,7 +507,7 @@ function createPlatformMock(os = "ios") {
|
|
|
458
507
|
isVision: false,
|
|
459
508
|
isTesting: true,
|
|
460
509
|
select: vitest.vi.fn((specifics) => {
|
|
461
|
-
return specifics[os] ?? specifics.default;
|
|
510
|
+
return specifics[os] ?? specifics.native ?? specifics.default;
|
|
462
511
|
}),
|
|
463
512
|
constants: {
|
|
464
513
|
reactNativeVersion: {
|
|
@@ -516,12 +565,26 @@ function createDimensionsMock() {
|
|
|
516
565
|
}
|
|
517
566
|
//#endregion
|
|
518
567
|
//#region src/mocks/apis/StyleSheet.ts
|
|
568
|
+
function flattenImpl(style) {
|
|
569
|
+
if (style == null || style === false) return void 0;
|
|
570
|
+
if (!Array.isArray(style)) return style;
|
|
571
|
+
const result = {};
|
|
572
|
+
let hasAny = false;
|
|
573
|
+
for (const item of style) {
|
|
574
|
+
const flattened = flattenImpl(item);
|
|
575
|
+
if (flattened != null) {
|
|
576
|
+
hasAny = true;
|
|
577
|
+
Object.assign(result, flattened);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return hasAny ? result : void 0;
|
|
581
|
+
}
|
|
519
582
|
function createStyleSheetMock() {
|
|
520
583
|
return {
|
|
521
584
|
create: vitest.vi.fn((styles) => {
|
|
522
585
|
return styles;
|
|
523
586
|
}),
|
|
524
|
-
flatten: vitest.vi.fn((
|
|
587
|
+
flatten: vitest.vi.fn((style) => flattenImpl(style)),
|
|
525
588
|
compose: vitest.vi.fn((a, b) => [a, b]),
|
|
526
589
|
absoluteFill: {
|
|
527
590
|
position: "absolute",
|
|
@@ -543,10 +606,50 @@ function createStyleSheetMock() {
|
|
|
543
606
|
}
|
|
544
607
|
//#endregion
|
|
545
608
|
//#region src/mocks/apis/Animated.ts
|
|
609
|
+
function toNum(v) {
|
|
610
|
+
return typeof v === "number" ? v : parseFloat(v) || 0;
|
|
611
|
+
}
|
|
612
|
+
function interpolateValue(value, inputRange, outputRange, extrapolate, extrapolateLeft, extrapolateRight, easing) {
|
|
613
|
+
let i = 0;
|
|
614
|
+
for (; i < inputRange.length - 2; i++) if (value <= inputRange[i + 1]) break;
|
|
615
|
+
const inMin = inputRange[i];
|
|
616
|
+
const inMax = inputRange[i + 1];
|
|
617
|
+
const outMin = toNum(outputRange[i]);
|
|
618
|
+
const outMax = toNum(outputRange[i + 1]);
|
|
619
|
+
if (inMax === inMin) {
|
|
620
|
+
const result = value <= inMin ? outMin : outMax;
|
|
621
|
+
if (value < inputRange[0]) {
|
|
622
|
+
const leftMode = extrapolateLeft || extrapolate;
|
|
623
|
+
if (leftMode === "clamp") return toNum(outputRange[0]);
|
|
624
|
+
if (leftMode === "identity") return value;
|
|
625
|
+
}
|
|
626
|
+
if (value > inputRange[inputRange.length - 1]) {
|
|
627
|
+
const rightMode = extrapolateRight || extrapolate;
|
|
628
|
+
if (rightMode === "clamp") return toNum(outputRange[outputRange.length - 1]);
|
|
629
|
+
if (rightMode === "identity") return value;
|
|
630
|
+
}
|
|
631
|
+
return result;
|
|
632
|
+
}
|
|
633
|
+
let t = (value - inMin) / (inMax - inMin);
|
|
634
|
+
if (easing) t = easing(t);
|
|
635
|
+
let result = outMin + t * (outMax - outMin);
|
|
636
|
+
const leftMode = extrapolateLeft || extrapolate;
|
|
637
|
+
const rightMode = extrapolateRight || extrapolate;
|
|
638
|
+
if (value < inputRange[0]) {
|
|
639
|
+
if (leftMode === "clamp") return toNum(outputRange[0]);
|
|
640
|
+
if (leftMode === "identity") return value;
|
|
641
|
+
}
|
|
642
|
+
if (value > inputRange[inputRange.length - 1]) {
|
|
643
|
+
if (rightMode === "clamp") return toNum(outputRange[outputRange.length - 1]);
|
|
644
|
+
if (rightMode === "identity") return value;
|
|
645
|
+
}
|
|
646
|
+
return result;
|
|
647
|
+
}
|
|
546
648
|
var AnimatedValue = class AnimatedValue {
|
|
547
649
|
_value;
|
|
548
650
|
_listeners = /* @__PURE__ */ new Map();
|
|
549
651
|
_listenerIdCounter = 0;
|
|
652
|
+
_tracking = null;
|
|
550
653
|
constructor(value = 0) {
|
|
551
654
|
this._value = value;
|
|
552
655
|
}
|
|
@@ -569,16 +672,9 @@ var AnimatedValue = class AnimatedValue {
|
|
|
569
672
|
this._listeners.clear();
|
|
570
673
|
}
|
|
571
674
|
interpolate(config) {
|
|
572
|
-
const { inputRange, outputRange } = config || {};
|
|
675
|
+
const { inputRange, outputRange, extrapolate = "extend", extrapolateLeft, extrapolateRight, easing } = config || {};
|
|
573
676
|
if (!inputRange || !outputRange || inputRange.length < 2 || outputRange.length < 2) return new AnimatedValue(this._value);
|
|
574
|
-
|
|
575
|
-
let i = 0;
|
|
576
|
-
for (; i < inputRange.length - 2; i++) if (val <= inputRange[i + 1]) break;
|
|
577
|
-
const inMin = inputRange[i];
|
|
578
|
-
const inMax = inputRange[i + 1];
|
|
579
|
-
const outMin = typeof outputRange[i] === "number" ? outputRange[i] : parseFloat(outputRange[i]) || 0;
|
|
580
|
-
const outMax = typeof outputRange[i + 1] === "number" ? outputRange[i + 1] : parseFloat(outputRange[i + 1]) || 0;
|
|
581
|
-
return new AnimatedValue(outMin + (inMax === inMin ? 0 : (val - inMin) / (inMax - inMin)) * (outMax - outMin));
|
|
677
|
+
return new AnimatedValue(interpolateValue(this._value, inputRange, outputRange, extrapolate, extrapolateLeft, extrapolateRight, easing));
|
|
582
678
|
}
|
|
583
679
|
stopAnimation(callback) {
|
|
584
680
|
callback?.(this._value);
|
|
@@ -586,6 +682,9 @@ var AnimatedValue = class AnimatedValue {
|
|
|
586
682
|
resetAnimation(callback) {
|
|
587
683
|
callback?.(this._value);
|
|
588
684
|
}
|
|
685
|
+
toJSON() {
|
|
686
|
+
return this._value;
|
|
687
|
+
}
|
|
589
688
|
setOffset(_offset) {}
|
|
590
689
|
flattenOffset() {}
|
|
591
690
|
extractOffset() {}
|
|
@@ -636,33 +735,178 @@ var AnimatedValueXY = class {
|
|
|
636
735
|
return [{ translateX: this.x }, { translateY: this.y }];
|
|
637
736
|
}
|
|
638
737
|
};
|
|
738
|
+
const namedColorMap = {
|
|
739
|
+
red: [
|
|
740
|
+
255,
|
|
741
|
+
0,
|
|
742
|
+
0,
|
|
743
|
+
1
|
|
744
|
+
],
|
|
745
|
+
green: [
|
|
746
|
+
0,
|
|
747
|
+
128,
|
|
748
|
+
0,
|
|
749
|
+
1
|
|
750
|
+
],
|
|
751
|
+
blue: [
|
|
752
|
+
0,
|
|
753
|
+
0,
|
|
754
|
+
255,
|
|
755
|
+
1
|
|
756
|
+
],
|
|
757
|
+
white: [
|
|
758
|
+
255,
|
|
759
|
+
255,
|
|
760
|
+
255,
|
|
761
|
+
1
|
|
762
|
+
],
|
|
763
|
+
black: [
|
|
764
|
+
0,
|
|
765
|
+
0,
|
|
766
|
+
0,
|
|
767
|
+
1
|
|
768
|
+
],
|
|
769
|
+
transparent: [
|
|
770
|
+
0,
|
|
771
|
+
0,
|
|
772
|
+
0,
|
|
773
|
+
0
|
|
774
|
+
],
|
|
775
|
+
yellow: [
|
|
776
|
+
255,
|
|
777
|
+
255,
|
|
778
|
+
0,
|
|
779
|
+
1
|
|
780
|
+
],
|
|
781
|
+
cyan: [
|
|
782
|
+
0,
|
|
783
|
+
255,
|
|
784
|
+
255,
|
|
785
|
+
1
|
|
786
|
+
],
|
|
787
|
+
magenta: [
|
|
788
|
+
255,
|
|
789
|
+
0,
|
|
790
|
+
255,
|
|
791
|
+
1
|
|
792
|
+
]
|
|
793
|
+
};
|
|
794
|
+
function parseColorString(color) {
|
|
795
|
+
const rgba = color.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+))?\s*\)$/);
|
|
796
|
+
if (rgba) return [
|
|
797
|
+
parseInt(rgba[1]),
|
|
798
|
+
parseInt(rgba[2]),
|
|
799
|
+
parseInt(rgba[3]),
|
|
800
|
+
rgba[4] != null ? parseFloat(rgba[4]) : 1
|
|
801
|
+
];
|
|
802
|
+
const hex8 = color.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);
|
|
803
|
+
if (hex8) return [
|
|
804
|
+
parseInt(hex8[1], 16),
|
|
805
|
+
parseInt(hex8[2], 16),
|
|
806
|
+
parseInt(hex8[3], 16),
|
|
807
|
+
parseInt(hex8[4], 16) / 255
|
|
808
|
+
];
|
|
809
|
+
const hex6 = color.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);
|
|
810
|
+
if (hex6) return [
|
|
811
|
+
parseInt(hex6[1], 16),
|
|
812
|
+
parseInt(hex6[2], 16),
|
|
813
|
+
parseInt(hex6[3], 16),
|
|
814
|
+
1
|
|
815
|
+
];
|
|
816
|
+
const named = namedColorMap[color.toLowerCase()];
|
|
817
|
+
if (named) return named;
|
|
818
|
+
return [
|
|
819
|
+
0,
|
|
820
|
+
0,
|
|
821
|
+
0,
|
|
822
|
+
1
|
|
823
|
+
];
|
|
824
|
+
}
|
|
639
825
|
var AnimatedColor = class {
|
|
640
826
|
r;
|
|
641
827
|
g;
|
|
642
828
|
b;
|
|
643
829
|
a;
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
830
|
+
_listeners = /* @__PURE__ */ new Map();
|
|
831
|
+
_listenerIdCounter = 0;
|
|
832
|
+
constructor(color) {
|
|
833
|
+
if (typeof color === "string") {
|
|
834
|
+
const [r, g, b, a] = parseColorString(color);
|
|
835
|
+
this.r = new AnimatedValue(r);
|
|
836
|
+
this.g = new AnimatedValue(g);
|
|
837
|
+
this.b = new AnimatedValue(b);
|
|
838
|
+
this.a = new AnimatedValue(a);
|
|
839
|
+
} else if (color && typeof color === "object") if (color.r instanceof AnimatedValue) {
|
|
840
|
+
this.r = color.r;
|
|
841
|
+
this.g = color.g;
|
|
842
|
+
this.b = color.b;
|
|
843
|
+
this.a = color.a;
|
|
844
|
+
} else if (typeof color.r === "number") {
|
|
845
|
+
this.r = new AnimatedValue(color.r);
|
|
846
|
+
this.g = new AnimatedValue(color.g ?? 0);
|
|
847
|
+
this.b = new AnimatedValue(color.b ?? 0);
|
|
848
|
+
this.a = new AnimatedValue(color.a ?? 1);
|
|
849
|
+
} else {
|
|
850
|
+
this.r = new AnimatedValue(0);
|
|
851
|
+
this.g = new AnimatedValue(0);
|
|
852
|
+
this.b = new AnimatedValue(0);
|
|
853
|
+
this.a = new AnimatedValue(1);
|
|
854
|
+
}
|
|
855
|
+
else {
|
|
856
|
+
this.r = new AnimatedValue(0);
|
|
857
|
+
this.g = new AnimatedValue(0);
|
|
858
|
+
this.b = new AnimatedValue(0);
|
|
859
|
+
this.a = new AnimatedValue(1);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
setValue(value) {
|
|
863
|
+
if (typeof value === "string") {
|
|
864
|
+
const [r, g, b, a] = parseColorString(value);
|
|
865
|
+
this.r.setValue(r);
|
|
866
|
+
this.g.setValue(g);
|
|
867
|
+
this.b.setValue(b);
|
|
868
|
+
this.a.setValue(a);
|
|
869
|
+
} else if (value && typeof value === "object" && typeof value.r === "number") {
|
|
870
|
+
this.r.setValue(value.r);
|
|
871
|
+
this.g.setValue(value.g ?? 0);
|
|
872
|
+
this.b.setValue(value.b ?? 0);
|
|
873
|
+
this.a.setValue(value.a ?? 1);
|
|
874
|
+
}
|
|
875
|
+
const colorStr = this.__getValue();
|
|
876
|
+
this._listeners.forEach((fn) => fn({ value: colorStr }));
|
|
877
|
+
}
|
|
878
|
+
__getValue() {
|
|
879
|
+
return `rgba(${Math.round(this.r.getValue())}, ${Math.round(this.g.getValue())}, ${Math.round(this.b.getValue())}, ${this.a.getValue()})`;
|
|
880
|
+
}
|
|
881
|
+
addListener(callback) {
|
|
882
|
+
const id = String(++this._listenerIdCounter);
|
|
883
|
+
this._listeners.set(id, callback);
|
|
884
|
+
return id;
|
|
885
|
+
}
|
|
886
|
+
removeListener(id) {
|
|
887
|
+
this._listeners.delete(id);
|
|
888
|
+
}
|
|
889
|
+
removeAllListeners() {
|
|
890
|
+
this._listeners.clear();
|
|
649
891
|
}
|
|
650
|
-
setValue(_value) {}
|
|
651
892
|
setOffset(_offset) {}
|
|
652
893
|
flattenOffset() {}
|
|
653
894
|
stopAnimation(callback) {
|
|
654
|
-
callback?.(
|
|
895
|
+
callback?.(this.__getValue());
|
|
655
896
|
}
|
|
656
897
|
resetAnimation(callback) {
|
|
657
|
-
callback?.(
|
|
898
|
+
callback?.(this.__getValue());
|
|
658
899
|
}
|
|
659
900
|
};
|
|
660
|
-
function createAnimation() {
|
|
901
|
+
function createAnimation(onStart) {
|
|
661
902
|
return {
|
|
662
903
|
start: vitest.vi.fn((callback) => {
|
|
904
|
+
onStart?.();
|
|
663
905
|
callback?.({ finished: true });
|
|
664
906
|
}),
|
|
665
|
-
stop: vitest.vi.fn()
|
|
907
|
+
stop: vitest.vi.fn((callback) => {
|
|
908
|
+
callback?.({ finished: false });
|
|
909
|
+
}),
|
|
666
910
|
reset: vitest.vi.fn()
|
|
667
911
|
};
|
|
668
912
|
}
|
|
@@ -676,19 +920,188 @@ function createAnimatedWrapper(displayName) {
|
|
|
676
920
|
Component.displayName = displayName;
|
|
677
921
|
return Component;
|
|
678
922
|
}
|
|
923
|
+
function stopTracking(value) {
|
|
924
|
+
if (value._tracking) {
|
|
925
|
+
value._tracking.source.removeListener(value._tracking.listenerId);
|
|
926
|
+
value._tracking = null;
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
function startTracking(value, toValue) {
|
|
930
|
+
stopTracking(value);
|
|
931
|
+
if (toValue instanceof AnimatedValue) {
|
|
932
|
+
value.setValue(toValue.getValue());
|
|
933
|
+
value._tracking = {
|
|
934
|
+
source: toValue,
|
|
935
|
+
listenerId: toValue.addListener(({ value: v }) => {
|
|
936
|
+
value.setValue(v);
|
|
937
|
+
})
|
|
938
|
+
};
|
|
939
|
+
} else value.setValue(toValue);
|
|
940
|
+
}
|
|
679
941
|
function createAnimatedMock() {
|
|
680
942
|
return {
|
|
681
943
|
Value: AnimatedValue,
|
|
682
944
|
ValueXY: AnimatedValueXY,
|
|
683
945
|
Color: AnimatedColor,
|
|
684
|
-
timing: vitest.vi.fn((
|
|
685
|
-
|
|
946
|
+
timing: vitest.vi.fn((value, config) => {
|
|
947
|
+
return createAnimation(() => {
|
|
948
|
+
if (value instanceof AnimatedValue && config?.toValue != null) startTracking(value, config.toValue);
|
|
949
|
+
});
|
|
950
|
+
}),
|
|
951
|
+
spring: vitest.vi.fn((value, config) => {
|
|
952
|
+
return createAnimation(() => {
|
|
953
|
+
if (value instanceof AnimatedValue && config?.toValue != null) startTracking(value, config.toValue);
|
|
954
|
+
});
|
|
955
|
+
}),
|
|
686
956
|
decay: vitest.vi.fn((_value, _config) => createAnimation()),
|
|
687
|
-
sequence: vitest.vi.fn((
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
957
|
+
sequence: vitest.vi.fn((animations) => {
|
|
958
|
+
let current = 0;
|
|
959
|
+
let cb;
|
|
960
|
+
return {
|
|
961
|
+
start: vitest.vi.fn((endCallback) => {
|
|
962
|
+
cb = endCallback;
|
|
963
|
+
if (animations.length === 0) {
|
|
964
|
+
cb?.({ finished: true });
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
if (current >= animations.length) current = 0;
|
|
968
|
+
const runNext = (result) => {
|
|
969
|
+
if (!result.finished) {
|
|
970
|
+
cb?.({ finished: false });
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
current++;
|
|
974
|
+
if (current >= animations.length) {
|
|
975
|
+
cb?.({ finished: true });
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
animations[current]?.start(runNext);
|
|
979
|
+
};
|
|
980
|
+
animations[current]?.start(runNext);
|
|
981
|
+
}),
|
|
982
|
+
stop: vitest.vi.fn(() => {
|
|
983
|
+
if (current < animations.length) animations[current]?.stop?.();
|
|
984
|
+
}),
|
|
985
|
+
reset: vitest.vi.fn(() => {
|
|
986
|
+
current = 0;
|
|
987
|
+
for (const anim of animations) anim?.reset?.();
|
|
988
|
+
})
|
|
989
|
+
};
|
|
990
|
+
}),
|
|
991
|
+
parallel: vitest.vi.fn((animations) => {
|
|
992
|
+
let doneCount = 0;
|
|
993
|
+
let hasBeenStopped = false;
|
|
994
|
+
let cb;
|
|
995
|
+
const validAnims = animations.filter(Boolean);
|
|
996
|
+
return {
|
|
997
|
+
start: vitest.vi.fn((endCallback) => {
|
|
998
|
+
cb = endCallback;
|
|
999
|
+
doneCount = 0;
|
|
1000
|
+
hasBeenStopped = false;
|
|
1001
|
+
if (validAnims.length === 0) {
|
|
1002
|
+
cb?.({ finished: true });
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
validAnims.forEach((anim) => {
|
|
1006
|
+
anim.start((result) => {
|
|
1007
|
+
doneCount++;
|
|
1008
|
+
if (!result.finished && !hasBeenStopped) {
|
|
1009
|
+
hasBeenStopped = true;
|
|
1010
|
+
validAnims.forEach((other) => {
|
|
1011
|
+
if (other !== anim) other.stop?.();
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
if (doneCount >= validAnims.length) cb?.(result);
|
|
1015
|
+
});
|
|
1016
|
+
});
|
|
1017
|
+
}),
|
|
1018
|
+
stop: vitest.vi.fn(() => {
|
|
1019
|
+
hasBeenStopped = true;
|
|
1020
|
+
validAnims.forEach((anim) => {
|
|
1021
|
+
anim.stop?.();
|
|
1022
|
+
});
|
|
1023
|
+
}),
|
|
1024
|
+
reset: vitest.vi.fn(() => {
|
|
1025
|
+
for (const anim of validAnims) anim?.reset?.();
|
|
1026
|
+
})
|
|
1027
|
+
};
|
|
1028
|
+
}),
|
|
1029
|
+
stagger: vitest.vi.fn((_time, animations) => {
|
|
1030
|
+
const validAnims = animations.filter(Boolean);
|
|
1031
|
+
let doneCount = 0;
|
|
1032
|
+
let hasBeenStopped = false;
|
|
1033
|
+
return {
|
|
1034
|
+
start: vitest.vi.fn((endCallback) => {
|
|
1035
|
+
doneCount = 0;
|
|
1036
|
+
hasBeenStopped = false;
|
|
1037
|
+
if (validAnims.length === 0) {
|
|
1038
|
+
endCallback?.({ finished: true });
|
|
1039
|
+
return;
|
|
1040
|
+
}
|
|
1041
|
+
validAnims.forEach((anim) => {
|
|
1042
|
+
anim.start?.((result) => {
|
|
1043
|
+
doneCount++;
|
|
1044
|
+
if (!result.finished && !hasBeenStopped) {
|
|
1045
|
+
hasBeenStopped = true;
|
|
1046
|
+
validAnims.forEach((other) => {
|
|
1047
|
+
if (other !== anim) other.stop?.();
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
if (doneCount >= validAnims.length) endCallback?.(result);
|
|
1051
|
+
});
|
|
1052
|
+
});
|
|
1053
|
+
}),
|
|
1054
|
+
stop: vitest.vi.fn(() => {
|
|
1055
|
+
hasBeenStopped = true;
|
|
1056
|
+
validAnims.forEach((anim) => anim.stop?.());
|
|
1057
|
+
}),
|
|
1058
|
+
reset: vitest.vi.fn(() => {
|
|
1059
|
+
validAnims.forEach((anim) => anim.reset?.());
|
|
1060
|
+
})
|
|
1061
|
+
};
|
|
1062
|
+
}),
|
|
1063
|
+
loop: vitest.vi.fn((animation, config) => {
|
|
1064
|
+
const iterations = config?.iterations;
|
|
1065
|
+
const resetBeforeIteration = config?.resetBeforeIteration !== false;
|
|
1066
|
+
let stopped = false;
|
|
1067
|
+
let startCallback;
|
|
1068
|
+
return {
|
|
1069
|
+
start: vitest.vi.fn((endCallback) => {
|
|
1070
|
+
stopped = false;
|
|
1071
|
+
startCallback = endCallback;
|
|
1072
|
+
if (iterations === 0) {
|
|
1073
|
+
endCallback?.({ finished: true });
|
|
1074
|
+
return;
|
|
1075
|
+
}
|
|
1076
|
+
let iterationCount = 0;
|
|
1077
|
+
const onIteration = (result) => {
|
|
1078
|
+
if (stopped || !result.finished) {
|
|
1079
|
+
startCallback?.(result);
|
|
1080
|
+
return;
|
|
1081
|
+
}
|
|
1082
|
+
iterationCount++;
|
|
1083
|
+
if (iterations != null && iterations > 0 && iterationCount >= iterations) {
|
|
1084
|
+
startCallback?.({ finished: true });
|
|
1085
|
+
return;
|
|
1086
|
+
}
|
|
1087
|
+
if (resetBeforeIteration) animation.reset?.();
|
|
1088
|
+
animation.start(onIteration);
|
|
1089
|
+
};
|
|
1090
|
+
if (resetBeforeIteration) animation.reset?.();
|
|
1091
|
+
animation.start(onIteration);
|
|
1092
|
+
}),
|
|
1093
|
+
stop: vitest.vi.fn(() => {
|
|
1094
|
+
stopped = true;
|
|
1095
|
+
animation.stop?.();
|
|
1096
|
+
}),
|
|
1097
|
+
reset: vitest.vi.fn(() => {
|
|
1098
|
+
animation.reset?.();
|
|
1099
|
+
})
|
|
1100
|
+
};
|
|
1101
|
+
}),
|
|
1102
|
+
delay: vitest.vi.fn((_time) => {
|
|
1103
|
+
return createAnimation();
|
|
1104
|
+
}),
|
|
692
1105
|
add: vitest.vi.fn((a, b) => {
|
|
693
1106
|
return new AnimatedValue((a instanceof AnimatedValue ? a.getValue() : typeof a === "number" ? a : 0) + (b instanceof AnimatedValue ? b.getValue() : typeof b === "number" ? b : 0));
|
|
694
1107
|
}),
|
|
@@ -707,11 +1120,44 @@ function createAnimatedMock() {
|
|
|
707
1120
|
return new AnimatedValue(((a instanceof AnimatedValue ? a.getValue() : typeof a === "number" ? a : 0) % modulus + modulus) % modulus);
|
|
708
1121
|
}),
|
|
709
1122
|
diffClamp: vitest.vi.fn((a, min, max) => {
|
|
710
|
-
const
|
|
711
|
-
|
|
1123
|
+
const result = new AnimatedValue(Math.min(Math.max(a instanceof AnimatedValue ? a.getValue() : 0, min), max));
|
|
1124
|
+
if (a instanceof AnimatedValue) {
|
|
1125
|
+
let lastInput = a.getValue();
|
|
1126
|
+
let current = result.getValue();
|
|
1127
|
+
a.addListener(({ value }) => {
|
|
1128
|
+
const diff = value - lastInput;
|
|
1129
|
+
lastInput = value;
|
|
1130
|
+
current = Math.min(Math.max(current + diff, min), max);
|
|
1131
|
+
result.setValue(current);
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
1134
|
+
return result;
|
|
1135
|
+
}),
|
|
1136
|
+
event: vitest.vi.fn((argMapping, config) => {
|
|
1137
|
+
const handler = vitest.vi.fn((...args) => {
|
|
1138
|
+
argMapping.forEach((mapping, index) => {
|
|
1139
|
+
if (mapping && args[index]) traverseMapping(mapping, args[index]);
|
|
1140
|
+
});
|
|
1141
|
+
config?.listener?.(...args);
|
|
1142
|
+
});
|
|
1143
|
+
function traverseMapping(mapping, value) {
|
|
1144
|
+
if (mapping instanceof AnimatedValue && typeof value === "number") {
|
|
1145
|
+
mapping.setValue(value);
|
|
1146
|
+
return;
|
|
1147
|
+
}
|
|
1148
|
+
if (typeof mapping === "object" && mapping !== null && typeof value === "object" && value !== null) {
|
|
1149
|
+
for (const key of Object.keys(mapping)) if (key in value) traverseMapping(mapping[key], value[key]);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
return handler;
|
|
1153
|
+
}),
|
|
1154
|
+
forkEvent: vitest.vi.fn((handler, listener) => {
|
|
1155
|
+
return (...args) => {
|
|
1156
|
+
if (typeof handler === "function") handler(...args);
|
|
1157
|
+
else if (handler && handler.__isNative) {}
|
|
1158
|
+
listener(...args);
|
|
1159
|
+
};
|
|
712
1160
|
}),
|
|
713
|
-
event: vitest.vi.fn((_argMapping, _config) => vitest.vi.fn()),
|
|
714
|
-
forkEvent: vitest.vi.fn(),
|
|
715
1161
|
unforkEvent: vitest.vi.fn(),
|
|
716
1162
|
createAnimatedComponent: vitest.vi.fn((component) => {
|
|
717
1163
|
const Wrapper = react.default.forwardRef((props, ref) => {
|
|
@@ -935,12 +1381,17 @@ function createAppearanceMock() {
|
|
|
935
1381
|
}
|
|
936
1382
|
//#endregion
|
|
937
1383
|
//#region src/mocks/apis/PixelRatio.ts
|
|
938
|
-
function createPixelRatioMock() {
|
|
1384
|
+
function createPixelRatioMock(getDimensions) {
|
|
1385
|
+
const getScale = () => getDimensions ? getDimensions().scale : 3;
|
|
1386
|
+
const getFontScale = () => getDimensions ? getDimensions().fontScale : 1;
|
|
939
1387
|
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) =>
|
|
1388
|
+
get: vitest.vi.fn(() => getScale()),
|
|
1389
|
+
getFontScale: vitest.vi.fn(() => getFontScale()),
|
|
1390
|
+
getPixelSizeForLayoutSize: vitest.vi.fn((layoutSize) => Math.round(layoutSize * getScale())),
|
|
1391
|
+
roundToNearestPixel: vitest.vi.fn((layoutSize) => {
|
|
1392
|
+
const ratio = getScale();
|
|
1393
|
+
return Math.round(layoutSize * ratio) / ratio;
|
|
1394
|
+
})
|
|
944
1395
|
};
|
|
945
1396
|
}
|
|
946
1397
|
//#endregion
|
|
@@ -1126,10 +1577,79 @@ function createLogBoxMock() {
|
|
|
1126
1577
|
}
|
|
1127
1578
|
//#endregion
|
|
1128
1579
|
//#region src/mocks/apis/Easing.ts
|
|
1580
|
+
const NEWTON_ITERATIONS = 4;
|
|
1581
|
+
const NEWTON_MIN_SLOPE = .001;
|
|
1582
|
+
const SUBDIVISION_PRECISION = 1e-7;
|
|
1583
|
+
const SUBDIVISION_MAX_ITERATIONS = 10;
|
|
1584
|
+
const kSplineTableSize = 11;
|
|
1585
|
+
const kSampleStepSize = 1 / (kSplineTableSize - 1);
|
|
1586
|
+
function A(aA1, aA2) {
|
|
1587
|
+
return 1 - 3 * aA2 + 3 * aA1;
|
|
1588
|
+
}
|
|
1589
|
+
function B(aA1, aA2) {
|
|
1590
|
+
return 3 * aA2 - 6 * aA1;
|
|
1591
|
+
}
|
|
1592
|
+
function C(aA1) {
|
|
1593
|
+
return 3 * aA1;
|
|
1594
|
+
}
|
|
1595
|
+
function calcBezier(aT, aA1, aA2) {
|
|
1596
|
+
return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT;
|
|
1597
|
+
}
|
|
1598
|
+
function getSlope(aT, aA1, aA2) {
|
|
1599
|
+
return 3 * A(aA1, aA2) * aT * aT + 2 * B(aA1, aA2) * aT + C(aA1);
|
|
1600
|
+
}
|
|
1601
|
+
function binarySubdivide(aX, aA, aB, mX1, mX2) {
|
|
1602
|
+
let currentX;
|
|
1603
|
+
let currentT;
|
|
1604
|
+
let i = 0;
|
|
1605
|
+
let low = aA;
|
|
1606
|
+
let high = aB;
|
|
1607
|
+
do {
|
|
1608
|
+
currentT = low + (high - low) / 2;
|
|
1609
|
+
currentX = calcBezier(currentT, mX1, mX2) - aX;
|
|
1610
|
+
if (currentX > 0) high = currentT;
|
|
1611
|
+
else low = currentT;
|
|
1612
|
+
} while (Math.abs(currentX) > SUBDIVISION_PRECISION && ++i < SUBDIVISION_MAX_ITERATIONS);
|
|
1613
|
+
return currentT;
|
|
1614
|
+
}
|
|
1615
|
+
function newtonRaphsonIterate(aX, aGuessT, mX1, mX2) {
|
|
1616
|
+
let guessT = aGuessT;
|
|
1617
|
+
for (let i = 0; i < NEWTON_ITERATIONS; ++i) {
|
|
1618
|
+
const currentSlope = getSlope(guessT, mX1, mX2);
|
|
1619
|
+
if (currentSlope === 0) return guessT;
|
|
1620
|
+
const currentX = calcBezier(guessT, mX1, mX2) - aX;
|
|
1621
|
+
guessT -= currentX / currentSlope;
|
|
1622
|
+
}
|
|
1623
|
+
return guessT;
|
|
1624
|
+
}
|
|
1625
|
+
function bezier(mX1, mY1, mX2, mY2) {
|
|
1626
|
+
if (!(mX1 >= 0 && mX1 <= 1 && mX2 >= 0 && mX2 <= 1)) throw new Error("bezier x values must be in [0, 1] range");
|
|
1627
|
+
if (mX1 === mY1 && mX2 === mY2) return (x) => x;
|
|
1628
|
+
const sampleValues = new Float32Array(kSplineTableSize);
|
|
1629
|
+
for (let i = 0; i < kSplineTableSize; ++i) sampleValues[i] = calcBezier(i * kSampleStepSize, mX1, mX2);
|
|
1630
|
+
function getTForX(aX) {
|
|
1631
|
+
let intervalStart = 0;
|
|
1632
|
+
let currentSample = 1;
|
|
1633
|
+
const lastSample = kSplineTableSize - 1;
|
|
1634
|
+
for (; currentSample !== lastSample && sampleValues[currentSample] <= aX; ++currentSample) intervalStart += kSampleStepSize;
|
|
1635
|
+
--currentSample;
|
|
1636
|
+
const dist = (aX - sampleValues[currentSample]) / (sampleValues[currentSample + 1] - sampleValues[currentSample]);
|
|
1637
|
+
const guessForT = intervalStart + dist * kSampleStepSize;
|
|
1638
|
+
const initialSlope = getSlope(guessForT, mX1, mX2);
|
|
1639
|
+
if (initialSlope >= NEWTON_MIN_SLOPE) return newtonRaphsonIterate(aX, guessForT, mX1, mX2);
|
|
1640
|
+
else if (initialSlope === 0) return guessForT;
|
|
1641
|
+
else return binarySubdivide(aX, intervalStart, intervalStart + kSampleStepSize, mX1, mX2);
|
|
1642
|
+
}
|
|
1643
|
+
return function(x) {
|
|
1644
|
+
if (x === 0 || x === 1) return x;
|
|
1645
|
+
return calcBezier(getTForX(x), mY1, mY2);
|
|
1646
|
+
};
|
|
1647
|
+
}
|
|
1129
1648
|
function createEasingMock() {
|
|
1649
|
+
const easeFn = bezier(.42, 0, 1, 1);
|
|
1130
1650
|
return {
|
|
1131
1651
|
linear: vitest.vi.fn((t) => t),
|
|
1132
|
-
ease: vitest.vi.fn((t) =>
|
|
1652
|
+
ease: vitest.vi.fn((t) => easeFn(t)),
|
|
1133
1653
|
quad: vitest.vi.fn((t) => t * t),
|
|
1134
1654
|
cubic: vitest.vi.fn((t) => t * t * t),
|
|
1135
1655
|
poly: vitest.vi.fn((n) => (t) => Math.pow(t, n)),
|
|
@@ -1144,21 +1664,17 @@ function createEasingMock() {
|
|
|
1144
1664
|
bounce: vitest.vi.fn((t) => {
|
|
1145
1665
|
if (t < 1 / 2.75) return 7.5625 * t * t;
|
|
1146
1666
|
if (t < 2 / 2.75) {
|
|
1147
|
-
t
|
|
1148
|
-
return 7.5625 *
|
|
1667
|
+
const t2 = t - 1.5 / 2.75;
|
|
1668
|
+
return 7.5625 * t2 * t2 + .75;
|
|
1149
1669
|
}
|
|
1150
1670
|
if (t < 2.5 / 2.75) {
|
|
1151
|
-
t
|
|
1152
|
-
return 7.5625 *
|
|
1671
|
+
const t2 = t - 2.25 / 2.75;
|
|
1672
|
+
return 7.5625 * t2 * t2 + .9375;
|
|
1153
1673
|
}
|
|
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;
|
|
1674
|
+
const t2 = t - 2.625 / 2.75;
|
|
1675
|
+
return 7.5625 * t2 * t2 + .984375;
|
|
1161
1676
|
}),
|
|
1677
|
+
bezier: vitest.vi.fn((x1, y1, x2, y2) => bezier(x1, y1, x2, y2)),
|
|
1162
1678
|
in: vitest.vi.fn((easing) => easing),
|
|
1163
1679
|
out: vitest.vi.fn((easing) => (t) => 1 - easing(1 - t)),
|
|
1164
1680
|
inOut: vitest.vi.fn((easing) => (t) => {
|
|
@@ -1289,27 +1805,42 @@ function createUseWindowDimensionsMock() {
|
|
|
1289
1805
|
//#endregion
|
|
1290
1806
|
//#region src/mocks/native/NativeModules.ts
|
|
1291
1807
|
/**
|
|
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.
|
|
1808
|
+
* Creates a callable proxy — used for module methods.
|
|
1809
|
+
* Calling it returns undefined; accessing properties returns another
|
|
1810
|
+
* callable proxy (supporting arbitrary depth like Foo.bar.baz()).
|
|
1298
1811
|
*/
|
|
1299
|
-
function
|
|
1812
|
+
function createCallableProxy() {
|
|
1300
1813
|
const cache = /* @__PURE__ */ new Map();
|
|
1301
1814
|
const noop = () => {};
|
|
1302
1815
|
return new Proxy(noop, {
|
|
1303
1816
|
get(_target, prop) {
|
|
1304
1817
|
if (typeof prop === "symbol") return void 0;
|
|
1305
1818
|
if (cache.has(prop)) return cache.get(prop);
|
|
1306
|
-
const child =
|
|
1819
|
+
const child = createCallableProxy();
|
|
1307
1820
|
cache.set(prop, child);
|
|
1308
1821
|
return child;
|
|
1309
1822
|
},
|
|
1310
1823
|
apply() {}
|
|
1311
1824
|
});
|
|
1312
1825
|
}
|
|
1826
|
+
/**
|
|
1827
|
+
* Creates an object proxy for a native module.
|
|
1828
|
+
* typeof returns "object" (matching real RN), property access returns
|
|
1829
|
+
* callable proxies so NativeModules.Foo.bar() works without throwing.
|
|
1830
|
+
*
|
|
1831
|
+
* Property accesses are cached so that NativeModules.Foo === NativeModules.Foo
|
|
1832
|
+
* holds true, matching real React Native identity semantics.
|
|
1833
|
+
*/
|
|
1834
|
+
function createModuleProxy$1() {
|
|
1835
|
+
const cache = /* @__PURE__ */ new Map();
|
|
1836
|
+
return new Proxy({}, { get(_target, prop) {
|
|
1837
|
+
if (typeof prop === "symbol") return void 0;
|
|
1838
|
+
if (cache.has(prop)) return cache.get(prop);
|
|
1839
|
+
const child = createCallableProxy();
|
|
1840
|
+
cache.set(prop, child);
|
|
1841
|
+
return child;
|
|
1842
|
+
} });
|
|
1843
|
+
}
|
|
1313
1844
|
function createNativeModulesMock() {
|
|
1314
1845
|
const cache = /* @__PURE__ */ new Map();
|
|
1315
1846
|
return new Proxy({}, { get(_target, prop) {
|
|
@@ -1441,10 +1972,96 @@ function createAppRegistryMock() {
|
|
|
1441
1972
|
cancelHeadlessTask: vitest.vi.fn()
|
|
1442
1973
|
};
|
|
1443
1974
|
}
|
|
1975
|
+
function hslToRgb(h, s, l) {
|
|
1976
|
+
const sNorm = s / 100;
|
|
1977
|
+
const lNorm = l / 100;
|
|
1978
|
+
const c = (1 - Math.abs(2 * lNorm - 1)) * sNorm;
|
|
1979
|
+
const hPrime = h / 60;
|
|
1980
|
+
const x = c * (1 - Math.abs(hPrime % 2 - 1));
|
|
1981
|
+
let r1 = 0, g1 = 0, b1 = 0;
|
|
1982
|
+
if (hPrime < 1) {
|
|
1983
|
+
r1 = c;
|
|
1984
|
+
g1 = x;
|
|
1985
|
+
} else if (hPrime < 2) {
|
|
1986
|
+
r1 = x;
|
|
1987
|
+
g1 = c;
|
|
1988
|
+
} else if (hPrime < 3) {
|
|
1989
|
+
g1 = c;
|
|
1990
|
+
b1 = x;
|
|
1991
|
+
} else if (hPrime < 4) {
|
|
1992
|
+
g1 = x;
|
|
1993
|
+
b1 = c;
|
|
1994
|
+
} else if (hPrime < 5) {
|
|
1995
|
+
r1 = x;
|
|
1996
|
+
b1 = c;
|
|
1997
|
+
} else {
|
|
1998
|
+
r1 = c;
|
|
1999
|
+
b1 = x;
|
|
2000
|
+
}
|
|
2001
|
+
const m = lNorm - c / 2;
|
|
2002
|
+
return [
|
|
2003
|
+
Math.round((r1 + m) * 255),
|
|
2004
|
+
Math.round((g1 + m) * 255),
|
|
2005
|
+
Math.round((b1 + m) * 255)
|
|
2006
|
+
];
|
|
2007
|
+
}
|
|
1444
2008
|
function createProcessColorMock() {
|
|
2009
|
+
const namedColors = {
|
|
2010
|
+
red: 4294901760,
|
|
2011
|
+
green: 4278222848,
|
|
2012
|
+
blue: 4278190335,
|
|
2013
|
+
white: 4294967295,
|
|
2014
|
+
black: 4278190080,
|
|
2015
|
+
transparent: 0,
|
|
2016
|
+
yellow: 4294967040,
|
|
2017
|
+
cyan: 4278255615,
|
|
2018
|
+
magenta: 4294902015,
|
|
2019
|
+
orange: 4294944e3,
|
|
2020
|
+
purple: 4286578816,
|
|
2021
|
+
gray: 4286611584,
|
|
2022
|
+
grey: 4286611584
|
|
2023
|
+
};
|
|
1445
2024
|
return vitest.vi.fn((color) => {
|
|
1446
2025
|
if (color == null) return null;
|
|
1447
2026
|
if (typeof color === "number") return color;
|
|
2027
|
+
if (typeof color === "string") {
|
|
2028
|
+
const lower = color.toLowerCase().trim();
|
|
2029
|
+
if (namedColors[lower] != null) return namedColors[lower];
|
|
2030
|
+
const hex6 = lower.match(/^#([0-9a-f]{6})$/);
|
|
2031
|
+
if (hex6) return 4278190080 + parseInt(hex6[1], 16) >>> 0;
|
|
2032
|
+
const hex3 = lower.match(/^#([0-9a-f]{3})$/);
|
|
2033
|
+
if (hex3) {
|
|
2034
|
+
const [r, g, b] = hex3[1].split("");
|
|
2035
|
+
return 4278190080 + parseInt(`${r}${r}${g}${g}${b}${b}`, 16) >>> 0;
|
|
2036
|
+
}
|
|
2037
|
+
const hex8 = lower.match(/^#([0-9a-f]{8})$/);
|
|
2038
|
+
if (hex8) {
|
|
2039
|
+
const rgb = parseInt(hex8[1].slice(0, 6), 16);
|
|
2040
|
+
return (parseInt(hex8[1].slice(6, 8), 16) << 24) + rgb >>> 0;
|
|
2041
|
+
}
|
|
2042
|
+
const rgbMatch = lower.match(/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/);
|
|
2043
|
+
if (rgbMatch) {
|
|
2044
|
+
const [, r, g, b] = rgbMatch;
|
|
2045
|
+
return (255 << 24) + (parseInt(r) << 16) + (parseInt(g) << 8) + parseInt(b) >>> 0;
|
|
2046
|
+
}
|
|
2047
|
+
const rgbaMatch = lower.match(/^rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*([\d.]+)\s*\)$/);
|
|
2048
|
+
if (rgbaMatch) {
|
|
2049
|
+
const [, r, g, b, a] = rgbaMatch;
|
|
2050
|
+
return (Math.round(parseFloat(a) * 255) << 24) + (parseInt(r) << 16) + (parseInt(g) << 8) + parseInt(b) >>> 0;
|
|
2051
|
+
}
|
|
2052
|
+
const hslMatch = lower.match(/^hsl\(\s*([\d.]+)\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%\s*\)$/);
|
|
2053
|
+
if (hslMatch) {
|
|
2054
|
+
const [, h, s, l] = hslMatch;
|
|
2055
|
+
const [r, g, b] = hslToRgb(parseFloat(h), parseFloat(s), parseFloat(l));
|
|
2056
|
+
return (255 << 24) + (r << 16) + (g << 8) + b >>> 0;
|
|
2057
|
+
}
|
|
2058
|
+
const hslaMatch = lower.match(/^hsla\(\s*([\d.]+)\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%\s*,\s*([\d.]+)\s*\)$/);
|
|
2059
|
+
if (hslaMatch) {
|
|
2060
|
+
const [, h, s, l, a] = hslaMatch;
|
|
2061
|
+
const [r, g, b] = hslToRgb(parseFloat(h), parseFloat(s), parseFloat(l));
|
|
2062
|
+
return (Math.round(parseFloat(a) * 255) << 24) + (r << 16) + (g << 8) + b >>> 0;
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
1448
2065
|
return 4278190080;
|
|
1449
2066
|
});
|
|
1450
2067
|
}
|
|
@@ -1556,7 +2173,7 @@ function buildReactNativeMock(platform = "ios") {
|
|
|
1556
2173
|
Vibration: createVibrationMock(),
|
|
1557
2174
|
PermissionsAndroid: createPermissionsAndroidMock(),
|
|
1558
2175
|
Appearance: createAppearanceMock(),
|
|
1559
|
-
PixelRatio:
|
|
2176
|
+
PixelRatio: void 0,
|
|
1560
2177
|
LayoutAnimation: createLayoutAnimationMock(),
|
|
1561
2178
|
Clipboard: createClipboardMock(),
|
|
1562
2179
|
Share: createShareMock(),
|
|
@@ -1646,6 +2263,17 @@ function buildReactNativeMock(platform = "ios") {
|
|
|
1646
2263
|
usePressability: vitest.vi.fn(() => ({})),
|
|
1647
2264
|
default: void 0
|
|
1648
2265
|
};
|
|
2266
|
+
mock.PixelRatio = createPixelRatioMock(() => mock.Dimensions.get("window"));
|
|
2267
|
+
const origSetColorScheme = mock.Appearance.setColorScheme;
|
|
2268
|
+
mock.Appearance.setColorScheme = vitest.vi.fn((scheme) => {
|
|
2269
|
+
origSetColorScheme(scheme);
|
|
2270
|
+
mock.useColorScheme._setScheme(scheme);
|
|
2271
|
+
});
|
|
2272
|
+
const origDimensionsSet = mock.Dimensions.set;
|
|
2273
|
+
mock.Dimensions.set = vitest.vi.fn((dims) => {
|
|
2274
|
+
origDimensionsSet(dims);
|
|
2275
|
+
if (dims.window) mock.useWindowDimensions._setDimensions(dims.window);
|
|
2276
|
+
});
|
|
1649
2277
|
mock.default = mock;
|
|
1650
2278
|
return mock;
|
|
1651
2279
|
}
|
|
@@ -1669,6 +2297,7 @@ function buildReactNativeMock(platform = "ios") {
|
|
|
1669
2297
|
const VIRTUAL_RN_PATH = "\0vitest-native:react-native";
|
|
1670
2298
|
let originalResolveFilename = null;
|
|
1671
2299
|
let installed = false;
|
|
2300
|
+
const presetRedirects = /* @__PURE__ */ new Map();
|
|
1672
2301
|
function installCjsBridge(mockObject) {
|
|
1673
2302
|
if (installed) return;
|
|
1674
2303
|
try {
|
|
@@ -1681,8 +2310,15 @@ function installCjsBridge(mockObject) {
|
|
|
1681
2310
|
Module._cache[VIRTUAL_RN_PATH] = syntheticModule;
|
|
1682
2311
|
originalResolveFilename = Module._resolveFilename;
|
|
1683
2312
|
Module._resolveFilename = function(request, parent, isMain, options) {
|
|
1684
|
-
if (request === "react-native") return VIRTUAL_RN_PATH;
|
|
1685
|
-
|
|
2313
|
+
if (request === "react-native" || request.startsWith("react-native/")) return VIRTUAL_RN_PATH;
|
|
2314
|
+
const exactMatch = presetRedirects.get(request);
|
|
2315
|
+
if (exactMatch) return exactMatch;
|
|
2316
|
+
const slashIdx = request.indexOf("/", request.startsWith("@") ? request.indexOf("/") + 1 : 0);
|
|
2317
|
+
if (slashIdx !== -1) {
|
|
2318
|
+
const pkg = request.slice(0, slashIdx);
|
|
2319
|
+
const subpathMatch = presetRedirects.get(pkg);
|
|
2320
|
+
if (subpathMatch) return subpathMatch;
|
|
2321
|
+
}
|
|
1686
2322
|
return originalResolveFilename.call(this, request, parent, isMain, options);
|
|
1687
2323
|
};
|
|
1688
2324
|
installed = true;
|
|
@@ -1699,6 +2335,8 @@ function uninstallCjsBridge() {
|
|
|
1699
2335
|
originalResolveFilename = null;
|
|
1700
2336
|
}
|
|
1701
2337
|
delete Module._cache[VIRTUAL_RN_PATH];
|
|
2338
|
+
for (const virtualPath of presetRedirects.values()) delete Module._cache[virtualPath];
|
|
2339
|
+
presetRedirects.clear();
|
|
1702
2340
|
installed = false;
|
|
1703
2341
|
} catch (e) {
|
|
1704
2342
|
if (process.env.VITEST_NATIVE_DIAGNOSTICS === "true") console.warn(`[vitest-native] CJS bridge uninstall failed: ${e?.message}`);
|
|
@@ -1714,11 +2352,7 @@ function installPresetCjsBridge(moduleName, mockObject) {
|
|
|
1714
2352
|
syntheticModule.loaded = true;
|
|
1715
2353
|
syntheticModule.exports = mockObject;
|
|
1716
2354
|
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
|
-
};
|
|
2355
|
+
presetRedirects.set(moduleName, virtualPath);
|
|
1722
2356
|
} catch (e) {
|
|
1723
2357
|
if (process.env.VITEST_NATIVE_DIAGNOSTICS === "true") console.warn(`[vitest-native] Preset CJS bridge for ${moduleName} failed: ${e?.message}`);
|
|
1724
2358
|
}
|
|
@@ -1803,9 +2437,9 @@ function loadPresetFactory(exportName) {
|
|
|
1803
2437
|
return typeof factory === "function" ? factory : null;
|
|
1804
2438
|
}
|
|
1805
2439
|
function autoDetectPresets() {
|
|
1806
|
-
const detected = [];
|
|
1807
2440
|
const projectRoot = process.env.VITEST_NATIVE_PROJECT_ROOT || process.cwd();
|
|
1808
2441
|
const projectReq = (0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json"));
|
|
2442
|
+
const detected = [];
|
|
1809
2443
|
for (const [pkgName, exportName] of Object.entries(require_preset_map.AUTO_DETECT_PRESETS)) try {
|
|
1810
2444
|
projectReq.resolve(pkgName);
|
|
1811
2445
|
const factory = loadPresetFactory(exportName);
|
|
@@ -1837,8 +2471,8 @@ try {
|
|
|
1837
2471
|
if (rntl?.configure) rntl.configure({ hostComponentNames: {
|
|
1838
2472
|
text: "Text",
|
|
1839
2473
|
textInput: "TextInput",
|
|
1840
|
-
switch: "
|
|
1841
|
-
scrollView: "
|
|
2474
|
+
switch: "RCTSwitch",
|
|
2475
|
+
scrollView: "RCTScrollView",
|
|
1842
2476
|
modal: "Modal",
|
|
1843
2477
|
image: "Image",
|
|
1844
2478
|
button: "Button",
|