@momo-kits/foundation 0.169.4-beta.7 → 0.169.4-beta.8
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/Application/BottomSheet.tsx +11 -5
- package/Application/BottomTab/BottomTabBar.styles.ts +0 -3
- package/Application/BottomTab/BottomTabBar.tsx +12 -11
- package/Application/BottomTab/CurvedContainer.tsx +2 -8
- package/Application/BottomTab/CustomBottomTabItem.tsx +4 -9
- package/Application/BottomTab/DuoRailTabBar.tsx +3 -3
- package/Application/BottomTab/DuoTabBar.tsx +3 -15
- package/Application/BottomTab/TabBarIcon.tsx +0 -24
- package/Application/BottomTab/metrics.ts +3 -26
- package/Application/KitAdoption/AdoptionBadge.tsx +119 -0
- package/Application/KitAdoption/AdoptionProbe.tsx +145 -0
- package/Application/KitAdoption/AdoptionStore.ts +80 -0
- package/Application/KitAdoption/constants.ts +104 -0
- package/Application/KitAdoption/index.ts +5 -0
- package/Application/KitAdoption/styles.ts +55 -0
- package/Application/KitAdoption/types.ts +48 -0
- package/Application/KitAdoption/utils.ts +393 -0
- package/Application/ModalScreen.tsx +7 -1
- package/Application/NavigationContainer.tsx +2 -0
- package/Application/StackScreen.tsx +4 -1
- package/Application/__tests__/KitAdoption.integration.test.tsx +235 -0
- package/Application/__tests__/KitAdoption.test.ts +260 -0
- package/Application/__tests__/KitAdoptionStore.test.ts +110 -0
- package/Application/types.tabs.ts +1 -0
- package/Text/utils.ts +3 -8
- package/package.json +1 -1
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
jest.unmock('react-native');
|
|
2
|
+
|
|
3
|
+
/* Native packages are stubbed: the real modules are ESM or need native code under jest. */
|
|
4
|
+
jest.mock('react-native-reanimated', () => {
|
|
5
|
+
const { View, Text, Image } = require('react-native');
|
|
6
|
+
const Animated = {
|
|
7
|
+
View,
|
|
8
|
+
Text,
|
|
9
|
+
Image,
|
|
10
|
+
createAnimatedComponent: (component: unknown) => component,
|
|
11
|
+
};
|
|
12
|
+
return {
|
|
13
|
+
__esModule: true,
|
|
14
|
+
default: Animated,
|
|
15
|
+
...Animated,
|
|
16
|
+
useSharedValue: (value: unknown) => ({ value }),
|
|
17
|
+
useAnimatedStyle: () => ({}),
|
|
18
|
+
useDerivedValue: (fn: () => unknown) => ({ value: fn() }),
|
|
19
|
+
withTiming: (value: unknown) => value,
|
|
20
|
+
withSpring: (value: unknown) => value,
|
|
21
|
+
runOnJS: (fn: unknown) => fn,
|
|
22
|
+
interpolate: () => 0,
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
jest.mock('react-native-fast-image', () => {
|
|
26
|
+
const mockReact = require('react');
|
|
27
|
+
const FastImageStub = (props: Record<string, unknown>) =>
|
|
28
|
+
mockReact.createElement('FastImage', props);
|
|
29
|
+
return { __esModule: true, default: FastImageStub };
|
|
30
|
+
});
|
|
31
|
+
jest.mock('react-native-linear-gradient', () => {
|
|
32
|
+
const mockReact = require('react');
|
|
33
|
+
const LinearGradientStub = (props: Record<string, unknown>) =>
|
|
34
|
+
mockReact.createElement('LinearGradient', props);
|
|
35
|
+
return { __esModule: true, default: LinearGradientStub };
|
|
36
|
+
});
|
|
37
|
+
jest.mock('lottie-react-native', () => {
|
|
38
|
+
const mockReact = require('react');
|
|
39
|
+
const LottieStub = () => null;
|
|
40
|
+
return { __esModule: true, default: mockReact.forwardRef(LottieStub) };
|
|
41
|
+
});
|
|
42
|
+
jest.mock('../../Application', () => ({
|
|
43
|
+
useComponentId: () => ({ componentId: 'test-id' }),
|
|
44
|
+
}));
|
|
45
|
+
|
|
46
|
+
import React, { Fragment, type ElementType, type ReactNode } from 'react';
|
|
47
|
+
import {
|
|
48
|
+
FlatList,
|
|
49
|
+
Image,
|
|
50
|
+
Text as RNText,
|
|
51
|
+
TouchableOpacity,
|
|
52
|
+
View,
|
|
53
|
+
} from 'react-native';
|
|
54
|
+
import { act, create, type ReactTestRenderer } from 'react-test-renderer';
|
|
55
|
+
|
|
56
|
+
import { Button as FoundationButton } from '../../Button';
|
|
57
|
+
import { Text as FoundationText } from '../../Text';
|
|
58
|
+
import {
|
|
59
|
+
createRegistries,
|
|
60
|
+
getCurrentFiber,
|
|
61
|
+
walkScreen,
|
|
62
|
+
} from '../KitAdoption/utils';
|
|
63
|
+
import type { FiberLike, WalkResult } from '../KitAdoption/types';
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Production jsx of the React build. Elements made with it carry no
|
|
67
|
+
* `_debugStack`, the same as mini-app code shipped as a prod bundle. Loaded by
|
|
68
|
+
* resolved path: the react `exports` map blocks cjs/ under jest.
|
|
69
|
+
*/
|
|
70
|
+
const reactEntry = (
|
|
71
|
+
require as unknown as { resolve: (id: string) => string }
|
|
72
|
+
).resolve('react');
|
|
73
|
+
const prodJsxRuntime = require(reactEntry.replace(
|
|
74
|
+
/index\.js$/,
|
|
75
|
+
'cjs/react-jsx-runtime.production.js',
|
|
76
|
+
)) as {
|
|
77
|
+
jsx: (
|
|
78
|
+
type: ElementType,
|
|
79
|
+
props: Record<string, unknown>,
|
|
80
|
+
) => React.ReactElement;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** Builds an element with the prod jsx runtime, the way a mini-app prod bundle does. */
|
|
84
|
+
const prodJsx = (
|
|
85
|
+
type: ElementType,
|
|
86
|
+
props: Record<string, unknown>,
|
|
87
|
+
...children: ReactNode[]
|
|
88
|
+
): React.ReactElement => {
|
|
89
|
+
if (children.length === 0) return prodJsxRuntime.jsx(type, props);
|
|
90
|
+
return prodJsxRuntime.jsx(type, {
|
|
91
|
+
...props,
|
|
92
|
+
children: children.length > 1 ? children : children[0],
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** Test-only probe: exposes its fiber so the measurement starts from the real tree. */
|
|
97
|
+
class Probe extends React.Component<{ children?: ReactNode }> {
|
|
98
|
+
render(): ReactNode {
|
|
99
|
+
return this.props.children;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Kit registry built from the components this test renders. The full foundation
|
|
105
|
+
* index cannot load under jest (navigation and native-only packages), so it is
|
|
106
|
+
* not used. TouchableOpacity is listed as the foundation re-export, which must not count as kit.
|
|
107
|
+
*/
|
|
108
|
+
const registries = createRegistries(
|
|
109
|
+
[{ Button: FoundationButton, Text: FoundationText }, { TouchableOpacity }],
|
|
110
|
+
require('react-native') as Record<string, unknown>,
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
/** Renders the tree under a Probe and walks the probe's child, as AdoptionProbe does. */
|
|
114
|
+
const measure = (tree: ReactNode): WalkResult => {
|
|
115
|
+
let instance: Probe | null = null;
|
|
116
|
+
let renderer: ReactTestRenderer | undefined;
|
|
117
|
+
act(() => {
|
|
118
|
+
renderer = create(
|
|
119
|
+
<Probe
|
|
120
|
+
ref={(value: Probe | null) => {
|
|
121
|
+
instance = value;
|
|
122
|
+
}}
|
|
123
|
+
>
|
|
124
|
+
{tree}
|
|
125
|
+
</Probe>,
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
const fiber = (instance as unknown as { _reactInternals: FiberLike })
|
|
129
|
+
._reactInternals;
|
|
130
|
+
const root = getCurrentFiber(fiber).child;
|
|
131
|
+
if (!root) throw new Error('probe rendered no child fiber');
|
|
132
|
+
const result = walkScreen(root, registries);
|
|
133
|
+
act(() => {
|
|
134
|
+
renderer?.unmount();
|
|
135
|
+
});
|
|
136
|
+
return result;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/** Shorthand for the K/R/O triple the demo screen prints. */
|
|
140
|
+
const kro = (result: WalkResult): string =>
|
|
141
|
+
`K${result.counts.K} R${result.counts.R} O${result.counts.O}`;
|
|
142
|
+
|
|
143
|
+
describe('KitAdoption integration (prod-built app tree over dev host)', () => {
|
|
144
|
+
it('a. kit-only tree: kit components counted, their internals not', () => {
|
|
145
|
+
const result = measure(
|
|
146
|
+
prodJsx(
|
|
147
|
+
View,
|
|
148
|
+
{},
|
|
149
|
+
prodJsx(FoundationText, {}, 'a'),
|
|
150
|
+
prodJsx(FoundationButton, { title: 'b' }),
|
|
151
|
+
),
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
expect(kro(result)).toBe('K2 R1 O0');
|
|
155
|
+
expect(result.status).toBe('ok');
|
|
156
|
+
expect(result.kit).toEqual({ Text: 1, Button: 1 });
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('b. app wrapper is transparent, each use of the kit inside it is counted', () => {
|
|
160
|
+
// Two buttons are siblings, so they sit under one dev root, like a real screen.
|
|
161
|
+
// The probe walks a single root and not its siblings (see walkScreen).
|
|
162
|
+
function MyButton(props: { title: string }): React.ReactElement {
|
|
163
|
+
return prodJsx(FoundationButton, props);
|
|
164
|
+
}
|
|
165
|
+
function Screen(): React.ReactElement {
|
|
166
|
+
return prodJsx(
|
|
167
|
+
Fragment,
|
|
168
|
+
{},
|
|
169
|
+
prodJsx(MyButton, { title: '1' }),
|
|
170
|
+
prodJsx(MyButton, { title: '2' }),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
const result = measure(<Screen />);
|
|
174
|
+
|
|
175
|
+
expect(result.counts.K).toBe(2);
|
|
176
|
+
expect(kro(result)).toBe('K2 R0 O0');
|
|
177
|
+
expect(result.top_transparent).toEqual([{ name: 'MyButton', count: 2 }]);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('c. raw react-native primitives written by the app count as R', () => {
|
|
181
|
+
const result = measure(
|
|
182
|
+
prodJsx(
|
|
183
|
+
View,
|
|
184
|
+
{},
|
|
185
|
+
prodJsx(RNText, {}, 'x'),
|
|
186
|
+
prodJsx(Image, { source: { uri: 'https://example.com/a.png' } }),
|
|
187
|
+
prodJsx(TouchableOpacity, {}),
|
|
188
|
+
),
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
expect(kro(result)).toBe('K0 R4 O0');
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('d. app child in a kit container counts K, the container internals do not', () => {
|
|
195
|
+
const result = measure(
|
|
196
|
+
prodJsx(FoundationText, {}, prodJsx(FoundationText, {}, 'child')),
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
expect(kro(result)).toBe('K2 R0 O0');
|
|
200
|
+
expect(result.kit).toEqual({ Text: 2 });
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('e. FlatList counts R once, each renderItem element counts K', () => {
|
|
204
|
+
const items = ['one', 'two', 'three'];
|
|
205
|
+
const result = measure(
|
|
206
|
+
prodJsx(FlatList, {
|
|
207
|
+
data: items,
|
|
208
|
+
keyExtractor: (item: string) => item,
|
|
209
|
+
renderItem: ({ item }: { item: string }) =>
|
|
210
|
+
prodJsx(FoundationText, {}, item),
|
|
211
|
+
}),
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
expect(kro(result)).toBe('K3 R1 O0');
|
|
215
|
+
expect(result.rn).toEqual({ FlatList: 1 });
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('f. foundation re-export of TouchableOpacity counts as R, not K', () => {
|
|
219
|
+
const result = measure(prodJsx(TouchableOpacity, {}, 'x'));
|
|
220
|
+
|
|
221
|
+
expect(kro(result)).toBe('K0 R1 O0');
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('g. an all-dev tree is reported as dev_bundle', () => {
|
|
225
|
+
const result = measure(
|
|
226
|
+
<View>
|
|
227
|
+
<RNText>x</RNText>
|
|
228
|
+
</View>,
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
expect(result.status).toBe('dev_bundle');
|
|
232
|
+
expect(result.counts).toEqual({ K: 0, R: 0, O: 0 });
|
|
233
|
+
expect(result.fibers).toBeGreaterThan(0);
|
|
234
|
+
});
|
|
235
|
+
});
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { createElement, Fragment } from 'react';
|
|
2
|
+
|
|
3
|
+
import { FIBER_TAG, REACT_MEMO_TYPE } from '../KitAdoption/constants';
|
|
4
|
+
import type { FiberLike } from '../KitAdoption/types';
|
|
5
|
+
import {
|
|
6
|
+
createRegistries,
|
|
7
|
+
getCurrentFiber,
|
|
8
|
+
walkScreen,
|
|
9
|
+
} from '../KitAdoption/utils';
|
|
10
|
+
|
|
11
|
+
const STACK = 'dev-stack';
|
|
12
|
+
|
|
13
|
+
const Button = () => null;
|
|
14
|
+
const Card = () => null;
|
|
15
|
+
const Chip = () => null;
|
|
16
|
+
const MyButton = () => null;
|
|
17
|
+
const Plain = () => null;
|
|
18
|
+
const RNView = () => null;
|
|
19
|
+
const Text = () => null;
|
|
20
|
+
const TouchableOpacity = () => null;
|
|
21
|
+
|
|
22
|
+
const makeFiber = (overrides: Partial<FiberLike> = {}): FiberLike => ({
|
|
23
|
+
tag: FIBER_TAG.FunctionComponent,
|
|
24
|
+
type: undefined,
|
|
25
|
+
child: null,
|
|
26
|
+
sibling: null,
|
|
27
|
+
return: null,
|
|
28
|
+
alternate: null,
|
|
29
|
+
memoizedProps: {},
|
|
30
|
+
...overrides,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
/** Sets children of parent (child, sibling, return links) and returns parent. */
|
|
34
|
+
const link = (parent: FiberLike, children: FiberLike[]): FiberLike => {
|
|
35
|
+
children.forEach((child, index) => {
|
|
36
|
+
child.return = parent;
|
|
37
|
+
child.sibling = children[index + 1] ?? null;
|
|
38
|
+
});
|
|
39
|
+
parent.child = children[0] ?? null;
|
|
40
|
+
return parent;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** Linear chain of plain app fibers, returned from the top. */
|
|
44
|
+
const buildChain = (length: number): FiberLike => {
|
|
45
|
+
let head = makeFiber({ type: Plain });
|
|
46
|
+
for (let i = 1; i < length; i += 1) {
|
|
47
|
+
head = link(makeFiber({ type: Plain }), [head]);
|
|
48
|
+
}
|
|
49
|
+
return head;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
describe('KitAdoption walkScreen', () => {
|
|
53
|
+
it('counts a kit component once and skips its host-internal fibers', () => {
|
|
54
|
+
const internalView = makeFiber({
|
|
55
|
+
tag: FIBER_TAG.HostComponent,
|
|
56
|
+
type: 'RCTView',
|
|
57
|
+
_debugStack: STACK,
|
|
58
|
+
});
|
|
59
|
+
const button = link(makeFiber({ type: Button }), [internalView]);
|
|
60
|
+
const myButton = link(makeFiber({ type: MyButton }), [button]);
|
|
61
|
+
|
|
62
|
+
const result = walkScreen(
|
|
63
|
+
myButton,
|
|
64
|
+
createRegistries([{ Button }], { View: RNView }),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
expect(result.counts.K).toBe(1);
|
|
68
|
+
expect(result.counts.R).toBe(0);
|
|
69
|
+
expect(result.kit.Button).toBe(1);
|
|
70
|
+
expect(result.top_transparent).toEqual([{ name: 'MyButton', count: 1 }]);
|
|
71
|
+
expect(result.status).toBe('ok');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('counts a raw RN View written by the app as R', () => {
|
|
75
|
+
const result = walkScreen(
|
|
76
|
+
makeFiber({ type: RNView }),
|
|
77
|
+
createRegistries([{ Button }], { View: RNView }),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
expect(result.counts.R).toBe(1);
|
|
81
|
+
expect(result.counts.K).toBe(0);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('counts an app Text inside a kit Card as K and ignores the Card host view', () => {
|
|
85
|
+
const cardInternalView = makeFiber({
|
|
86
|
+
tag: FIBER_TAG.HostComponent,
|
|
87
|
+
type: 'RCTView',
|
|
88
|
+
_debugStack: STACK,
|
|
89
|
+
});
|
|
90
|
+
const appText = makeFiber({ type: Text });
|
|
91
|
+
const card = link(makeFiber({ type: Card }), [cardInternalView, appText]);
|
|
92
|
+
|
|
93
|
+
const result = walkScreen(
|
|
94
|
+
card,
|
|
95
|
+
createRegistries([{ Card, Text: Text }], { Text: Text }),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
expect(result.counts.K).toBe(2);
|
|
99
|
+
expect(result.counts.R).toBe(0);
|
|
100
|
+
expect(result.kit.Card).toBe(1);
|
|
101
|
+
expect(result.kit.Text).toBe(1);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('counts a lib unit once and only the slot children the app passed in', () => {
|
|
105
|
+
const slotElement = createElement(Button, { label: 'slot' });
|
|
106
|
+
const internalButton = makeFiber({ type: Button, memoizedProps: {} });
|
|
107
|
+
const slotFiber = makeFiber({
|
|
108
|
+
type: Button,
|
|
109
|
+
memoizedProps: slotElement.props,
|
|
110
|
+
});
|
|
111
|
+
const chip = link(
|
|
112
|
+
makeFiber({ type: Chip, memoizedProps: { children: slotElement } }),
|
|
113
|
+
[internalButton, slotFiber],
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const result = walkScreen(chip, createRegistries([{ Button }], {}));
|
|
117
|
+
|
|
118
|
+
expect(result.counts.K).toBe(2);
|
|
119
|
+
expect(result.kit.Chip).toBe(1);
|
|
120
|
+
expect(result.kit.Button).toBe(1);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('treats the children of a Fragment passed into a lib unit as slots', () => {
|
|
124
|
+
const first = createElement(Button, { label: 'first' });
|
|
125
|
+
const second = createElement(Button, { label: 'second' });
|
|
126
|
+
const fragment = createElement(Fragment, null, first, second);
|
|
127
|
+
const chip = link(
|
|
128
|
+
makeFiber({ type: Chip, memoizedProps: { children: fragment } }),
|
|
129
|
+
[
|
|
130
|
+
makeFiber({ type: Button, memoizedProps: {} }),
|
|
131
|
+
makeFiber({ type: Button, memoizedProps: first.props }),
|
|
132
|
+
makeFiber({ type: Button, memoizedProps: second.props }),
|
|
133
|
+
],
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const result = walkScreen(chip, createRegistries([{ Button }], {}));
|
|
137
|
+
|
|
138
|
+
expect(result.kit.Button).toBe(2);
|
|
139
|
+
expect(result.counts.K).toBe(3);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('does not count the TouchableOpacity re-export as kit', () => {
|
|
143
|
+
const registries = createRegistries([{ TouchableOpacity }], {
|
|
144
|
+
TouchableOpacity,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const result = walkScreen(
|
|
148
|
+
makeFiber({ type: TouchableOpacity }),
|
|
149
|
+
registries,
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
expect(result.counts.K).toBe(0);
|
|
153
|
+
expect(result.counts.R).toBe(1);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('reports truncated when the walk exceeds the fiber limit', () => {
|
|
157
|
+
const result = walkScreen(
|
|
158
|
+
buildChain(10),
|
|
159
|
+
createRegistries([], {}),
|
|
160
|
+
undefined,
|
|
161
|
+
5,
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
expect(result.truncated).toBe(true);
|
|
165
|
+
expect(result.status).toBe('truncated');
|
|
166
|
+
expect(result.fibers).toBe(5);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('reports dev_bundle when every fiber carries a dev stack', () => {
|
|
170
|
+
const child = makeFiber({
|
|
171
|
+
tag: FIBER_TAG.HostComponent,
|
|
172
|
+
type: 'RCTView',
|
|
173
|
+
_debugStack: STACK,
|
|
174
|
+
});
|
|
175
|
+
const root = link(
|
|
176
|
+
makeFiber({
|
|
177
|
+
tag: FIBER_TAG.HostComponent,
|
|
178
|
+
type: 'RCTView',
|
|
179
|
+
_debugStack: STACK,
|
|
180
|
+
}),
|
|
181
|
+
[child],
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
const result = walkScreen(root, createRegistries([{ Button }], {}));
|
|
185
|
+
|
|
186
|
+
expect(result.status).toBe('dev_bundle');
|
|
187
|
+
expect(result.counts).toEqual({ K: 0, R: 0, O: 0 });
|
|
188
|
+
expect(result.fibers).toBe(2);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('does not count the inner fiber of a host-created memo as app code', () => {
|
|
192
|
+
const innerView = makeFiber({ type: RNView });
|
|
193
|
+
const hostMemo = link(
|
|
194
|
+
makeFiber({
|
|
195
|
+
tag: FIBER_TAG.MemoComponent,
|
|
196
|
+
type: { $$typeof: REACT_MEMO_TYPE, type: RNView },
|
|
197
|
+
_debugStack: STACK,
|
|
198
|
+
}),
|
|
199
|
+
[innerView],
|
|
200
|
+
);
|
|
201
|
+
const hostKit = link(
|
|
202
|
+
makeFiber({ type: Button, _debugStack: STACK }),
|
|
203
|
+
[hostMemo],
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
const result = walkScreen(
|
|
207
|
+
hostKit,
|
|
208
|
+
createRegistries([{ Button }], { View: RNView }),
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
expect(result.counts.R).toBe(0);
|
|
212
|
+
expect(result.counts.K).toBe(0);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('counts an app-authored memo wrapping a kit component exactly once', () => {
|
|
216
|
+
const innerButton = makeFiber({ type: Button });
|
|
217
|
+
const appMemo = link(
|
|
218
|
+
makeFiber({
|
|
219
|
+
tag: FIBER_TAG.MemoComponent,
|
|
220
|
+
type: { $$typeof: REACT_MEMO_TYPE, type: Button },
|
|
221
|
+
}),
|
|
222
|
+
[innerButton],
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
const result = walkScreen(
|
|
226
|
+
appMemo,
|
|
227
|
+
createRegistries([{ Button }], {}),
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
expect(result.counts.K).toBe(1);
|
|
231
|
+
expect(result.kit.Button).toBe(1);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
describe('KitAdoption getCurrentFiber', () => {
|
|
236
|
+
it('returns the alternate when the fiber hangs off a stale HostRoot', () => {
|
|
237
|
+
const alternate = makeFiber({ type: Plain });
|
|
238
|
+
const fiber = makeFiber({ type: Plain, alternate });
|
|
239
|
+
const committedRoot = makeFiber({ tag: FIBER_TAG.HostRoot });
|
|
240
|
+
link(
|
|
241
|
+
makeFiber({
|
|
242
|
+
tag: FIBER_TAG.HostRoot,
|
|
243
|
+
stateNode: { current: committedRoot },
|
|
244
|
+
}),
|
|
245
|
+
[fiber],
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
expect(getCurrentFiber(fiber)).toBe(alternate);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it('returns the fiber itself when its HostRoot is the committed one', () => {
|
|
252
|
+
const alternate = makeFiber({ type: Plain });
|
|
253
|
+
const fiber = makeFiber({ type: Plain, alternate });
|
|
254
|
+
const hostRoot = makeFiber({ tag: FIBER_TAG.HostRoot });
|
|
255
|
+
link(hostRoot, [fiber]);
|
|
256
|
+
hostRoot.stateNode = { current: hostRoot };
|
|
257
|
+
|
|
258
|
+
expect(getCurrentFiber(fiber)).toBe(fiber);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { AdoptionStore } from '../KitAdoption/AdoptionStore';
|
|
2
|
+
import type { ScreenSnapshot } from '../KitAdoption/types';
|
|
3
|
+
|
|
4
|
+
/** Snapshot with the given K/R/O counts for one app and screen. */
|
|
5
|
+
const makeSnapshot = (
|
|
6
|
+
screen: string,
|
|
7
|
+
counts: { K: number; R: number; O: number },
|
|
8
|
+
appId = 'app-1',
|
|
9
|
+
): ScreenSnapshot => ({
|
|
10
|
+
counts,
|
|
11
|
+
kit: {},
|
|
12
|
+
rn: {},
|
|
13
|
+
other: {},
|
|
14
|
+
top_transparent: [],
|
|
15
|
+
fibers: 0,
|
|
16
|
+
truncated: false,
|
|
17
|
+
status: 'ok',
|
|
18
|
+
app_id: appId,
|
|
19
|
+
screen_name: screen,
|
|
20
|
+
platform: 'ios',
|
|
21
|
+
ts: 0,
|
|
22
|
+
walk_ms: 0,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('AdoptionStore save', () => {
|
|
26
|
+
it('keeps the existing snapshot when it has more nodes', () => {
|
|
27
|
+
const store = new AdoptionStore();
|
|
28
|
+
store.save(makeSnapshot('Home', { K: 3, R: 0, O: 0 }));
|
|
29
|
+
store.save(makeSnapshot('Home', { K: 1, R: 0, O: 0 }));
|
|
30
|
+
|
|
31
|
+
expect(store.getAll()).toHaveLength(1);
|
|
32
|
+
expect(store.getAll()[0].counts.K).toBe(3);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('replaces the existing snapshot when the new one has more nodes', () => {
|
|
36
|
+
const store = new AdoptionStore();
|
|
37
|
+
store.save(makeSnapshot('Home', { K: 1, R: 0, O: 0 }));
|
|
38
|
+
store.save(makeSnapshot('Home', { K: 2, R: 2, O: 1 }));
|
|
39
|
+
|
|
40
|
+
expect(store.getAll()[0].counts).toEqual({ K: 2, R: 2, O: 1 });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('keys snapshots by app and screen', () => {
|
|
44
|
+
const store = new AdoptionStore();
|
|
45
|
+
store.save(makeSnapshot('Home', { K: 1, R: 0, O: 0 }, 'app-1'));
|
|
46
|
+
store.save(makeSnapshot('Home', { K: 1, R: 0, O: 0 }, 'app-2'));
|
|
47
|
+
|
|
48
|
+
expect(store.getAll()).toHaveLength(2);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('getLatest returns the most recently saved screen', () => {
|
|
52
|
+
const store = new AdoptionStore();
|
|
53
|
+
store.save(makeSnapshot('Home', { K: 1, R: 0, O: 0 }));
|
|
54
|
+
store.save(makeSnapshot('Detail', { K: 1, R: 0, O: 0 }));
|
|
55
|
+
|
|
56
|
+
expect(store.getLatest()?.screen_name).toBe('Detail');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('clear empties the store and getLatest', () => {
|
|
60
|
+
const store = new AdoptionStore();
|
|
61
|
+
store.save(makeSnapshot('Home', { K: 1, R: 0, O: 0 }));
|
|
62
|
+
store.clear();
|
|
63
|
+
|
|
64
|
+
expect(store.getAll()).toEqual([]);
|
|
65
|
+
expect(store.getLatest()).toBeUndefined();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('AdoptionStore subscribe', () => {
|
|
70
|
+
it('notifies listeners on save and stops after unsubscribe', () => {
|
|
71
|
+
const store = new AdoptionStore();
|
|
72
|
+
const listener = jest.fn();
|
|
73
|
+
const unsubscribe = store.subscribe(listener);
|
|
74
|
+
|
|
75
|
+
store.save(makeSnapshot('Home', { K: 1, R: 0, O: 0 }));
|
|
76
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
77
|
+
|
|
78
|
+
unsubscribe();
|
|
79
|
+
store.save(makeSnapshot('Detail', { K: 1, R: 0, O: 0 }));
|
|
80
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('AdoptionStore active measurer', () => {
|
|
85
|
+
it('remeasureActive calls the active measurer', () => {
|
|
86
|
+
const store = new AdoptionStore();
|
|
87
|
+
const measure = jest.fn();
|
|
88
|
+
store.setActive(measure);
|
|
89
|
+
|
|
90
|
+
store.remeasureActive();
|
|
91
|
+
|
|
92
|
+
expect(measure).toHaveBeenCalledTimes(1);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('clearActive only clears the measurer that is still active', () => {
|
|
96
|
+
const store = new AdoptionStore();
|
|
97
|
+
const first = jest.fn();
|
|
98
|
+
const second = jest.fn();
|
|
99
|
+
store.setActive(first);
|
|
100
|
+
store.setActive(second);
|
|
101
|
+
|
|
102
|
+
store.clearActive(first);
|
|
103
|
+
store.remeasureActive();
|
|
104
|
+
expect(second).toHaveBeenCalledTimes(1);
|
|
105
|
+
|
|
106
|
+
store.clearActive(second);
|
|
107
|
+
store.remeasureActive();
|
|
108
|
+
expect(second).toHaveBeenCalledTimes(1);
|
|
109
|
+
});
|
|
110
|
+
});
|
package/Text/utils.ts
CHANGED
|
@@ -9,18 +9,13 @@ const MAX_DEVICE_SCALE = 5;
|
|
|
9
9
|
const useScaleSize = (size: number, scaleRate?: number) => {
|
|
10
10
|
const { width } = useWindowDimensions();
|
|
11
11
|
const context = useContext<any>(MiniAppContext);
|
|
12
|
-
// opt-in ceiling: when set (arg or context), the final size never exceeds size × capRate,
|
|
13
|
-
// in both the mini-app userScaleRate path and the OS path. Undefined keeps the old behavior.
|
|
14
|
-
const capRate = scaleRate ?? context?.fontScale?.maxScaleRate;
|
|
15
|
-
const cap = (scaled: number) =>
|
|
16
|
-
capRate != null ? Math.min(scaled, size * capRate) : scaled;
|
|
17
12
|
if (context?.fontScale?.useOSScaleRate === false) {
|
|
18
|
-
return
|
|
13
|
+
return size * (context.fontScale.userScaleRate ?? 1);
|
|
19
14
|
}
|
|
20
15
|
const fontScale = PixelRatio.getFontScale();
|
|
21
16
|
const deviceScale = width / DEFAULT_SCREEN_SIZE;
|
|
22
17
|
|
|
23
|
-
const maxScaleRate =
|
|
18
|
+
const maxScaleRate = scaleRate ?? MAX_FONT_SCALE;
|
|
24
19
|
|
|
25
20
|
let fontSizeDeviceScale = size;
|
|
26
21
|
let fontSizeOSScale = size;
|
|
@@ -39,7 +34,7 @@ const useScaleSize = (size: number, scaleRate?: number) => {
|
|
|
39
34
|
);
|
|
40
35
|
}
|
|
41
36
|
|
|
42
|
-
return
|
|
37
|
+
return Math.max(fontSizeDeviceScale, fontSizeOSScale);
|
|
43
38
|
};
|
|
44
39
|
|
|
45
40
|
const scaleSize = (size: number) => {
|