@terreno/test 57.17.0 → 57.19.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/CHANGELOG.md +1681 -0
  2. package/README.md +6 -0
  3. package/dist/assertions/expectAPIError.d.ts +18 -0
  4. package/dist/assertions/expectAPIError.js +33 -0
  5. package/dist/assertions/strictResponseProperties.d.ts +14 -0
  6. package/dist/assertions/strictResponseProperties.js +37 -0
  7. package/dist/cache/mongoTestCache.js +2 -2
  8. package/dist/cli/collectFiles.d.ts +38 -0
  9. package/dist/cli/collectFiles.js +73 -0
  10. package/dist/cli/testHygiene.d.ts +134 -0
  11. package/dist/cli/testHygiene.js +357 -0
  12. package/dist/cli/testHygieneCli.d.ts +2 -0
  13. package/dist/cli/testHygieneCli.js +5 -0
  14. package/dist/cli/testHygieneRules.d.ts +58 -0
  15. package/dist/cli/testHygieneRules.js +517 -0
  16. package/dist/cli/testShard.d.ts +155 -0
  17. package/dist/cli/testShard.js +377 -0
  18. package/dist/cli/testShardCli.d.ts +2 -0
  19. package/dist/cli/testShardCli.js +5 -0
  20. package/dist/expo/authTokenStorage.d.ts +76 -0
  21. package/dist/expo/authTokenStorage.js +160 -0
  22. package/dist/expo/expoModuleMocks.d.ts +24 -0
  23. package/dist/expo/expoModuleMocks.js +456 -0
  24. package/dist/expo/libraryModuleMocks.d.ts +17 -0
  25. package/dist/expo/libraryModuleMocks.js +347 -0
  26. package/dist/expo/mockHelpers.d.ts +39 -0
  27. package/dist/expo/mockHelpers.js +45 -0
  28. package/dist/expo/mockModuleAliases.d.ts +50 -0
  29. package/dist/expo/mockModuleAliases.js +174 -0
  30. package/dist/expo/reactNativeMock.d.ts +39 -0
  31. package/dist/expo/reactNativeMock.js +421 -0
  32. package/dist/expo/registerExpoTestEnvironment.d.ts +69 -0
  33. package/dist/expo/registerExpoTestEnvironment.js +163 -0
  34. package/dist/expo/renderWithTerreno.d.ts +47 -0
  35. package/dist/expo/renderWithTerreno.js +47 -0
  36. package/dist/http/authWithToken.d.ts +20 -0
  37. package/dist/http/authWithToken.js +30 -0
  38. package/dist/index.d.ts +14 -1
  39. package/dist/index.js +33 -1
  40. package/dist/logging/silenceLogs.js +0 -2
  41. package/dist/mocks/aiSdk.d.ts +77 -0
  42. package/dist/mocks/aiSdk.js +102 -0
  43. package/dist/mocks/cloudTasks.d.ts +91 -0
  44. package/dist/mocks/cloudTasks.js +197 -0
  45. package/dist/mocks/expoPush.d.ts +53 -0
  46. package/dist/mocks/expoPush.js +57 -0
  47. package/dist/mocks/gcs.d.ts +101 -0
  48. package/dist/mocks/gcs.js +162 -0
  49. package/dist/mocks/googleAuth.d.ts +69 -0
  50. package/dist/mocks/googleAuth.js +55 -0
  51. package/dist/mocks/mockGroup.d.ts +11 -0
  52. package/dist/mocks/mockGroup.js +21 -0
  53. package/dist/mocks/notifiers.d.ts +21 -0
  54. package/dist/mocks/notifiers.js +26 -0
  55. package/dist/mocks/sendgrid.d.ts +22 -0
  56. package/dist/mocks/sendgrid.js +32 -0
  57. package/dist/mocks/twilio.d.ts +72 -0
  58. package/dist/mocks/twilio.js +64 -0
  59. package/dist/preload/registerBackendPreload.js +4 -1
  60. package/dist/time/freezeTime.d.ts +16 -0
  61. package/dist/time/freezeTime.js +49 -0
  62. package/dist/transaction/testTransaction.d.ts +43 -6
  63. package/dist/transaction/testTransaction.js +371 -92
  64. package/dist/utils/waitUntil.d.ts +22 -0
  65. package/dist/utils/waitUntil.js +34 -0
  66. package/package.json +53 -8
  67. package/src/assertions/expectAPIError.ts +51 -0
  68. package/src/assertions/strictResponseProperties.ts +42 -0
  69. package/src/cache/mongoTestCache.ts +2 -2
  70. package/src/cli/collectFiles.ts +115 -0
  71. package/src/cli/testHygiene.ts +539 -0
  72. package/src/cli/testHygieneCli.ts +4 -0
  73. package/src/cli/testHygieneRules.ts +719 -0
  74. package/src/cli/testShard.ts +532 -0
  75. package/src/cli/testShardCli.ts +4 -0
  76. package/src/expo/authTokenStorage.ts +198 -0
  77. package/src/expo/expoModuleMocks.ts +503 -0
  78. package/src/expo/libraryModuleMocks.ts +411 -0
  79. package/src/expo/mockHelpers.ts +73 -0
  80. package/src/expo/mockModuleAliases.ts +252 -0
  81. package/src/expo/reactNativeMock.ts +534 -0
  82. package/src/expo/registerExpoTestEnvironment.ts +260 -0
  83. package/src/expo/renderWithTerreno.ts +79 -0
  84. package/src/http/authWithToken.ts +38 -0
  85. package/src/index.ts +82 -1
  86. package/src/logging/silenceLogs.ts +0 -2
  87. package/src/mocks/aiSdk.ts +199 -0
  88. package/src/mocks/cloudTasks.ts +323 -0
  89. package/src/mocks/expoPush.ts +134 -0
  90. package/src/mocks/gcs.ts +305 -0
  91. package/src/mocks/googleAuth.ts +136 -0
  92. package/src/mocks/mockGroup.ts +32 -0
  93. package/src/mocks/notifiers.ts +36 -0
  94. package/src/mocks/sendgrid.ts +57 -0
  95. package/src/mocks/twilio.ts +143 -0
  96. package/src/preload/registerBackendPreload.ts +4 -1
  97. package/src/time/freezeTime.ts +61 -0
  98. package/src/transaction/testTransaction.ts +456 -124
  99. package/src/utils/waitUntil.ts +47 -0
  100. package/biome.jsonc +0 -20
  101. package/bunfig.toml +0 -16
  102. package/src/cache/mongoTestCache.test.ts +0 -103
  103. package/src/env/setTerrenoTestEnv.test.ts +0 -26
  104. package/src/http/authAsUser.test.ts +0 -40
  105. package/src/http/getBaseServer.test.ts +0 -45
  106. package/src/logging/setupEnvironment.test.ts +0 -24
  107. package/src/logging/silenceLogs.test.ts +0 -52
  108. package/src/logging/testLogger.test.ts +0 -58
  109. package/src/mocks/sentryBun.test.ts +0 -230
  110. package/src/mongo/connection.test.ts +0 -43
  111. package/src/mongo/mongoServer.test.ts +0 -99
  112. package/src/preload/registerBackendPreload.test.ts +0 -10
  113. package/src/transaction/testTransaction.test.ts +0 -64
  114. package/src/utils/helpers.test.ts +0 -46
  115. package/tsconfig.json +0 -18
@@ -0,0 +1,411 @@
1
+ import {mock} from "bun:test";
2
+ import {
3
+ createContext,
4
+ createElement,
5
+ forwardRef,
6
+ type ReactElement,
7
+ type ReactNode,
8
+ useImperativeHandle,
9
+ useState,
10
+ } from "react";
11
+
12
+ import {
13
+ hostComponent,
14
+ hostComponents,
15
+ type MockComponentProps,
16
+ type ModuleExports,
17
+ type ModuleMockMap,
18
+ passthroughComponent,
19
+ stub,
20
+ stubIdentity,
21
+ stubResolving,
22
+ stubReturning,
23
+ } from "./mockHelpers";
24
+
25
+ /** Safe-area insets every safe-area hook and context reports. */
26
+ const MOCK_SAFE_AREA_INSETS = {bottom: 0, left: 0, right: 0, top: 0} as const;
27
+
28
+ /** Safe-area frame every safe-area hook and context reports. */
29
+ const MOCK_SAFE_AREA_FRAME = {height: 812, width: 375, x: 0, y: 0} as const;
30
+
31
+ /** Network state `@react-native-community/netinfo` reports: online over Wi-Fi. */
32
+ export const MOCK_NET_INFO_STATE = {
33
+ details: {},
34
+ isConnected: true,
35
+ isInternetReachable: true,
36
+ isWifiEnabled: true,
37
+ type: "wifi",
38
+ } as const;
39
+
40
+ /** A shared value: `.value` plus the `get`/`set`/`modify` API Reanimated 3+ exposes. */
41
+ interface MockSharedValue<T> {
42
+ get: () => T;
43
+ modify: (modifier: (value: T) => T) => T;
44
+ set: (value: T) => void;
45
+ value: T;
46
+ }
47
+
48
+ /** Gesture builder whose every configuration method returns the gesture itself. */
49
+ type ChainableGesture = Record<string, (...args: unknown[]) => ChainableGesture>;
50
+
51
+ const GESTURE_METHODS = [
52
+ "activeOffsetX",
53
+ "activeOffsetY",
54
+ "averageTouches",
55
+ "blocksExternalGesture",
56
+ "cancelsTouchesInView",
57
+ "direction",
58
+ "enabled",
59
+ "failOffsetX",
60
+ "failOffsetY",
61
+ "hitSlop",
62
+ "manualActivation",
63
+ "maxDelay",
64
+ "maxDist",
65
+ "maxDuration",
66
+ "maxPointers",
67
+ "minDistance",
68
+ "minDuration",
69
+ "minPointers",
70
+ "minVelocity",
71
+ "minVelocityX",
72
+ "minVelocityY",
73
+ "numberOfPointers",
74
+ "numberOfTaps",
75
+ "onBegin",
76
+ "onChange",
77
+ "onEnd",
78
+ "onFinalize",
79
+ "onStart",
80
+ "onTouchesCancelled",
81
+ "onTouchesDown",
82
+ "onTouchesMove",
83
+ "onTouchesUp",
84
+ "onUpdate",
85
+ "requireExternalGestureToFail",
86
+ "runOnJS",
87
+ "shouldCancelWhenOutside",
88
+ "simultaneousWithExternalGesture",
89
+ "withRef",
90
+ "withTestId",
91
+ ];
92
+
93
+ const createChainableGesture = (): ChainableGesture => {
94
+ const gesture: ChainableGesture = {};
95
+ for (const method of GESTURE_METHODS) {
96
+ gesture[method] = mock((): ChainableGesture => gesture);
97
+ }
98
+ return gesture;
99
+ };
100
+
101
+ const createSharedValue = <T>(initialValue: T): MockSharedValue<T> => {
102
+ const shared: MockSharedValue<T> = {
103
+ get: (): T => shared.value,
104
+ modify: (modifier: (value: T) => T): T => {
105
+ shared.value = modifier(shared.value);
106
+ return shared.value;
107
+ },
108
+ set: (value: T): void => {
109
+ shared.value = value;
110
+ },
111
+ value: initialValue,
112
+ };
113
+ return shared;
114
+ };
115
+
116
+ /** Linear interpolation clamped to the first/last range entries, like Reanimated's default. */
117
+ export const interpolate = (
118
+ value: number,
119
+ inputRange: readonly number[],
120
+ outputRange: readonly number[]
121
+ ): number => {
122
+ const inMin = inputRange[0] ?? 0;
123
+ const inMax = inputRange[inputRange.length - 1] ?? inMin;
124
+ const outMin = outputRange[0] ?? 0;
125
+ const outMax = outputRange[outputRange.length - 1] ?? outMin;
126
+ if (inMax === inMin) {
127
+ return outMin;
128
+ }
129
+ return outMin + ((value - inMin) / (inMax - inMin)) * (outMax - outMin);
130
+ };
131
+
132
+ const createReanimatedMock = (): ModuleExports => {
133
+ const Animated = {
134
+ createAnimatedComponent: stubIdentity(),
135
+ FlatList: hostComponent("FlatList"),
136
+ Image: hostComponent("Image"),
137
+ ScrollView: hostComponent("ScrollView"),
138
+ Text: hostComponent("Text"),
139
+ View: hostComponent("View"),
140
+ };
141
+ const layoutAnimation = (): Record<string, unknown> => {
142
+ const animation: Record<string, unknown> = {};
143
+ for (const method of ["delay", "duration", "easing", "springify", "damping", "withCallback"]) {
144
+ animation[method] = mock(() => animation);
145
+ }
146
+ return animation;
147
+ };
148
+ return {
149
+ ...Animated,
150
+ cancelAnimation: stub(),
151
+ default: Animated,
152
+ Easing: {
153
+ bezier: stubReturning(stubIdentity()),
154
+ cubic: stubIdentity(),
155
+ ease: stubIdentity(),
156
+ in: stubIdentity(),
157
+ inOut: stubIdentity(),
158
+ linear: stubIdentity(),
159
+ out: stubIdentity(),
160
+ quad: stubIdentity(),
161
+ },
162
+ Extrapolation: {CLAMP: "clamp", EXTEND: "extend", IDENTITY: "identity"},
163
+ FadeIn: layoutAnimation(),
164
+ FadeOut: layoutAnimation(),
165
+ interpolate: mock(interpolate),
166
+ interpolateColor: mock(
167
+ (_value: number, _input: readonly number[], output: readonly string[]) => output[0]
168
+ ),
169
+ Layout: layoutAnimation(),
170
+ LinearTransition: layoutAnimation(),
171
+ makeMutable: mock(createSharedValue),
172
+ ReduceMotion: {Always: "always", Never: "never", System: "system"},
173
+ runOnJS: stubIdentity(),
174
+ runOnUI: stubIdentity(),
175
+ SlideInDown: layoutAnimation(),
176
+ SlideOutDown: layoutAnimation(),
177
+ useAnimatedGestureHandler: stubReturning({}),
178
+ useAnimatedProps: mock((updater: () => unknown) => updater()),
179
+ useAnimatedReaction: stub(),
180
+ useAnimatedRef: stubReturning({current: null}),
181
+ useAnimatedScrollHandler: stubReturning(stub()),
182
+ useAnimatedStyle: mock((updater: () => unknown) => updater()),
183
+ useDerivedValue: mock((updater: () => unknown) => createSharedValue(updater())),
184
+ useReducedMotion: stubReturning(false),
185
+ useSharedValue: mock(createSharedValue),
186
+ withDecay: stubIdentity(),
187
+ withDelay: mock(<T>(_delay: number, value: T): T => value),
188
+ withRepeat: stubIdentity(),
189
+ withSequence: mock((...values: unknown[]): unknown => values[0]),
190
+ withSpring: stubIdentity(),
191
+ withTiming: stubIdentity(),
192
+ ZoomIn: layoutAnimation(),
193
+ ZoomOut: layoutAnimation(),
194
+ };
195
+ };
196
+
197
+ const createGestureHandlerMock = (): ModuleExports => {
198
+ const gesture = mock(createChainableGesture);
199
+ return {
200
+ ...hostComponents([
201
+ "BaseButton",
202
+ "BorderlessButton",
203
+ "DrawerLayout",
204
+ "FlatList",
205
+ "GestureDetector",
206
+ "LongPressGestureHandler",
207
+ "PanGestureHandler",
208
+ "PinchGestureHandler",
209
+ "RectButton",
210
+ "ScrollView",
211
+ "Swipeable",
212
+ "TapGestureHandler",
213
+ "TouchableHighlight",
214
+ "TouchableOpacity",
215
+ "TouchableWithoutFeedback",
216
+ ]),
217
+ createNativeWrapper: stubIdentity(),
218
+ Directions: {DOWN: 8, LEFT: 2, RIGHT: 1, UP: 4},
219
+ Gesture: {
220
+ Exclusive: gesture,
221
+ Fling: gesture,
222
+ Hover: gesture,
223
+ LongPress: gesture,
224
+ Manual: gesture,
225
+ Native: gesture,
226
+ Pan: gesture,
227
+ Pinch: gesture,
228
+ Race: gesture,
229
+ Rotation: gesture,
230
+ Simultaneous: gesture,
231
+ Tap: gesture,
232
+ },
233
+ GestureHandlerRootView: hostComponent("GestureHandlerRootView"),
234
+ gestureHandlerRootHOC: stubIdentity(),
235
+ State: {ACTIVE: 4, BEGAN: 2, CANCELLED: 3, END: 5, FAILED: 1, UNDETERMINED: 0},
236
+ };
237
+ };
238
+
239
+ const SVG_ELEMENTS = [
240
+ "Circle",
241
+ "ClipPath",
242
+ "Defs",
243
+ "Ellipse",
244
+ "ForeignObject",
245
+ "G",
246
+ "Line",
247
+ "LinearGradient",
248
+ "Marker",
249
+ "Mask",
250
+ "Path",
251
+ "Pattern",
252
+ "Polygon",
253
+ "Polyline",
254
+ "RadialGradient",
255
+ "Rect",
256
+ "Stop",
257
+ "Svg",
258
+ "Symbol",
259
+ "TextPath",
260
+ "TSpan",
261
+ "Use",
262
+ ];
263
+
264
+ const createSvgMock = (): ModuleExports => {
265
+ const elements = hostComponents(SVG_ELEMENTS);
266
+ return {
267
+ ...elements,
268
+ default: elements.Svg,
269
+ Image: hostComponent("SvgImage"),
270
+ SvgXml: hostComponent("SvgXml"),
271
+ Text: hostComponent("SvgText"),
272
+ };
273
+ };
274
+
275
+ const createSafeAreaMock = (): ModuleExports => {
276
+ const metrics = {frame: MOCK_SAFE_AREA_FRAME, insets: MOCK_SAFE_AREA_INSETS};
277
+ const SafeAreaInsetsContext = createContext<typeof MOCK_SAFE_AREA_INSETS>(MOCK_SAFE_AREA_INSETS);
278
+ const SafeAreaFrameContext = createContext<typeof MOCK_SAFE_AREA_FRAME>(MOCK_SAFE_AREA_FRAME);
279
+ return {
280
+ initialWindowMetrics: metrics,
281
+ initialWindowSafeAreaInsets: MOCK_SAFE_AREA_INSETS,
282
+ SafeAreaFrameContext,
283
+ SafeAreaInsetsContext,
284
+ SafeAreaProvider: passthroughComponent,
285
+ SafeAreaView: hostComponent("SafeAreaView"),
286
+ useSafeAreaFrame: stubReturning(MOCK_SAFE_AREA_FRAME),
287
+ useSafeAreaInsets: stubReturning(MOCK_SAFE_AREA_INSETS),
288
+ withSafeAreaInsets: stubIdentity(),
289
+ };
290
+ };
291
+
292
+ const createNetInfoMock = (): ModuleExports => {
293
+ const netInfo = {
294
+ addEventListener: mock((listener?: (state: typeof MOCK_NET_INFO_STATE) => void) => {
295
+ listener?.(MOCK_NET_INFO_STATE);
296
+ return stub();
297
+ }),
298
+ configure: stub(),
299
+ fetch: stubResolving(MOCK_NET_INFO_STATE),
300
+ refresh: stubResolving(MOCK_NET_INFO_STATE),
301
+ useNetInfo: stubReturning(MOCK_NET_INFO_STATE),
302
+ };
303
+ return {
304
+ ...netInfo,
305
+ default: netInfo,
306
+ NetInfoStateType: {cellular: "cellular", none: "none", unknown: "unknown", wifi: "wifi"},
307
+ };
308
+ };
309
+
310
+ /**
311
+ * `react-native-actions-sheet` renders nothing until a native animation opens it. This stand-in
312
+ * tracks the imperative `show` / `hide` / `setModalVisible` calls instead, so opened content is
313
+ * assertable and closed content is absent.
314
+ */
315
+ const ActionSheet = forwardRef<unknown, {children?: ReactNode}>(
316
+ ({children}, ref): ReactElement | null => {
317
+ const [isVisible, setIsVisible] = useState(false);
318
+ useImperativeHandle(ref, () => ({
319
+ hide: (): void => setIsVisible(false),
320
+ setModalVisible: (visible?: boolean): void => setIsVisible(Boolean(visible)),
321
+ show: (): void => setIsVisible(true),
322
+ }));
323
+ if (!isVisible) {
324
+ return null;
325
+ }
326
+ return createElement("ActionSheet", null, children);
327
+ }
328
+ );
329
+
330
+ const createPresstoMock = (): ModuleExports => {
331
+ const pressable =
332
+ (name: string) =>
333
+ ({
334
+ enabled = true,
335
+ onPress,
336
+ ...props
337
+ }: MockComponentProps & {enabled?: boolean; onPress?: unknown}): ReactElement =>
338
+ createElement(
339
+ name,
340
+ {...props, disabled: !enabled, onPress: enabled ? onPress : undefined},
341
+ props.children
342
+ );
343
+ return {
344
+ PressableOpacity: pressable("PressableOpacity"),
345
+ PressableScale: pressable("PressableScale"),
346
+ PressableWithoutFeedback: pressable("PressableWithoutFeedback"),
347
+ };
348
+ };
349
+
350
+ /**
351
+ * Builds stand-ins for the native libraries most Expo apps depend on: Reanimated, Gesture
352
+ * Handler, SVG, safe-area-context, NetInfo, WebView, the community picker / slider /
353
+ * date-time picker, react-native-actions-sheet, pressto, and Sentry.
354
+ */
355
+ export const createLibraryModuleMocks = (): ModuleMockMap => {
356
+ const safeArea = createSafeAreaMock();
357
+ const Picker = Object.assign(hostComponent("Picker"), {Item: hostComponent("Picker.Item")});
358
+ const sentry = (): ModuleExports => ({
359
+ addBreadcrumb: stub(),
360
+ captureEvent: stubReturning("test-event-id"),
361
+ captureException: stubReturning("test-event-id"),
362
+ captureMessage: stubReturning("test-event-id"),
363
+ ErrorBoundary: passthroughComponent,
364
+ flush: stubResolving(true),
365
+ init: stub(),
366
+ reactNavigationIntegration: stubReturning({name: "ReactNavigation"}),
367
+ setContext: stub(),
368
+ setTag: stub(),
369
+ setUser: stub(),
370
+ withScope: mock((callback: (scope: Record<string, unknown>) => void) =>
371
+ callback({setContext: stub(), setExtra: stub(), setLevel: stub(), setTag: stub()})
372
+ ),
373
+ wrap: stubIdentity(),
374
+ });
375
+
376
+ return {
377
+ "@react-native-community/datetimepicker": () => ({
378
+ DateTimePickerAndroid: {
379
+ dismiss: stubResolving(undefined),
380
+ open: stubResolving({action: "dismissedAction"}),
381
+ },
382
+ default: hostComponent("DateTimePicker"),
383
+ }),
384
+ "@react-native-community/netinfo": createNetInfoMock,
385
+ "@react-native-community/slider": () => ({default: hostComponent("Slider")}),
386
+ "@react-native-picker/picker": () => ({Picker, PickerIOS: Picker}),
387
+ "@sentry/react-native": sentry,
388
+ pressto: createPresstoMock,
389
+ "react-native-actions-sheet": () => ({
390
+ __esModule: true,
391
+ default: ActionSheet,
392
+ SheetManager: {hide: stubResolving(undefined), show: stubResolving(undefined)},
393
+ }),
394
+ "react-native-gesture-handler": createGestureHandlerMock,
395
+ "react-native-reanimated": createReanimatedMock,
396
+ "react-native-safe-area-context": () => safeArea,
397
+ "react-native-safe-area-context/jest/mock": () => ({default: safeArea}),
398
+ "react-native-screens": () => ({
399
+ enableFreeze: stub(),
400
+ enableScreens: stub(),
401
+ Screen: hostComponent("Screen"),
402
+ ScreenContainer: hostComponent("ScreenContainer"),
403
+ ScreenStack: hostComponent("ScreenStack"),
404
+ }),
405
+ "react-native-svg": createSvgMock,
406
+ "react-native-webview": () => {
407
+ const WebView = hostComponent("WebView");
408
+ return {default: WebView, WebView};
409
+ },
410
+ };
411
+ };
@@ -0,0 +1,73 @@
1
+ import {type Mock, mock} from "bun:test";
2
+ import {createElement, Fragment, type ReactElement, type ReactNode} from "react";
3
+
4
+ /** Exports a `mock.module` factory returns. */
5
+ export type ModuleExports = Record<string, unknown>;
6
+
7
+ /** A `mock.module` factory: called by Bun when the mocked module is first imported. */
8
+ export type ModuleFactory = () => ModuleExports;
9
+
10
+ /** Map of module specifier to the factory that replaces it. */
11
+ export type ModuleMockMap = Record<string, ModuleFactory>;
12
+
13
+ /** Props the host-element stand-ins accept and forward. */
14
+ export type MockComponentProps = Record<string, unknown> & {children?: ReactNode};
15
+
16
+ /** A function component used as a stand-in for a native view. */
17
+ export type MockComponent = (props: MockComponentProps) => ReactElement;
18
+
19
+ /** Runtime platform the mocks report through `Platform.OS` and `EXPO_OS`. */
20
+ export type ExpoTestPlatform = "android" | "ios" | "web";
21
+
22
+ /** A `{remove}` subscription handle like the ones React Native listeners return. */
23
+ export interface MockSubscription {
24
+ remove: Mock<() => void>;
25
+ }
26
+
27
+ const noop = (): void => {};
28
+
29
+ /** A `bun:test` mock that does nothing and returns `undefined`. */
30
+ export const stub = (): Mock<(...args: unknown[]) => void> => mock(noop);
31
+
32
+ /** A `bun:test` mock that always returns `value`. */
33
+ export const stubReturning = <T>(value: T): Mock<(...args: unknown[]) => T> =>
34
+ mock((..._args: unknown[]): T => value);
35
+
36
+ /** A `bun:test` mock that always resolves to `value`. */
37
+ export const stubResolving = <T>(value: T): Mock<(...args: unknown[]) => Promise<T>> =>
38
+ mock((..._args: unknown[]): Promise<T> => Promise.resolve(value));
39
+
40
+ /** A `bun:test` mock that returns its first argument unchanged. */
41
+ export const stubIdentity = (): Mock<<T>(value: T, ...rest: unknown[]) => T> =>
42
+ mock(<T>(value: T, ..._rest: unknown[]): T => value);
43
+
44
+ /** A fresh `{remove}` subscription handle. */
45
+ const subscription = (): MockSubscription => ({remove: mock(noop)});
46
+
47
+ /** A `bun:test` mock that returns a fresh `{remove}` subscription on every call. */
48
+ export const stubSubscription = (): Mock<(...args: unknown[]) => MockSubscription> =>
49
+ mock((..._args: unknown[]): MockSubscription => subscription());
50
+
51
+ /**
52
+ * A component that renders a host element named `name` with every prop and child forwarded,
53
+ * so tests can find it by `testID`, inspect its props, and fire its handlers.
54
+ */
55
+ export const hostComponent = (name: string): MockComponent => {
56
+ const Component = ({children, ...props}: MockComponentProps): ReactElement =>
57
+ createElement(name, props, children);
58
+ Object.defineProperty(Component, "name", {value: name});
59
+ return Component;
60
+ };
61
+
62
+ /** A component that renders its children only (a provider or wrapper stand-in). */
63
+ export const passthroughComponent = ({children}: MockComponentProps): ReactElement =>
64
+ createElement(Fragment, null, children);
65
+
66
+ /** Builds a map of host components, one per name. */
67
+ export const hostComponents = (names: string[]): Record<string, MockComponent> => {
68
+ const components: Record<string, MockComponent> = {};
69
+ for (const name of names) {
70
+ components[name] = hostComponent(name);
71
+ }
72
+ return components;
73
+ };