Skip to content

Commit 0e9b5cf

Browse files
authored
feat(core): Allow to change native methods behavior (#14)
1 parent 755b98c commit 0e9b5cf

17 files changed

+319
-87
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"@babel/register": "^7.23.7",
3838
"@react-native/babel-preset": "^0.73.21",
3939
"babel-plugin-module-resolver": "^5.0.0",
40-
"dot-prop-immutable": "^2.1.1"
40+
"dot-prop-immutable": "^2.1.1",
41+
"ts-pattern": "^5.0.8"
4142
},
4243
"devDependencies": {
4344
"@assertive-ts/core": "^2.1.0",

register.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const start = Date.now();
2-
require("./dist/main");
2+
require("./dist/register");
33

44
const end = Date.now();
55
const diff = (end - start) / 1000;

src/helpers/mockComponent.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ import {
66
ReactNode,
77
createElement,
88
} from "react";
9+
import type { NativeMethods } from "react-native";
10+
11+
import type { ScrollViewMethods } from "../lib/Components/ScrollView";
12+
import type { TextInputMethods } from "../lib/Components/TextInput";
13+
14+
export type AllNativeMethods = NativeMethods | ScrollViewMethods | TextInputMethods;
915

1016
export function mockComponent<P, C extends ComponentClass<PropsWithChildren<P>>>(
1117
RealComponent: C,
12-
instanceMethods?: object | null,
18+
instanceMethods?: AllNativeMethods,
1319
): C {
1420
const SuperClass: ComponentClass<PropsWithChildren<P>> = typeof RealComponent === "function"
1521
? RealComponent
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import { NativeMethods } from "react-native";
2+
13
import { noop } from "./commons";
24

3-
export const MockNativeMethods = {
5+
export const nativeMethodsMock: NativeMethods = {
46
blur: noop,
57
focus: noop,
68
measure: noop,
79
measureInWindow: noop,
810
measureLayout: noop,
11+
refs: { },
912
setNativeProps: noop,
1013
};

src/lib/Components/ActivityIndicator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import { ActivityIndicator } from "react-native";
22

33
import { mockComponent } from "../../helpers/mockComponent";
44

5-
export const ActivityIndicatorMock = mockComponent(ActivityIndicator, null);
5+
export const ActivityIndicatorMock = mockComponent(ActivityIndicator);

src/lib/Components/Image.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@ import { Image } from "react-native";
44
import { noop } from "../../helpers/commons";
55
import { mockComponent } from "../../helpers/mockComponent";
66

7-
const Mock = mockComponent(Image as ComponentClass);
7+
export type ImageMethods = Partial<typeof Image>;
88

9-
export const ImageMock = Object.assign(Mock, {
9+
export const imageMethodsMock: ImageMethods = {
1010
getSize: noop,
1111
getSizeWithHeaders: noop,
12-
prefetch: noop,
13-
prefetchWithMetadata: noop,
14-
queryCache: noop,
15-
resolveAssetSource: noop,
16-
});
12+
prefetch: () => Promise.resolve(false),
13+
prefetchWithMetadata: () => Promise.resolve(false),
14+
queryCache: () => Promise.resolve({ }),
15+
resolveAssetSource: () => ({
16+
height: 0,
17+
scale: 0,
18+
uri: "",
19+
width: 0,
20+
}),
21+
};
22+
23+
const Mock = mockComponent(Image as ComponentClass);
24+
25+
export const ImageMock = Object.assign(Mock, imageMethodsMock);

src/lib/Components/ScrollView.ts

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,62 @@
11
/* eslint-disable sort-keys */
2-
import { PropsWithChildren, ReactNode, createElement } from "react";
3-
import { ScrollView, View, requireNativeComponent } from "react-native";
2+
import { ElementRef, PropsWithChildren, ReactNode, createElement } from "react";
3+
import { HostComponent, NativeMethods, ScrollView, View, requireNativeComponent } from "react-native";
44

55
import { noop } from "../../helpers/commons";
66
import { mockComponent } from "../../helpers/mockComponent";
7-
import { MockNativeMethods } from "../../helpers/mockNativeMethods";
7+
import { nativeMethodsMock } from "../../helpers/nativeMethodsMock";
88

9-
const RCTScrollView = requireNativeComponent("RCTScrollView");
10-
const BaseMock = mockComponent(ScrollView, {
11-
...MockNativeMethods,
12-
getScrollResponder: noop,
9+
export type ScrollViewMethods = NativeMethods | ScrollView & {
10+
getInnerViewRef: () => ElementRef<typeof View> | null;
11+
getNativeScrollRef: () => ElementRef<HostComponent<unknown>> | null;
12+
};
13+
14+
export const scrollViewMethodsMock: ScrollViewMethods = {
15+
...nativeMethodsMock,
16+
getScrollResponder: () => ({
17+
addListenerOn: noop,
18+
componentWillMount: noop,
19+
scrollResponderGetScrollableNode: noop,
20+
scrollResponderHandleMomentumScrollBegin: noop,
21+
scrollResponderHandleMomentumScrollEnd: noop,
22+
scrollResponderHandleResponderGrant: noop,
23+
scrollResponderHandleResponderReject: noop,
24+
scrollResponderHandleResponderRelease: noop,
25+
scrollResponderHandleScroll: noop,
26+
scrollResponderHandleScrollBeginDrag: noop,
27+
scrollResponderHandleScrollEndDrag: noop,
28+
scrollResponderHandleScrollShouldSetResponder: () => false,
29+
scrollResponderHandleStartShouldSetResponder: () => false,
30+
scrollResponderHandleStartShouldSetResponderCapture: () => false,
31+
scrollResponderHandleTerminationRequest: () => false,
32+
scrollResponderHandleTouchEnd: noop,
33+
scrollResponderHandleTouchMove: noop,
34+
scrollResponderHandleTouchStart: noop,
35+
scrollResponderInputMeasureAndScrollToKeyboard: noop,
36+
scrollResponderIsAnimating: () => false,
37+
scrollResponderKeyboardDidHide: noop,
38+
scrollResponderKeyboardDidShow: noop,
39+
scrollResponderKeyboardWillHide: noop,
40+
scrollResponderKeyboardWillShow: noop,
41+
scrollResponderScrollNativeHandleToKeyboard: noop,
42+
scrollResponderScrollTo: noop,
43+
scrollResponderTextInputFocusError: noop,
44+
scrollResponderZoomTo: noop,
45+
}),
1346
getScrollableNode: noop,
1447
getInnerViewNode: noop,
15-
getInnerViewRef: noop,
16-
getNativeScrollRef: noop,
48+
getInnerViewRef: () => null,
49+
getNativeScrollRef: () => null,
1750
scrollTo: noop,
1851
scrollToEnd: noop,
1952
flashScrollIndicators: noop,
2053
scrollResponderZoomTo: noop,
2154
scrollResponderScrollNativeHandleToKeyboard: noop,
22-
});
55+
56+
};
57+
58+
const RCTScrollView = requireNativeComponent("RCTScrollView");
59+
const BaseMock = mockComponent(ScrollView, scrollViewMethodsMock);
2360

2461
export class ScrollViewMock<P> extends BaseMock {
2562

src/lib/Components/Text.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Text } from "react-native";
22

33
import { mockComponent } from "../../helpers/mockComponent";
4-
import { MockNativeMethods } from "../../helpers/mockNativeMethods";
4+
import { nativeMethodsMock } from "../../helpers/nativeMethodsMock";
55

6-
export const TextMock = mockComponent(Text, MockNativeMethods);
6+
export const TextMock = mockComponent(Text, nativeMethodsMock);

src/lib/Components/TextInput.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
import { TextInput } from "react-native";
1+
import { ElementRef } from "react";
2+
import { HostComponent, NativeMethods, TextInput } from "react-native";
23

34
import { noop } from "../../helpers/commons";
45
import { mockComponent } from "../../helpers/mockComponent";
5-
import { MockNativeMethods } from "../../helpers/mockNativeMethods";
6+
import { nativeMethodsMock } from "../../helpers/nativeMethodsMock";
67

7-
export const TextInputMock = mockComponent(TextInput, {
8-
...MockNativeMethods,
8+
export type TextInputMethods = NativeMethods | TextInput & {
9+
getNativeRef: () => ElementRef<HostComponent<unknown>> | undefined;
10+
};
11+
12+
export const textInputMethodsMock: TextInputMethods = {
13+
...nativeMethodsMock,
914
clear: noop,
10-
getNativeRef: noop,
11-
isFocused: noop,
12-
});
15+
getNativeRef: () => undefined,
16+
isFocused: () => false,
17+
setSelection: noop,
18+
};
19+
20+
export const TextInputMock = mockComponent(TextInput, textInputMethodsMock);

src/lib/Components/View.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { View } from "react-native";
22

33
import { mockComponent } from "../../helpers/mockComponent";
4-
import { MockNativeMethods } from "../../helpers/mockNativeMethods";
4+
import { nativeMethodsMock } from "../../helpers/nativeMethodsMock";
55

6-
export const ViewMock = mockComponent(View, MockNativeMethods);
6+
export const ViewMock = mockComponent(View, nativeMethodsMock);

0 commit comments

Comments
 (0)