vitest-native 0.7.0 → 0.9.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 +373 -0
- package/README.md +32 -14
- package/dist/cli.cjs +506 -0
- package/dist/cli.d.cts +4 -0
- package/dist/cli.d.mts +4 -0
- package/dist/cli.mjs +502 -0
- package/dist/ecosystem-BIWaVwYL.cjs +294 -0
- package/dist/ecosystem-EFtHnKS9.mjs +256 -0
- package/dist/{index-DbfKy_bm.d.mts → index-CEgRDdhd.d.cts} +22 -3
- package/dist/{index-BgzErxHj.d.cts → index-j-0tueBr.d.mts} +20 -5
- package/dist/index.cjs +345 -200
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +341 -196
- package/dist/jest-compat/setup.mjs +65 -1
- package/dist/native/boundary.mjs +108 -30
- package/dist/native/compile-cache.mjs +45 -0
- package/dist/native/explain.mjs +69 -0
- package/dist/native/hooks.mjs +77 -4
- package/dist/native/loader.mjs +36 -7
- package/dist/native/match.mjs +107 -8
- package/dist/native/registry.mjs +471 -0
- package/dist/native/reset.mjs +18 -12
- package/dist/native/runner.mjs +13 -7
- package/dist/native/setup.mjs +44 -0
- package/dist/native/transform.mjs +88 -19
- package/dist/native/worker.mjs +13 -0
- package/dist/pool-CJmjRgsB.mjs +117 -0
- package/dist/pool-DzsEo9vv.cjs +121 -0
- package/dist/{preset-map-CNBOeqAu.cjs → preset-map-CG4FMtsl.cjs} +5 -1
- package/dist/{preset-map-C1UwEald.mjs → preset-map-CNsZKfvu.mjs} +5 -1
- package/dist/{presets-BTG8fiNF.cjs → presets-C4faM3ds.cjs} +838 -2
- package/dist/{presets-BFuAMwYQ.mjs → presets-CuDrNA_W.mjs} +761 -3
- 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 +327 -347
- package/dist/setup.mjs +318 -338
- package/docs/hot-default-design.md +129 -0
- package/docs/migrating-from-jest.md +43 -7
- package/docs/migrating-from-vitest-react-native.md +1 -1
- package/docs/release-readiness.md +2 -1
- package/package.json +23 -7
- package/dist/pool-B7LBfcMQ.mjs +0 -67
- package/dist/pool-p54QV1dQ.cjs +0 -69
package/dist/setup.mjs
CHANGED
|
@@ -1,24 +1,12 @@
|
|
|
1
1
|
import { n as __require } from "./chunk-CjcI7cDX.mjs";
|
|
2
|
-
import { t as AUTO_DETECT_PRESETS } from "./preset-map-
|
|
3
|
-
import {
|
|
2
|
+
import { t as AUTO_DETECT_PRESETS } from "./preset-map-CNsZKfvu.mjs";
|
|
3
|
+
import { D as buildPressableHostProps, E as createPressableMock, c as createTouchableOpacityMock, d as createFlatListMock, f as createScrollViewMock, i as createViewMock, l as createVirtualizedListMock, o as createTouchableWithoutFeedbackMock, p as createTextInputMock, s as createTouchableHighlightMock, t as presets_exports, u as createSectionListMock } from "./presets-CuDrNA_W.mjs";
|
|
4
4
|
import { serializer } from "./serializer.mjs";
|
|
5
5
|
import { animatedMatchers } from "./matchers.mjs";
|
|
6
6
|
import { createRequire } from "node:module";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { afterAll, expect, vi } from "vitest";
|
|
9
9
|
import React from "react";
|
|
10
|
-
//#region src/mocks/components/View.ts
|
|
11
|
-
function createViewMock() {
|
|
12
|
-
const View = React.forwardRef((props, ref) => {
|
|
13
|
-
return React.createElement("View", {
|
|
14
|
-
...props,
|
|
15
|
-
ref
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
View.displayName = "View";
|
|
19
|
-
return View;
|
|
20
|
-
}
|
|
21
|
-
//#endregion
|
|
22
10
|
//#region src/mocks/components/Text.ts
|
|
23
11
|
function createTextMock() {
|
|
24
12
|
const Text = React.forwardRef((props, ref) => {
|
|
@@ -66,141 +54,6 @@ function createImageMock() {
|
|
|
66
54
|
return Image;
|
|
67
55
|
}
|
|
68
56
|
//#endregion
|
|
69
|
-
//#region src/mocks/components/TextInput.ts
|
|
70
|
-
function createTextInputMock() {
|
|
71
|
-
const TextInput = React.forwardRef((props, ref) => {
|
|
72
|
-
const instanceMethods = {
|
|
73
|
-
focus: vi.fn(),
|
|
74
|
-
blur: vi.fn(),
|
|
75
|
-
clear: vi.fn(),
|
|
76
|
-
isFocused: vi.fn(() => false),
|
|
77
|
-
setNativeProps: vi.fn()
|
|
78
|
-
};
|
|
79
|
-
React.useImperativeHandle(ref, () => instanceMethods);
|
|
80
|
-
return React.createElement("TextInput", {
|
|
81
|
-
...props,
|
|
82
|
-
ref
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
TextInput.displayName = "TextInput";
|
|
86
|
-
return TextInput;
|
|
87
|
-
}
|
|
88
|
-
//#endregion
|
|
89
|
-
//#region src/mocks/components/ScrollView.ts
|
|
90
|
-
function createScrollViewMock() {
|
|
91
|
-
const ScrollView = React.forwardRef((props, ref) => {
|
|
92
|
-
const { children, refreshControl, ...rest } = props;
|
|
93
|
-
const instanceMethods = {
|
|
94
|
-
scrollTo: vi.fn(),
|
|
95
|
-
scrollToEnd: vi.fn(),
|
|
96
|
-
flashScrollIndicators: vi.fn(),
|
|
97
|
-
getScrollResponder: vi.fn(() => ({})),
|
|
98
|
-
getInnerViewNode: vi.fn(),
|
|
99
|
-
setNativeProps: vi.fn()
|
|
100
|
-
};
|
|
101
|
-
React.useImperativeHandle(ref, () => instanceMethods);
|
|
102
|
-
return React.createElement("RCTScrollView", {
|
|
103
|
-
...rest,
|
|
104
|
-
ref
|
|
105
|
-
}, refreshControl, React.createElement("View", null, children));
|
|
106
|
-
});
|
|
107
|
-
ScrollView.displayName = "ScrollView";
|
|
108
|
-
return ScrollView;
|
|
109
|
-
}
|
|
110
|
-
//#endregion
|
|
111
|
-
//#region src/mocks/components/FlatList.ts
|
|
112
|
-
function createFlatListMock() {
|
|
113
|
-
const FlatList = React.forwardRef((props, ref) => {
|
|
114
|
-
const { data, renderItem, keyExtractor, ListHeaderComponent, ListFooterComponent, ListEmptyComponent, ItemSeparatorComponent, refreshControl, ...rest } = props;
|
|
115
|
-
const instanceMethods = {
|
|
116
|
-
scrollToEnd: vi.fn(),
|
|
117
|
-
scrollToIndex: vi.fn(),
|
|
118
|
-
scrollToItem: vi.fn(),
|
|
119
|
-
scrollToOffset: vi.fn(),
|
|
120
|
-
recordInteraction: vi.fn(),
|
|
121
|
-
flashScrollIndicators: vi.fn(),
|
|
122
|
-
getScrollResponder: vi.fn(() => ({})),
|
|
123
|
-
getNativeScrollRef: vi.fn(),
|
|
124
|
-
getScrollableNode: vi.fn(),
|
|
125
|
-
setNativeProps: vi.fn()
|
|
126
|
-
};
|
|
127
|
-
React.useImperativeHandle(ref, () => instanceMethods);
|
|
128
|
-
const children = [];
|
|
129
|
-
if (ListHeaderComponent) children.push(React.createElement("FlatList-Header", { key: "__header" }, typeof ListHeaderComponent === "function" ? React.createElement(ListHeaderComponent) : ListHeaderComponent));
|
|
130
|
-
if (data && data.length > 0) data.forEach((item, index) => {
|
|
131
|
-
const key = keyExtractor ? keyExtractor(item, index) : String(index);
|
|
132
|
-
children.push(React.createElement(React.Fragment, { key }, renderItem({
|
|
133
|
-
item,
|
|
134
|
-
index,
|
|
135
|
-
separators: {
|
|
136
|
-
highlight: vi.fn(),
|
|
137
|
-
unhighlight: vi.fn(),
|
|
138
|
-
updateProps: vi.fn()
|
|
139
|
-
}
|
|
140
|
-
})));
|
|
141
|
-
if (ItemSeparatorComponent && index < data.length - 1) children.push(React.createElement(React.Fragment, { key: `separator-${index}` }, typeof ItemSeparatorComponent === "function" ? React.createElement(ItemSeparatorComponent) : ItemSeparatorComponent));
|
|
142
|
-
});
|
|
143
|
-
else if (ListEmptyComponent) children.push(React.createElement("FlatList-Empty", { key: "__empty" }, typeof ListEmptyComponent === "function" ? React.createElement(ListEmptyComponent) : ListEmptyComponent));
|
|
144
|
-
if (ListFooterComponent) children.push(React.createElement("FlatList-Footer", { key: "__footer" }, typeof ListFooterComponent === "function" ? React.createElement(ListFooterComponent) : ListFooterComponent));
|
|
145
|
-
if (refreshControl) children.unshift(refreshControl);
|
|
146
|
-
return React.createElement("FlatList", {
|
|
147
|
-
...rest,
|
|
148
|
-
ref
|
|
149
|
-
}, ...children);
|
|
150
|
-
});
|
|
151
|
-
FlatList.displayName = "FlatList";
|
|
152
|
-
return FlatList;
|
|
153
|
-
}
|
|
154
|
-
//#endregion
|
|
155
|
-
//#region src/mocks/components/SectionList.ts
|
|
156
|
-
function createSectionListMock() {
|
|
157
|
-
const SectionList = React.forwardRef((props, ref) => {
|
|
158
|
-
const { sections, renderItem, renderSectionHeader, renderSectionFooter, keyExtractor, refreshControl, ListEmptyComponent, ListHeaderComponent, ListFooterComponent, ItemSeparatorComponent, SectionSeparatorComponent, ...rest } = props;
|
|
159
|
-
const instanceMethods = {
|
|
160
|
-
scrollToEnd: vi.fn(),
|
|
161
|
-
scrollToIndex: vi.fn(),
|
|
162
|
-
scrollToLocation: vi.fn(),
|
|
163
|
-
recordInteraction: vi.fn(),
|
|
164
|
-
flashScrollIndicators: vi.fn(),
|
|
165
|
-
getScrollResponder: vi.fn(() => ({})),
|
|
166
|
-
getNativeScrollRef: vi.fn(),
|
|
167
|
-
getScrollableNode: vi.fn(),
|
|
168
|
-
setNativeProps: vi.fn()
|
|
169
|
-
};
|
|
170
|
-
React.useImperativeHandle(ref, () => instanceMethods);
|
|
171
|
-
const children = [];
|
|
172
|
-
if (ListHeaderComponent) children.push(React.createElement("SectionList-Header", { key: "__header" }, typeof ListHeaderComponent === "function" ? React.createElement(ListHeaderComponent) : ListHeaderComponent));
|
|
173
|
-
if (sections) sections.forEach((section, sectionIndex) => {
|
|
174
|
-
if (renderSectionHeader) children.push(React.createElement(React.Fragment, { key: `header-${sectionIndex}` }, renderSectionHeader({ section })));
|
|
175
|
-
if (SectionSeparatorComponent && sectionIndex > 0) children.push(React.createElement(React.Fragment, { key: `section-sep-${sectionIndex}` }, typeof SectionSeparatorComponent === "function" ? React.createElement(SectionSeparatorComponent) : SectionSeparatorComponent));
|
|
176
|
-
if (section.data) section.data.forEach((item, index) => {
|
|
177
|
-
const key = keyExtractor ? keyExtractor(item, index) : `${sectionIndex}-${index}`;
|
|
178
|
-
children.push(React.createElement(React.Fragment, { key }, renderItem({
|
|
179
|
-
item,
|
|
180
|
-
index,
|
|
181
|
-
section,
|
|
182
|
-
separators: {
|
|
183
|
-
highlight: vi.fn(),
|
|
184
|
-
unhighlight: vi.fn(),
|
|
185
|
-
updateProps: vi.fn()
|
|
186
|
-
}
|
|
187
|
-
})));
|
|
188
|
-
if (ItemSeparatorComponent && index < section.data.length - 1) children.push(React.createElement(React.Fragment, { key: `sep-${sectionIndex}-${index}` }, typeof ItemSeparatorComponent === "function" ? React.createElement(ItemSeparatorComponent) : ItemSeparatorComponent));
|
|
189
|
-
});
|
|
190
|
-
if (renderSectionFooter) children.push(React.createElement(React.Fragment, { key: `footer-${sectionIndex}` }, renderSectionFooter({ section })));
|
|
191
|
-
});
|
|
192
|
-
if (ListFooterComponent) children.push(React.createElement("SectionList-Footer", { key: "__footer" }, typeof ListFooterComponent === "function" ? React.createElement(ListFooterComponent) : ListFooterComponent));
|
|
193
|
-
if (refreshControl) children.unshift(refreshControl);
|
|
194
|
-
if ((!sections || sections.length === 0) && ListEmptyComponent) children.push(React.createElement("SectionList-Empty", { key: "__empty" }, typeof ListEmptyComponent === "function" ? React.createElement(ListEmptyComponent) : ListEmptyComponent));
|
|
195
|
-
return React.createElement("SectionList", {
|
|
196
|
-
...rest,
|
|
197
|
-
ref
|
|
198
|
-
}, ...children);
|
|
199
|
-
});
|
|
200
|
-
SectionList.displayName = "SectionList";
|
|
201
|
-
return SectionList;
|
|
202
|
-
}
|
|
203
|
-
//#endregion
|
|
204
57
|
//#region src/mocks/components/Modal.ts
|
|
205
58
|
function createModalMock() {
|
|
206
59
|
const Modal = React.forwardRef((props, ref) => {
|
|
@@ -216,27 +69,6 @@ function createModalMock() {
|
|
|
216
69
|
return Modal;
|
|
217
70
|
}
|
|
218
71
|
//#endregion
|
|
219
|
-
//#region src/mocks/components/TouchableOpacity.ts
|
|
220
|
-
function createTouchableOpacityMock() {
|
|
221
|
-
const TouchableOpacity = React.forwardRef((props, ref) => React.createElement("TouchableOpacity", buildPressableHostProps(props, ref)));
|
|
222
|
-
TouchableOpacity.displayName = "TouchableOpacity";
|
|
223
|
-
return TouchableOpacity;
|
|
224
|
-
}
|
|
225
|
-
//#endregion
|
|
226
|
-
//#region src/mocks/components/TouchableHighlight.ts
|
|
227
|
-
function createTouchableHighlightMock() {
|
|
228
|
-
const TouchableHighlight = React.forwardRef((props, ref) => React.createElement("TouchableHighlight", buildPressableHostProps(props, ref)));
|
|
229
|
-
TouchableHighlight.displayName = "TouchableHighlight";
|
|
230
|
-
return TouchableHighlight;
|
|
231
|
-
}
|
|
232
|
-
//#endregion
|
|
233
|
-
//#region src/mocks/components/TouchableWithoutFeedback.ts
|
|
234
|
-
function createTouchableWithoutFeedbackMock() {
|
|
235
|
-
const TouchableWithoutFeedback = React.forwardRef((props, ref) => React.createElement("TouchableWithoutFeedback", buildPressableHostProps(props, ref)));
|
|
236
|
-
TouchableWithoutFeedback.displayName = "TouchableWithoutFeedback";
|
|
237
|
-
return TouchableWithoutFeedback;
|
|
238
|
-
}
|
|
239
|
-
//#endregion
|
|
240
72
|
//#region src/mocks/components/TouchableNativeFeedback.ts
|
|
241
73
|
function createTouchableNativeFeedbackMock() {
|
|
242
74
|
const TouchableNativeFeedback = React.forwardRef((props, ref) => {
|
|
@@ -380,46 +212,6 @@ function createImageBackgroundMock() {
|
|
|
380
212
|
return ImageBackground;
|
|
381
213
|
}
|
|
382
214
|
//#endregion
|
|
383
|
-
//#region src/mocks/components/VirtualizedList.ts
|
|
384
|
-
function createVirtualizedListMock() {
|
|
385
|
-
const VirtualizedList = React.forwardRef((props, ref) => {
|
|
386
|
-
const { data, renderItem, keyExtractor, getItemCount, getItem, ListHeaderComponent, ListFooterComponent, ListEmptyComponent, ...rest } = props;
|
|
387
|
-
const instanceMethods = {
|
|
388
|
-
scrollToEnd: vi.fn(),
|
|
389
|
-
scrollToIndex: vi.fn(),
|
|
390
|
-
scrollToItem: vi.fn(),
|
|
391
|
-
scrollToOffset: vi.fn(),
|
|
392
|
-
recordInteraction: vi.fn(),
|
|
393
|
-
flashScrollIndicators: vi.fn(),
|
|
394
|
-
getScrollResponder: vi.fn(() => ({})),
|
|
395
|
-
getNativeScrollRef: vi.fn(),
|
|
396
|
-
getScrollableNode: vi.fn(),
|
|
397
|
-
setNativeProps: vi.fn()
|
|
398
|
-
};
|
|
399
|
-
React.useImperativeHandle(ref, () => instanceMethods);
|
|
400
|
-
const children = [];
|
|
401
|
-
if (ListHeaderComponent) children.push(React.createElement("VirtualizedList-Header", { key: "__header" }, typeof ListHeaderComponent === "function" ? React.createElement(ListHeaderComponent) : ListHeaderComponent));
|
|
402
|
-
const itemCount = getItemCount ? getItemCount(data) : data ? data.length : 0;
|
|
403
|
-
if (itemCount > 0) for (let index = 0; index < itemCount; index++) {
|
|
404
|
-
const item = getItem ? getItem(data, index) : data[index];
|
|
405
|
-
const key = keyExtractor ? keyExtractor(item, index) : String(index);
|
|
406
|
-
children.push(React.createElement(React.Fragment, { key }, renderItem({
|
|
407
|
-
item,
|
|
408
|
-
index,
|
|
409
|
-
separators: {}
|
|
410
|
-
})));
|
|
411
|
-
}
|
|
412
|
-
else if (ListEmptyComponent) children.push(React.createElement("VirtualizedList-Empty", { key: "__empty" }, typeof ListEmptyComponent === "function" ? React.createElement(ListEmptyComponent) : ListEmptyComponent));
|
|
413
|
-
if (ListFooterComponent) children.push(React.createElement("VirtualizedList-Footer", { key: "__footer" }, typeof ListFooterComponent === "function" ? React.createElement(ListFooterComponent) : ListFooterComponent));
|
|
414
|
-
return React.createElement("VirtualizedList", {
|
|
415
|
-
...rest,
|
|
416
|
-
ref
|
|
417
|
-
}, ...children);
|
|
418
|
-
});
|
|
419
|
-
VirtualizedList.displayName = "VirtualizedList";
|
|
420
|
-
return VirtualizedList;
|
|
421
|
-
}
|
|
422
|
-
//#endregion
|
|
423
215
|
//#region src/mocks/components/InputAccessoryView.ts
|
|
424
216
|
function createInputAccessoryViewMock() {
|
|
425
217
|
const InputAccessoryView = React.forwardRef((props, ref) => {
|
|
@@ -773,71 +565,152 @@ function checkValidRanges(inputRange, outputRange) {
|
|
|
773
565
|
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
566
|
if (inputRange.length !== outputRange.length) throw new Error("inputRange (" + inputRange.length + ") and outputRange (" + outputRange.length + ") must have the same length");
|
|
775
567
|
}
|
|
776
|
-
var
|
|
777
|
-
_value;
|
|
568
|
+
var AnimatedNode = class {
|
|
778
569
|
_listeners = /* @__PURE__ */ new Map();
|
|
570
|
+
_children = /* @__PURE__ */ new Set();
|
|
779
571
|
_listenerIdCounter = 0;
|
|
780
|
-
_tracking = null;
|
|
781
|
-
constructor(value = 0) {
|
|
782
|
-
this._value = value;
|
|
783
|
-
}
|
|
784
|
-
setValue(value) {
|
|
785
|
-
this._value = value;
|
|
786
|
-
this._listeners.forEach((fn) => fn({ value }));
|
|
787
|
-
}
|
|
788
572
|
getValue() {
|
|
789
|
-
return this.
|
|
573
|
+
return this.__getValue();
|
|
574
|
+
}
|
|
575
|
+
_attachToParents() {}
|
|
576
|
+
_detachFromParents() {}
|
|
577
|
+
_consumerCount() {
|
|
578
|
+
return this._listeners.size + this._children.size;
|
|
790
579
|
}
|
|
791
580
|
addListener(callback) {
|
|
792
581
|
const id = String(++this._listenerIdCounter);
|
|
793
582
|
this._listeners.set(id, callback);
|
|
583
|
+
if (this._consumerCount() === 1) this._attachToParents();
|
|
794
584
|
return id;
|
|
795
585
|
}
|
|
796
586
|
removeListener(id) {
|
|
797
|
-
this._listeners.delete(id);
|
|
587
|
+
if (this._listeners.delete(id) && this._consumerCount() === 0) this._detachFromParents();
|
|
798
588
|
}
|
|
799
589
|
removeAllListeners() {
|
|
590
|
+
const had = this._listeners.size > 0;
|
|
800
591
|
this._listeners.clear();
|
|
592
|
+
if (had && this._consumerCount() === 0) this._detachFromParents();
|
|
801
593
|
}
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
},
|
|
816
|
-
addListener: () => "0",
|
|
817
|
-
removeListener: () => {},
|
|
818
|
-
removeAllListeners: () => {},
|
|
819
|
-
stopAnimation: (cb) => cb?.(compute()),
|
|
820
|
-
resetAnimation: (cb) => cb?.(compute())
|
|
821
|
-
};
|
|
594
|
+
/** Graph edge: `fn` runs whenever this node's value may have changed. */
|
|
595
|
+
__addChild(fn) {
|
|
596
|
+
this._children.add(fn);
|
|
597
|
+
if (this._consumerCount() === 1) this._attachToParents();
|
|
598
|
+
}
|
|
599
|
+
__removeChild(fn) {
|
|
600
|
+
if (this._children.delete(fn) && this._consumerCount() === 0) this._detachFromParents();
|
|
601
|
+
}
|
|
602
|
+
/** Notify consumers with the node's CURRENT (offset-inclusive) value. */
|
|
603
|
+
_notify() {
|
|
604
|
+
if (this._listeners.size > 0) {
|
|
605
|
+
const value = this.__getValue();
|
|
606
|
+
this._listeners.forEach((fn) => fn({ value }));
|
|
822
607
|
}
|
|
823
|
-
|
|
608
|
+
this._children.forEach((fn) => fn());
|
|
609
|
+
}
|
|
610
|
+
interpolate(config) {
|
|
611
|
+
const { inputRange, outputRange } = config || {};
|
|
612
|
+
if (!inputRange || !outputRange || inputRange.length < 2 || outputRange.length < 2) return new AnimatedValue(Number(this.__getValue()) || 0);
|
|
613
|
+
return new AnimatedInterpolation(this, config);
|
|
824
614
|
}
|
|
825
615
|
stopAnimation(callback) {
|
|
826
|
-
callback?.(this.
|
|
616
|
+
callback?.(this.__getValue());
|
|
827
617
|
}
|
|
828
618
|
resetAnimation(callback) {
|
|
829
|
-
callback?.(this.
|
|
619
|
+
callback?.(this.__getValue());
|
|
830
620
|
}
|
|
831
621
|
toJSON() {
|
|
832
|
-
return this.
|
|
622
|
+
return this.__getValue();
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
var AnimatedValue = class extends AnimatedNode {
|
|
626
|
+
_value;
|
|
627
|
+
_offset = 0;
|
|
628
|
+
_tracking = null;
|
|
629
|
+
constructor(value = 0) {
|
|
630
|
+
super();
|
|
631
|
+
this._value = value;
|
|
632
|
+
}
|
|
633
|
+
setValue(value) {
|
|
634
|
+
this._value = value;
|
|
635
|
+
this._notify();
|
|
636
|
+
}
|
|
637
|
+
__getValue() {
|
|
638
|
+
return this._value + this._offset;
|
|
639
|
+
}
|
|
640
|
+
setOffset(offset) {
|
|
641
|
+
this._offset = offset;
|
|
642
|
+
}
|
|
643
|
+
flattenOffset() {
|
|
644
|
+
this._value += this._offset;
|
|
645
|
+
this._offset = 0;
|
|
646
|
+
}
|
|
647
|
+
extractOffset() {
|
|
648
|
+
this._offset += this._value;
|
|
649
|
+
this._value = 0;
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
var AnimatedInterpolation = class extends AnimatedNode {
|
|
653
|
+
_parent;
|
|
654
|
+
_config;
|
|
655
|
+
_preparedString = null;
|
|
656
|
+
constructor(parent, config) {
|
|
657
|
+
super();
|
|
658
|
+
this._parent = parent;
|
|
659
|
+
this._config = config;
|
|
660
|
+
const { inputRange, outputRange } = config;
|
|
661
|
+
checkValidRanges(inputRange, outputRange);
|
|
662
|
+
if (typeof outputRange[0] === "string") this._preparedString = prepareStringInterpolation(outputRange);
|
|
663
|
+
}
|
|
664
|
+
_propagate = () => this._notify();
|
|
665
|
+
_attachToParents() {
|
|
666
|
+
this._parent.__addChild(this._propagate);
|
|
667
|
+
}
|
|
668
|
+
_detachFromParents() {
|
|
669
|
+
this._parent.__removeChild(this._propagate);
|
|
670
|
+
}
|
|
671
|
+
__getValue() {
|
|
672
|
+
const { inputRange, outputRange, extrapolate = "extend", extrapolateLeft, extrapolateRight, easing = IDENTITY_EASING } = this._config;
|
|
673
|
+
const input = Number(this._parent.__getValue());
|
|
674
|
+
if (this._preparedString) return interpolateString(this._preparedString, input, inputRange, extrapolate, extrapolateLeft, extrapolateRight, easing);
|
|
675
|
+
return interpolateNumeric(input, inputRange, outputRange, extrapolate, extrapolateLeft, extrapolateRight, easing);
|
|
676
|
+
}
|
|
677
|
+
interpolate(config) {
|
|
678
|
+
if (this._preparedString) throw new Error("Cannot chain an interpolation off a string-valued interpolation");
|
|
679
|
+
return super.interpolate(config);
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
var AnimatedOp = class extends AnimatedNode {
|
|
683
|
+
_compute;
|
|
684
|
+
_parents;
|
|
685
|
+
_propagate = () => this._notify();
|
|
686
|
+
constructor(inputs, compute) {
|
|
687
|
+
super();
|
|
688
|
+
this._compute = compute;
|
|
689
|
+
this._parents = inputs.filter((i) => i instanceof AnimatedNode);
|
|
690
|
+
}
|
|
691
|
+
_attachToParents() {
|
|
692
|
+
for (const parent of this._parents) parent.__addChild(this._propagate);
|
|
693
|
+
}
|
|
694
|
+
_detachFromParents() {
|
|
695
|
+
for (const parent of this._parents) parent.__removeChild(this._propagate);
|
|
696
|
+
}
|
|
697
|
+
__getValue() {
|
|
698
|
+
return this._compute();
|
|
833
699
|
}
|
|
834
|
-
setOffset(_offset) {}
|
|
835
|
-
flattenOffset() {}
|
|
836
|
-
extractOffset() {}
|
|
837
700
|
};
|
|
701
|
+
/** Numeric value of an operand that may be a node, a number, or junk (→ 0). */
|
|
702
|
+
function operandValue(v) {
|
|
703
|
+
if (v instanceof AnimatedNode) {
|
|
704
|
+
const n = Number(v.__getValue());
|
|
705
|
+
return Number.isNaN(n) ? 0 : n;
|
|
706
|
+
}
|
|
707
|
+
return typeof v === "number" ? v : 0;
|
|
708
|
+
}
|
|
838
709
|
var AnimatedValueXY = class {
|
|
839
710
|
x;
|
|
840
711
|
y;
|
|
712
|
+
_listenerIdCounter = 0;
|
|
713
|
+
_joint = /* @__PURE__ */ new Map();
|
|
841
714
|
constructor(value) {
|
|
842
715
|
this.x = new AnimatedValue(value?.x ?? 0);
|
|
843
716
|
this.y = new AnimatedValue(value?.y ?? 0);
|
|
@@ -846,31 +719,52 @@ var AnimatedValueXY = class {
|
|
|
846
719
|
this.x.setValue(value.x);
|
|
847
720
|
this.y.setValue(value.y);
|
|
848
721
|
}
|
|
849
|
-
setOffset(
|
|
850
|
-
|
|
851
|
-
|
|
722
|
+
setOffset(offset) {
|
|
723
|
+
this.x.setOffset(offset.x);
|
|
724
|
+
this.y.setOffset(offset.y);
|
|
725
|
+
}
|
|
726
|
+
flattenOffset() {
|
|
727
|
+
this.x.flattenOffset();
|
|
728
|
+
this.y.flattenOffset();
|
|
729
|
+
}
|
|
730
|
+
extractOffset() {
|
|
731
|
+
this.x.extractOffset();
|
|
732
|
+
this.y.extractOffset();
|
|
733
|
+
}
|
|
734
|
+
__getValue() {
|
|
735
|
+
return {
|
|
736
|
+
x: this.x.__getValue(),
|
|
737
|
+
y: this.y.__getValue()
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
getValue() {
|
|
741
|
+
return this.__getValue();
|
|
742
|
+
}
|
|
852
743
|
stopAnimation(callback) {
|
|
853
|
-
callback?.(
|
|
854
|
-
x: this.x.getValue(),
|
|
855
|
-
y: this.y.getValue()
|
|
856
|
-
});
|
|
744
|
+
callback?.(this.__getValue());
|
|
857
745
|
}
|
|
858
746
|
resetAnimation(callback) {
|
|
859
|
-
callback?.(
|
|
860
|
-
x: this.x.getValue(),
|
|
861
|
-
y: this.y.getValue()
|
|
862
|
-
});
|
|
747
|
+
callback?.(this.__getValue());
|
|
863
748
|
}
|
|
864
|
-
addListener(
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
749
|
+
addListener(callback) {
|
|
750
|
+
const emit = () => callback(this.__getValue());
|
|
751
|
+
const id = String(++this._listenerIdCounter);
|
|
752
|
+
const pair = {
|
|
753
|
+
x: this.x.addListener(emit),
|
|
754
|
+
y: this.y.addListener(emit)
|
|
868
755
|
};
|
|
756
|
+
this._joint.set(id, pair);
|
|
757
|
+
return pair;
|
|
869
758
|
}
|
|
870
759
|
removeListener(id) {
|
|
871
760
|
this.x.removeListener(id.x);
|
|
872
761
|
this.y.removeListener(id.y);
|
|
873
762
|
}
|
|
763
|
+
removeAllListeners() {
|
|
764
|
+
this.x.removeAllListeners();
|
|
765
|
+
this.y.removeAllListeners();
|
|
766
|
+
this._joint.clear();
|
|
767
|
+
}
|
|
874
768
|
getLayout() {
|
|
875
769
|
return {
|
|
876
770
|
left: this.x,
|
|
@@ -968,26 +862,25 @@ function parseColorString(color) {
|
|
|
968
862
|
1
|
|
969
863
|
];
|
|
970
864
|
}
|
|
971
|
-
var AnimatedColor = class {
|
|
865
|
+
var AnimatedColor = class extends AnimatedNode {
|
|
972
866
|
r;
|
|
973
867
|
g;
|
|
974
868
|
b;
|
|
975
869
|
a;
|
|
976
|
-
_listeners = /* @__PURE__ */ new Map();
|
|
977
|
-
_listenerIdCounter = 0;
|
|
978
870
|
constructor(color) {
|
|
871
|
+
super();
|
|
979
872
|
if (typeof color === "string") {
|
|
980
873
|
const [r, g, b, a] = parseColorString(color);
|
|
981
874
|
this.r = new AnimatedValue(r);
|
|
982
875
|
this.g = new AnimatedValue(g);
|
|
983
876
|
this.b = new AnimatedValue(b);
|
|
984
877
|
this.a = new AnimatedValue(a);
|
|
985
|
-
} else if (color && typeof color === "object"
|
|
878
|
+
} else if (color && typeof color === "object" && color.r instanceof AnimatedValue) {
|
|
986
879
|
this.r = color.r;
|
|
987
880
|
this.g = color.g;
|
|
988
881
|
this.b = color.b;
|
|
989
882
|
this.a = color.a;
|
|
990
|
-
} else if (typeof color.r === "number") {
|
|
883
|
+
} else if (color && typeof color === "object" && typeof color.r === "number") {
|
|
991
884
|
this.r = new AnimatedValue(color.r);
|
|
992
885
|
this.g = new AnimatedValue(color.g ?? 0);
|
|
993
886
|
this.b = new AnimatedValue(color.b ?? 0);
|
|
@@ -998,12 +891,12 @@ var AnimatedColor = class {
|
|
|
998
891
|
this.b = new AnimatedValue(0);
|
|
999
892
|
this.a = new AnimatedValue(1);
|
|
1000
893
|
}
|
|
1001
|
-
|
|
1002
|
-
this.r
|
|
1003
|
-
this.g
|
|
1004
|
-
this.b
|
|
1005
|
-
this.a
|
|
1006
|
-
|
|
894
|
+
for (const channel of [
|
|
895
|
+
this.r,
|
|
896
|
+
this.g,
|
|
897
|
+
this.b,
|
|
898
|
+
this.a
|
|
899
|
+
]) channel.__addChild(() => this._notify());
|
|
1007
900
|
}
|
|
1008
901
|
setValue(value) {
|
|
1009
902
|
if (typeof value === "string") {
|
|
@@ -1018,31 +911,12 @@ var AnimatedColor = class {
|
|
|
1018
911
|
this.b.setValue(value.b ?? 0);
|
|
1019
912
|
this.a.setValue(value.a ?? 1);
|
|
1020
913
|
}
|
|
1021
|
-
const colorStr = this.__getValue();
|
|
1022
|
-
this._listeners.forEach((fn) => fn({ value: colorStr }));
|
|
1023
914
|
}
|
|
1024
915
|
__getValue() {
|
|
1025
916
|
return `rgba(${Math.round(this.r.getValue())}, ${Math.round(this.g.getValue())}, ${Math.round(this.b.getValue())}, ${this.a.getValue()})`;
|
|
1026
917
|
}
|
|
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();
|
|
1037
|
-
}
|
|
1038
918
|
setOffset(_offset) {}
|
|
1039
919
|
flattenOffset() {}
|
|
1040
|
-
stopAnimation(callback) {
|
|
1041
|
-
callback?.(this.__getValue());
|
|
1042
|
-
}
|
|
1043
|
-
resetAnimation(callback) {
|
|
1044
|
-
callback?.(this.__getValue());
|
|
1045
|
-
}
|
|
1046
920
|
};
|
|
1047
921
|
function createAnimation(onStart) {
|
|
1048
922
|
return {
|
|
@@ -1057,9 +931,9 @@ function createAnimation(onStart) {
|
|
|
1057
931
|
};
|
|
1058
932
|
}
|
|
1059
933
|
function resolveAnimatedLeaf(v) {
|
|
1060
|
-
if (v instanceof
|
|
934
|
+
if (v instanceof AnimatedNode) return v.__getValue();
|
|
1061
935
|
if (v && typeof v === "object" && typeof v.__getValue === "function") return v.__getValue();
|
|
1062
|
-
if (v && typeof v === "object" && typeof v.getValue === "function") return v.getValue();
|
|
936
|
+
if (v && typeof v === "object" && !Array.isArray(v) && typeof v.getValue === "function") return v.getValue();
|
|
1063
937
|
return v;
|
|
1064
938
|
}
|
|
1065
939
|
function resolveAnimatedStyle(style) {
|
|
@@ -1070,16 +944,40 @@ function resolveAnimatedStyle(style) {
|
|
|
1070
944
|
const out = {};
|
|
1071
945
|
for (const key of Object.keys(style)) {
|
|
1072
946
|
const val = style[key];
|
|
1073
|
-
if (key === "transform" && Array.isArray(val)) out[key] = val.map((t) => t && typeof t === "object" && !(t instanceof
|
|
947
|
+
if (key === "transform" && Array.isArray(val)) out[key] = val.map((t) => t && typeof t === "object" && !(t instanceof AnimatedNode) ? Object.fromEntries(Object.keys(t).map((tk) => [tk, resolveAnimatedLeaf(t[tk])])) : resolveAnimatedLeaf(t));
|
|
1074
948
|
else out[key] = resolveAnimatedLeaf(val);
|
|
1075
949
|
}
|
|
1076
950
|
return out;
|
|
1077
951
|
}
|
|
1078
952
|
return style;
|
|
1079
953
|
}
|
|
954
|
+
function collectAnimatedNodes(style, out) {
|
|
955
|
+
if (style instanceof AnimatedNode) {
|
|
956
|
+
out.add(style);
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
if (Array.isArray(style)) {
|
|
960
|
+
for (const s of style) collectAnimatedNodes(s, out);
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
if (style && typeof style === "object") for (const key of Object.keys(style)) collectAnimatedNodes(style[key], out);
|
|
964
|
+
}
|
|
965
|
+
function useAnimatedStyleSubscription(style) {
|
|
966
|
+
const [, force] = React.useReducer((n) => n + 1, 0);
|
|
967
|
+
React.useEffect(() => {
|
|
968
|
+
const nodes = /* @__PURE__ */ new Set();
|
|
969
|
+
collectAnimatedNodes(style, nodes);
|
|
970
|
+
if (nodes.size === 0) return void 0;
|
|
971
|
+
for (const node of nodes) node.__addChild(force);
|
|
972
|
+
return () => {
|
|
973
|
+
for (const node of nodes) node.__removeChild(force);
|
|
974
|
+
};
|
|
975
|
+
});
|
|
976
|
+
}
|
|
1080
977
|
function createAnimatedWrapper(displayName) {
|
|
1081
978
|
const hostName = displayName.replace(/^Animated\./, "");
|
|
1082
979
|
const Component = React.forwardRef((props, ref) => {
|
|
980
|
+
useAnimatedStyleSubscription(props?.style);
|
|
1083
981
|
if (props && props.style !== void 0) {
|
|
1084
982
|
const { style, ...rest } = props;
|
|
1085
983
|
return React.createElement(hostName, {
|
|
@@ -1098,7 +996,7 @@ function createAnimatedWrapper(displayName) {
|
|
|
1098
996
|
}
|
|
1099
997
|
function stopTracking(value) {
|
|
1100
998
|
if (value._tracking) {
|
|
1101
|
-
value._tracking.source.
|
|
999
|
+
value._tracking.source.__removeChild(value._tracking.child);
|
|
1102
1000
|
value._tracking = null;
|
|
1103
1001
|
}
|
|
1104
1002
|
}
|
|
@@ -1106,14 +1004,32 @@ function startTracking(value, toValue) {
|
|
|
1106
1004
|
stopTracking(value);
|
|
1107
1005
|
if (toValue instanceof AnimatedValue) {
|
|
1108
1006
|
value.setValue(toValue.getValue());
|
|
1007
|
+
const child = () => value.setValue(toValue.__getValue());
|
|
1008
|
+
toValue.__addChild(child);
|
|
1109
1009
|
value._tracking = {
|
|
1110
1010
|
source: toValue,
|
|
1111
|
-
|
|
1112
|
-
value.setValue(v);
|
|
1113
|
-
})
|
|
1011
|
+
child
|
|
1114
1012
|
};
|
|
1115
1013
|
} else value.setValue(toValue);
|
|
1116
1014
|
}
|
|
1015
|
+
var AnimatedDiffClamp = class extends AnimatedNode {
|
|
1016
|
+
_current;
|
|
1017
|
+
constructor(a, min, max) {
|
|
1018
|
+
super();
|
|
1019
|
+
let lastInput = operandValue(a);
|
|
1020
|
+
this._current = Math.min(Math.max(lastInput, min), max);
|
|
1021
|
+
if (a instanceof AnimatedNode) a.__addChild(() => {
|
|
1022
|
+
const value = Number(a.__getValue());
|
|
1023
|
+
const diff = value - lastInput;
|
|
1024
|
+
lastInput = value;
|
|
1025
|
+
this._current = Math.min(Math.max(this._current + diff, min), max);
|
|
1026
|
+
this._notify();
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
__getValue() {
|
|
1030
|
+
return this._current;
|
|
1031
|
+
}
|
|
1032
|
+
};
|
|
1117
1033
|
function createAnimatedMock() {
|
|
1118
1034
|
return {
|
|
1119
1035
|
Value: AnimatedValue,
|
|
@@ -1278,37 +1194,15 @@ function createAnimatedMock() {
|
|
|
1278
1194
|
delay: vi.fn((_time) => {
|
|
1279
1195
|
return createAnimation();
|
|
1280
1196
|
}),
|
|
1281
|
-
add: vi.fn((a, b) =>
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
divide: vi.fn((a, b) => {
|
|
1291
|
-
const aVal = a instanceof AnimatedValue ? a.getValue() : typeof a === "number" ? a : 0;
|
|
1292
|
-
const bVal = b instanceof AnimatedValue ? b.getValue() : typeof b === "number" ? b : 0;
|
|
1293
|
-
return new AnimatedValue(bVal === 0 ? 0 : aVal / bVal);
|
|
1294
|
-
}),
|
|
1295
|
-
modulo: vi.fn((a, modulus) => {
|
|
1296
|
-
return new AnimatedValue(((a instanceof AnimatedValue ? a.getValue() : typeof a === "number" ? a : 0) % modulus + modulus) % modulus);
|
|
1297
|
-
}),
|
|
1298
|
-
diffClamp: vi.fn((a, min, max) => {
|
|
1299
|
-
const result = new AnimatedValue(Math.min(Math.max(a instanceof AnimatedValue ? a.getValue() : 0, min), max));
|
|
1300
|
-
if (a instanceof AnimatedValue) {
|
|
1301
|
-
let lastInput = a.getValue();
|
|
1302
|
-
let current = result.getValue();
|
|
1303
|
-
a.addListener(({ value }) => {
|
|
1304
|
-
const diff = value - lastInput;
|
|
1305
|
-
lastInput = value;
|
|
1306
|
-
current = Math.min(Math.max(current + diff, min), max);
|
|
1307
|
-
result.setValue(current);
|
|
1308
|
-
});
|
|
1309
|
-
}
|
|
1310
|
-
return result;
|
|
1311
|
-
}),
|
|
1197
|
+
add: vi.fn((a, b) => new AnimatedOp([a, b], () => operandValue(a) + operandValue(b))),
|
|
1198
|
+
subtract: vi.fn((a, b) => new AnimatedOp([a, b], () => operandValue(a) - operandValue(b))),
|
|
1199
|
+
multiply: vi.fn((a, b) => new AnimatedOp([a, b], () => operandValue(a) * operandValue(b))),
|
|
1200
|
+
divide: vi.fn((a, b) => new AnimatedOp([a, b], () => {
|
|
1201
|
+
const divisor = operandValue(b);
|
|
1202
|
+
return divisor === 0 ? 0 : operandValue(a) / divisor;
|
|
1203
|
+
})),
|
|
1204
|
+
modulo: vi.fn((a, modulus) => new AnimatedOp([a], () => (operandValue(a) % modulus + modulus) % modulus)),
|
|
1205
|
+
diffClamp: vi.fn((a, min, max) => new AnimatedDiffClamp(a, min, max)),
|
|
1312
1206
|
event: vi.fn((argMapping, config) => {
|
|
1313
1207
|
const handler = vi.fn((...args) => {
|
|
1314
1208
|
argMapping.forEach((mapping, index) => {
|
|
@@ -1337,6 +1231,15 @@ function createAnimatedMock() {
|
|
|
1337
1231
|
unforkEvent: vi.fn(),
|
|
1338
1232
|
createAnimatedComponent: vi.fn((component) => {
|
|
1339
1233
|
const Wrapper = React.forwardRef((props, ref) => {
|
|
1234
|
+
useAnimatedStyleSubscription(props?.style);
|
|
1235
|
+
if (props && props.style !== void 0) {
|
|
1236
|
+
const { style, ...rest } = props;
|
|
1237
|
+
return React.createElement(component, {
|
|
1238
|
+
...rest,
|
|
1239
|
+
style: resolveAnimatedStyle(style),
|
|
1240
|
+
ref
|
|
1241
|
+
});
|
|
1242
|
+
}
|
|
1340
1243
|
return React.createElement(component, {
|
|
1341
1244
|
...props,
|
|
1342
1245
|
ref
|
|
@@ -2238,7 +2141,6 @@ function createProcessColorMock() {
|
|
|
2238
2141
|
return (Math.round(parseFloat(a) * 255) << 24) + (r << 16) + (g << 8) + b >>> 0;
|
|
2239
2142
|
}
|
|
2240
2143
|
}
|
|
2241
|
-
return 4278190080;
|
|
2242
2144
|
});
|
|
2243
2145
|
}
|
|
2244
2146
|
function createFindNodeHandleMock() {
|
|
@@ -2308,17 +2210,23 @@ function createTouchableMock() {
|
|
|
2308
2210
|
}
|
|
2309
2211
|
function createUseAnimatedValueMock() {
|
|
2310
2212
|
return vi.fn((initialValue) => {
|
|
2311
|
-
|
|
2213
|
+
const ref = React.useRef(null);
|
|
2214
|
+
if (ref.current == null) ref.current = new AnimatedValue(initialValue);
|
|
2215
|
+
return ref.current;
|
|
2312
2216
|
});
|
|
2313
2217
|
}
|
|
2314
2218
|
function createUseAnimatedValueXYMock() {
|
|
2315
2219
|
return vi.fn((initialValue) => {
|
|
2316
|
-
|
|
2220
|
+
const ref = React.useRef(null);
|
|
2221
|
+
if (ref.current == null) ref.current = new AnimatedValueXY(initialValue);
|
|
2222
|
+
return ref.current;
|
|
2317
2223
|
});
|
|
2318
2224
|
}
|
|
2319
2225
|
function createUseAnimatedColorMock() {
|
|
2320
2226
|
return vi.fn((initialValue) => {
|
|
2321
|
-
|
|
2227
|
+
const ref = React.useRef(null);
|
|
2228
|
+
if (ref.current == null) ref.current = new AnimatedColor(initialValue);
|
|
2229
|
+
return ref.current;
|
|
2322
2230
|
});
|
|
2323
2231
|
}
|
|
2324
2232
|
function buildReactNativeMock(platform = "ios") {
|
|
@@ -2486,7 +2394,54 @@ function buildReactNativeMock(platform = "ios") {
|
|
|
2486
2394
|
const VIRTUAL_RN_PATH = "\0vitest-native:react-native";
|
|
2487
2395
|
let originalResolveFilename = null;
|
|
2488
2396
|
let installed = false;
|
|
2397
|
+
let rnMock$1 = null;
|
|
2489
2398
|
const presetRedirects = /* @__PURE__ */ new Map();
|
|
2399
|
+
const presetMocks = /* @__PURE__ */ new Map();
|
|
2400
|
+
const leafModulePaths = /* @__PURE__ */ new Set();
|
|
2401
|
+
/**
|
|
2402
|
+
* The leaf module name a subpath require points at ("pkg/lib/Swipeable" or
|
|
2403
|
+
* ".../Platform.ios.js" → "Platform"). Mirrors native/match.mjs.
|
|
2404
|
+
*/
|
|
2405
|
+
function subpathLeafOf(request) {
|
|
2406
|
+
const base = request.split("/").pop();
|
|
2407
|
+
if (!base) return null;
|
|
2408
|
+
return base.split(".")[0] || null;
|
|
2409
|
+
}
|
|
2410
|
+
/**
|
|
2411
|
+
* Wrap a leaf export in Babel-CJS interop shape: the real compiled deep entry
|
|
2412
|
+
* exports `{ __esModule: true, default: X }`. A live Proxy keeps
|
|
2413
|
+
* direct-property consumers (`require('pkg/Sub').OS`) working too.
|
|
2414
|
+
*/
|
|
2415
|
+
function withDefaultInterop(value) {
|
|
2416
|
+
if (value === null || typeof value !== "object" && typeof value !== "function") return {
|
|
2417
|
+
__esModule: true,
|
|
2418
|
+
default: value
|
|
2419
|
+
};
|
|
2420
|
+
return new Proxy(value, {
|
|
2421
|
+
get: (t, p, r) => p === "default" ? t : p === "__esModule" ? true : Reflect.get(t, p, r),
|
|
2422
|
+
has: (t, p) => p === "default" || p === "__esModule" || Reflect.has(t, p)
|
|
2423
|
+
});
|
|
2424
|
+
}
|
|
2425
|
+
/**
|
|
2426
|
+
* Get or create a synthetic Module._cache entry for a subpath require whose
|
|
2427
|
+
* leaf name matches an export on the mock. Returns its virtual path, or null
|
|
2428
|
+
* when the leaf has no matching export (caller falls back to the root mock).
|
|
2429
|
+
*/
|
|
2430
|
+
function leafModulePath(Module, keyPrefix, mock, request) {
|
|
2431
|
+
const leaf = subpathLeafOf(request);
|
|
2432
|
+
if (!leaf || !Object.prototype.hasOwnProperty.call(mock, leaf)) return null;
|
|
2433
|
+
const virtualPath = `${keyPrefix}:${leaf}`;
|
|
2434
|
+
if (!Module._cache[virtualPath]) {
|
|
2435
|
+
const syntheticModule = new Module(virtualPath);
|
|
2436
|
+
syntheticModule.id = virtualPath;
|
|
2437
|
+
syntheticModule.filename = virtualPath;
|
|
2438
|
+
syntheticModule.loaded = true;
|
|
2439
|
+
syntheticModule.exports = withDefaultInterop(mock[leaf]);
|
|
2440
|
+
Module._cache[virtualPath] = syntheticModule;
|
|
2441
|
+
leafModulePaths.add(virtualPath);
|
|
2442
|
+
}
|
|
2443
|
+
return virtualPath;
|
|
2444
|
+
}
|
|
2490
2445
|
function installCjsBridge(mockObject) {
|
|
2491
2446
|
if (installed) return;
|
|
2492
2447
|
try {
|
|
@@ -2497,16 +2452,36 @@ function installCjsBridge(mockObject) {
|
|
|
2497
2452
|
syntheticModule.loaded = true;
|
|
2498
2453
|
syntheticModule.exports = mockObject;
|
|
2499
2454
|
Module._cache[VIRTUAL_RN_PATH] = syntheticModule;
|
|
2455
|
+
rnMock$1 = mockObject;
|
|
2500
2456
|
originalResolveFilename = Module._resolveFilename;
|
|
2501
2457
|
Module._resolveFilename = function(request, parent, isMain, options) {
|
|
2502
|
-
if (request === "react-native" || request.startsWith("react-native/"))
|
|
2458
|
+
if (request === "react-native" || request.startsWith("react-native/")) {
|
|
2459
|
+
if (request === "react-native/package.json") try {
|
|
2460
|
+
return originalResolveFilename.call(this, request, parent, isMain, options);
|
|
2461
|
+
} catch {}
|
|
2462
|
+
else if (request !== "react-native" && rnMock$1) {
|
|
2463
|
+
const leafPath = leafModulePath(Module, VIRTUAL_RN_PATH, rnMock$1, request);
|
|
2464
|
+
if (leafPath) return leafPath;
|
|
2465
|
+
}
|
|
2466
|
+
return VIRTUAL_RN_PATH;
|
|
2467
|
+
}
|
|
2503
2468
|
const exactMatch = presetRedirects.get(request);
|
|
2504
2469
|
if (exactMatch) return exactMatch;
|
|
2505
2470
|
const slashIdx = request.indexOf("/", request.startsWith("@") ? request.indexOf("/") + 1 : 0);
|
|
2506
2471
|
if (slashIdx !== -1) {
|
|
2507
2472
|
const pkg = request.slice(0, slashIdx);
|
|
2508
2473
|
const subpathMatch = presetRedirects.get(pkg);
|
|
2509
|
-
if (subpathMatch)
|
|
2474
|
+
if (subpathMatch) {
|
|
2475
|
+
if (request.endsWith(".json")) try {
|
|
2476
|
+
return originalResolveFilename.call(this, request, parent, isMain, options);
|
|
2477
|
+
} catch {}
|
|
2478
|
+
else {
|
|
2479
|
+
const mock = presetMocks.get(pkg);
|
|
2480
|
+
const leafPath = mock && leafModulePath(Module, subpathMatch, mock, request);
|
|
2481
|
+
if (leafPath) return leafPath;
|
|
2482
|
+
}
|
|
2483
|
+
return subpathMatch;
|
|
2484
|
+
}
|
|
2510
2485
|
}
|
|
2511
2486
|
return originalResolveFilename.call(this, request, parent, isMain, options);
|
|
2512
2487
|
};
|
|
@@ -2526,6 +2501,10 @@ function uninstallCjsBridge() {
|
|
|
2526
2501
|
delete Module._cache[VIRTUAL_RN_PATH];
|
|
2527
2502
|
for (const virtualPath of presetRedirects.values()) delete Module._cache[virtualPath];
|
|
2528
2503
|
presetRedirects.clear();
|
|
2504
|
+
presetMocks.clear();
|
|
2505
|
+
for (const virtualPath of leafModulePaths) delete Module._cache[virtualPath];
|
|
2506
|
+
leafModulePaths.clear();
|
|
2507
|
+
rnMock$1 = null;
|
|
2529
2508
|
installed = false;
|
|
2530
2509
|
} catch (e) {
|
|
2531
2510
|
if (process.env.VITEST_NATIVE_DIAGNOSTICS === "true") console.warn(`[vitest-native] CJS bridge uninstall failed: ${e?.message}`);
|
|
@@ -2542,6 +2521,7 @@ function installPresetCjsBridge(moduleName, mockObject) {
|
|
|
2542
2521
|
syntheticModule.exports = mockObject;
|
|
2543
2522
|
Module._cache[virtualPath] = syntheticModule;
|
|
2544
2523
|
presetRedirects.set(moduleName, virtualPath);
|
|
2524
|
+
presetMocks.set(moduleName, mockObject);
|
|
2545
2525
|
} catch (e) {
|
|
2546
2526
|
if (process.env.VITEST_NATIVE_DIAGNOSTICS === "true") console.warn(`[vitest-native] Preset CJS bridge for ${moduleName} failed: ${e?.message}`);
|
|
2547
2527
|
}
|