-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathtestUtils.tsx
More file actions
151 lines (132 loc) · 4.47 KB
/
testUtils.tsx
File metadata and controls
151 lines (132 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { act, render } from "@testing-library/react-native";
import React from "react";
import { View } from "react-native";
import type { LayoutChangeEvent } from "react-native";
import type {
FocusedInputLayoutChangedEvent,
FocusedInputSelectionChangedEvent,
NativeEvent,
} from "react-native-keyboard-controller";
import type { SharedValue } from "react-native-reanimated";
// ---------------------------------------------------------------------------
// Constants (derived from real device logs)
// ---------------------------------------------------------------------------
export const MOCK_SCREEN_HEIGHT = 928;
export const KEYBOARD_HEIGHT = 312;
export const BOTTOM_OFFSET = 62;
export const MOCK_SV_TARGET = 1469;
export const INPUT_TARGET_A = 1373;
export const INPUT_TARGET_B = 1395;
export const INPUT_LAYOUT_A = {
absoluteY: 281.67,
height: 60.33,
y: 165.67,
x: 0,
absoluteX: 16,
width: 394.67,
};
export const INPUT_LAYOUT_B = {
absoluteY: 695.67,
height: 60.33,
y: 579.67,
x: 0,
absoluteX: 16,
width: 121,
};
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export type KeyboardHandlers = {
onStart: (e: NativeEvent) => void;
onMove: (e: NativeEvent) => void;
onEnd: (e: NativeEvent) => void;
};
export type SelectionHandler = (e: FocusedInputSelectionChangedEvent) => void;
// ---------------------------------------------------------------------------
// Mock state
// ---------------------------------------------------------------------------
export const mockScrollTo = jest.fn();
export const mockInput = {
value: null,
} as SharedValue<FocusedInputLayoutChangedEvent | null>;
export const mockOffset = { value: 0 } as SharedValue<number>;
export const mockLayout = {
value: { width: 390, height: 812 },
} as SharedValue<{ width: number; height: number }>;
export const mockSize = {
value: { width: 390, height: 2000 },
} as SharedValue<{ width: number; height: number }>;
// ---------------------------------------------------------------------------
// Captured handlers (populated on each render)
// ---------------------------------------------------------------------------
export const mockKeyboardHandlers: { current: KeyboardHandlers } = {
current: undefined as unknown as KeyboardHandlers,
};
export const mockSelectionHandler: { current: SelectionHandler } = {
current: undefined as unknown as SelectionHandler,
};
export const mockCapturedOnLayout: {
current: ((e: LayoutChangeEvent) => void) | null;
} = { current: null };
// ---------------------------------------------------------------------------
// Re-export shared utilities
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
export const reset = () => {
mockScrollTo.mockClear();
mockOffset.value = 0;
mockLayout.value = { width: 390, height: 812 };
mockSize.value = { width: 390, height: 2000 };
mockInput.value = null;
mockCapturedOnLayout.current = null;
};
export const inputEvent = (
target: number,
layout: typeof INPUT_LAYOUT_A,
): FocusedInputLayoutChangedEvent => ({
target,
parentScrollViewTarget: MOCK_SV_TARGET,
layout: { ...layout },
});
export const selectionEvent = (
target: number,
endY = 47,
endPosition = 0,
): FocusedInputSelectionChangedEvent => ({
target,
selection: {
start: { x: 0, y: endY, position: endPosition },
end: { x: 0, y: endY, position: endPosition },
},
});
export const kbEvent = (height: number, target: number): NativeEvent => ({
height,
target,
duration: 285,
progress: KEYBOARD_HEIGHT > 0 ? height / KEYBOARD_HEIGHT : 0,
});
export const renderKeyboardAwareScrollView = async (
bottomOffset = BOTTOM_OFFSET,
) => {
const KeyboardAwareScrollView = require("../index").default;
render(
<KeyboardAwareScrollView bottomOffset={bottomOffset}>
<View />
</KeyboardAwareScrollView>,
);
await act(async () => {
mockCapturedOnLayout.current?.({
nativeEvent: { layout: { x: 0, y: 0, width: 390, height: 812 } },
} as LayoutChangeEvent);
});
};
export const lastScrollToY = (): number | undefined => {
const calls = mockScrollTo.mock.calls;
if (calls.length === 0) {
return undefined;
}
// scrollTo(ref, x, y, animated)
return calls[calls.length - 1][2] as number;
};