@momo-kits/foundation 0.169.16-beta.19 → 0.169.16-beta.20
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/Icon/index.tsx +2 -0
- package/Image/__tests__/utils.test.ts +90 -0
- package/Image/index.tsx +19 -2
- package/Image/types.ts +6 -0
- package/Image/useWebpSource.ts +52 -0
- package/Image/utils.ts +61 -0
- package/Input/ErrorView.tsx +1 -1
- package/Input/InputMoneyView.tsx +14 -6
- package/package.json +1 -1
- package/Input/__tests__/InputMoney.matrix.test.tsx +0 -340
- package/Input/__tests__/InputMoney.test.tsx +0 -270
package/Icon/index.tsx
CHANGED
|
@@ -10,6 +10,7 @@ import { Colors } from '../Consts';
|
|
|
10
10
|
/**
|
|
11
11
|
* Tinted icon from the bundled icon map (unknown names fall back to
|
|
12
12
|
* `ic_warning`) or from a URL. `color={null}` keeps the image's own colors.
|
|
13
|
+
* URLs never switch to WebP, matching the Compose/SwiftUI Icon.
|
|
13
14
|
*/
|
|
14
15
|
const Icon: React.FC<IconProps> = ({
|
|
15
16
|
source = 'ic_back',
|
|
@@ -38,6 +39,7 @@ const Icon: React.FC<IconProps> = ({
|
|
|
38
39
|
<Image
|
|
39
40
|
{...props}
|
|
40
41
|
loading={false}
|
|
42
|
+
webp={false}
|
|
41
43
|
source={getIconSource()}
|
|
42
44
|
style={[
|
|
43
45
|
style,
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { WEBP_ACCEPT, getWebpSource, isWebpCandidate } from '../utils';
|
|
2
|
+
|
|
3
|
+
const PNG = 'https://static.momocdn.net/app/img/kits/trustBanner/pci_dss.png';
|
|
4
|
+
const IMG_HOST_PNG = 'https://img.mservice.com.vn/app/img/kits/01.MoMo.png';
|
|
5
|
+
|
|
6
|
+
describe('isWebpCandidate', () => {
|
|
7
|
+
it.each([
|
|
8
|
+
PNG,
|
|
9
|
+
IMG_HOST_PNG,
|
|
10
|
+
'http://static.momocdn.net/a.jpg',
|
|
11
|
+
'https://STATIC.MOMOCDN.NET/a.jpg',
|
|
12
|
+
'https://static.momocdn.net:443/a.jpg',
|
|
13
|
+
'https://static.momocdn.net',
|
|
14
|
+
`${PNG}?w=100&h=50`,
|
|
15
|
+
`${PNG}#top`,
|
|
16
|
+
`${PNG}?imageformat=jpg`,
|
|
17
|
+
'https://static.momocdn.net/format=jpg/a.png',
|
|
18
|
+
])('accepts %s', uri => {
|
|
19
|
+
expect(isWebpCandidate(uri)).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it.each([
|
|
23
|
+
'https://static.momocdn.net/a.webp',
|
|
24
|
+
'https://static.momocdn.net/a.GIF',
|
|
25
|
+
'https://img.mservice.com.vn/a.Svg',
|
|
26
|
+
'https://static.momocdn.net/a.webp?v=2',
|
|
27
|
+
'https://static.momocdn.net/a.gif#x',
|
|
28
|
+
`${PNG}?format=jpg`,
|
|
29
|
+
`${PNG}?w=1&format=png`,
|
|
30
|
+
`${PNG}?format`,
|
|
31
|
+
'https://example.com/a.png',
|
|
32
|
+
'https://cdn.static.momocdn.net/a.png',
|
|
33
|
+
'https://static.momocdn.net.evil.com/a.png',
|
|
34
|
+
'file:///data/a.png',
|
|
35
|
+
'static.momocdn.net/a.png',
|
|
36
|
+
'data:image/png;base64,AAAA',
|
|
37
|
+
'',
|
|
38
|
+
])('rejects %s', uri => {
|
|
39
|
+
expect(isWebpCandidate(uri)).toBe(false);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('getWebpSource', () => {
|
|
44
|
+
it('adds the WebP Accept header and keeps the uri', () => {
|
|
45
|
+
expect(getWebpSource({ uri: IMG_HOST_PNG })).toEqual({
|
|
46
|
+
uri: IMG_HOST_PNG,
|
|
47
|
+
headers: { Accept: WEBP_ACCEPT },
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('keeps the query, fragment and other source fields', () => {
|
|
52
|
+
const source = { uri: `${PNG}?w=1#top`, priority: 'high', cache: 'web' };
|
|
53
|
+
expect(getWebpSource(source)).toEqual({
|
|
54
|
+
...source,
|
|
55
|
+
headers: { Accept: WEBP_ACCEPT },
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('keeps the existing headers', () => {
|
|
60
|
+
const source = { uri: PNG, headers: { Authorization: 'token' } };
|
|
61
|
+
expect(getWebpSource(source)).toEqual({
|
|
62
|
+
uri: PNG,
|
|
63
|
+
headers: { Authorization: 'token', Accept: WEBP_ACCEPT },
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('does not mutate the given source', () => {
|
|
68
|
+
const source = { uri: PNG, headers: { Authorization: 'token' } };
|
|
69
|
+
getWebpSource(source);
|
|
70
|
+
expect(source).toEqual({ uri: PNG, headers: { Authorization: 'token' } });
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it.each(['Accept', 'accept', 'ACCEPT'])(
|
|
74
|
+
'leaves a source with its own %s header alone',
|
|
75
|
+
name => {
|
|
76
|
+
const source = { uri: PNG, headers: { [name]: 'image/png' } };
|
|
77
|
+
expect(getWebpSource(source)).toBeUndefined();
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
it.each([
|
|
82
|
+
{},
|
|
83
|
+
{ uri: 'https://static.momocdn.net/a.gif' },
|
|
84
|
+
{ uri: `${PNG}?format=jpg` },
|
|
85
|
+
{ uri: 'https://example.com/a.png' },
|
|
86
|
+
{ uri: 'file:///data/a.png' },
|
|
87
|
+
])('leaves a non-candidate source alone: %p', source => {
|
|
88
|
+
expect(getWebpSource(source)).toBeUndefined();
|
|
89
|
+
});
|
|
90
|
+
});
|
package/Image/index.tsx
CHANGED
|
@@ -12,6 +12,7 @@ import { Skeleton } from '../Skeleton';
|
|
|
12
12
|
import { Icon } from '../Icon';
|
|
13
13
|
import { Styles } from '../Consts';
|
|
14
14
|
import { ImageProps } from './types';
|
|
15
|
+
import { useWebpSource } from './useWebpSource';
|
|
15
16
|
|
|
16
17
|
type Status = 'success' | 'loading' | 'error';
|
|
17
18
|
|
|
@@ -26,12 +27,13 @@ export const getImageName = (source: any): string => {
|
|
|
26
27
|
|
|
27
28
|
/**
|
|
28
29
|
* FastImage that shows a skeleton while loading (if `loading`) or inside a
|
|
29
|
-
* loading `SkeletonProvider`, and a fail icon on error.
|
|
30
|
+
* loading `SkeletonProvider`, and a fail icon on error. CDN images ask for WebP.
|
|
30
31
|
*/
|
|
31
32
|
const Image: React.FC<ImageProps> = ({
|
|
32
33
|
style,
|
|
33
34
|
source,
|
|
34
35
|
loading = true,
|
|
36
|
+
webp = true,
|
|
35
37
|
children,
|
|
36
38
|
onProgress,
|
|
37
39
|
onLoadStart,
|
|
@@ -45,9 +47,13 @@ const Image: React.FC<ImageProps> = ({
|
|
|
45
47
|
const context = useContext<any>(MiniAppContext);
|
|
46
48
|
const skeleton = useContext(SkeletonContext);
|
|
47
49
|
const hasErrorRef = useRef(false);
|
|
50
|
+
const isWebpFailedRef = useRef(false);
|
|
48
51
|
const [status, setStatus] = useState<Status>('success');
|
|
49
52
|
|
|
50
53
|
const showBaseLineDebug = context?.features?.showBaseLineDebug ?? false;
|
|
54
|
+
const dsConfig = context?.designSystemConfig?.config;
|
|
55
|
+
const isWebpConfigOn = dsConfig?.imageWebp !== false;
|
|
56
|
+
const webpSource = useWebpSource(source, webp && isWebpConfigOn);
|
|
51
57
|
|
|
52
58
|
const imageName = getImageName(source);
|
|
53
59
|
let accessibilityLabel: any = `img|${imageName}`;
|
|
@@ -85,7 +91,7 @@ const Image: React.FC<ImageProps> = ({
|
|
|
85
91
|
<FastImage
|
|
86
92
|
{...rest}
|
|
87
93
|
{...{ accessibilityLabel: rest.accessibilityLabel ?? accessibilityLabel }}
|
|
88
|
-
source={source}
|
|
94
|
+
source={webpSource.source}
|
|
89
95
|
style={[
|
|
90
96
|
styles.container,
|
|
91
97
|
style,
|
|
@@ -100,6 +106,7 @@ const Image: React.FC<ImageProps> = ({
|
|
|
100
106
|
}}
|
|
101
107
|
onLoadStart={() => {
|
|
102
108
|
hasErrorRef.current = false;
|
|
109
|
+
isWebpFailedRef.current = false;
|
|
103
110
|
onLoadStart?.();
|
|
104
111
|
}}
|
|
105
112
|
onLoad={event => {
|
|
@@ -107,6 +114,11 @@ const Image: React.FC<ImageProps> = ({
|
|
|
107
114
|
onLoad?.(event);
|
|
108
115
|
}}
|
|
109
116
|
onLoadEnd={() => {
|
|
117
|
+
// The failed WebP attempt stays invisible: the plain source loads next.
|
|
118
|
+
if (isWebpFailedRef.current) {
|
|
119
|
+
isWebpFailedRef.current = false;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
110
122
|
let current: Status = 'success';
|
|
111
123
|
if (hasErrorRef.current) {
|
|
112
124
|
current = 'error';
|
|
@@ -115,6 +127,11 @@ const Image: React.FC<ImageProps> = ({
|
|
|
115
127
|
onLoadEnd?.();
|
|
116
128
|
}}
|
|
117
129
|
onError={() => {
|
|
130
|
+
if (webpSource.isWebp) {
|
|
131
|
+
isWebpFailedRef.current = true;
|
|
132
|
+
webpSource.fallBack();
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
118
135
|
hasErrorRef.current = true;
|
|
119
136
|
onError?.();
|
|
120
137
|
}}
|
package/Image/types.ts
CHANGED
|
@@ -4,4 +4,10 @@ export interface ImageProps extends FastImageProps {
|
|
|
4
4
|
loading?: boolean;
|
|
5
5
|
accessibilityLabel?: any;
|
|
6
6
|
params?: any;
|
|
7
|
+
/**
|
|
8
|
+
* Asks the MoMo CDN (`img.mservice.com.vn`, `static.momocdn.net`) for WebP via
|
|
9
|
+
* the Accept header, retrying the plain source once if that fails. Default
|
|
10
|
+
* `true`; the `imageWebp: false` design system config turns it off everywhere.
|
|
11
|
+
*/
|
|
12
|
+
webp?: boolean;
|
|
7
13
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useCallback, useMemo, useState } from 'react';
|
|
2
|
+
import { type Source } from 'react-native-fast-image';
|
|
3
|
+
import { type ImageProps } from './types';
|
|
4
|
+
import { getWebpSource } from './utils';
|
|
5
|
+
|
|
6
|
+
type ImageSource = ImageProps['source'];
|
|
7
|
+
|
|
8
|
+
/** JSON of the WebP variant of a `{ uri }` source; undefined if not a candidate. */
|
|
9
|
+
const getWebpKey = (source: ImageSource): string | undefined => {
|
|
10
|
+
if (typeof source !== 'object' || !source) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
const webpSource = getWebpSource(source);
|
|
14
|
+
if (!webpSource) {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
return JSON.stringify(webpSource);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Asks the MoMo CDN for WebP (Accept header) until that request fails once,
|
|
22
|
+
* then loads the caller's source as is. A new uri or headers try WebP again.
|
|
23
|
+
*/
|
|
24
|
+
const useWebpSource = (source: ImageSource, isEnabled: boolean) => {
|
|
25
|
+
const [failedWebpKey, setFailedWebpKey] = useState<string>();
|
|
26
|
+
|
|
27
|
+
let webpKey: string | undefined;
|
|
28
|
+
if (isEnabled) {
|
|
29
|
+
webpKey = getWebpKey(source);
|
|
30
|
+
}
|
|
31
|
+
const isWebp = webpKey !== undefined && webpKey !== failedWebpKey;
|
|
32
|
+
|
|
33
|
+
// Keyed by JSON so an inline `{ uri }` doesn't hand FastImage a new source each render.
|
|
34
|
+
const webpSource = useMemo((): Source | undefined => {
|
|
35
|
+
if (webpKey === undefined) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
return JSON.parse(webpKey);
|
|
39
|
+
}, [webpKey]);
|
|
40
|
+
|
|
41
|
+
const fallBack = useCallback(() => {
|
|
42
|
+
setFailedWebpKey(webpKey);
|
|
43
|
+
}, [webpKey]);
|
|
44
|
+
|
|
45
|
+
let loadSource = source;
|
|
46
|
+
if (isWebp && webpSource) {
|
|
47
|
+
loadSource = webpSource;
|
|
48
|
+
}
|
|
49
|
+
return { source: loadSource, isWebp, fallBack };
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export { useWebpSource };
|
package/Image/utils.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const WEBP_HOSTS = ['img.mservice.com.vn', 'static.momocdn.net'];
|
|
2
|
+
// Image loaders send `image/*,*/*`, which the CDN answers with the original format.
|
|
3
|
+
const WEBP_ACCEPT = 'image/webp,image/*,*/*;q=0.8';
|
|
4
|
+
const ACCEPT_HEADER = 'accept';
|
|
5
|
+
const FORMAT_PARAM = 'format';
|
|
6
|
+
const SKIPPED_EXTENSION = /\.(webp|gif|svg)$/i;
|
|
7
|
+
// Split by hand: RN's URL polyfill rewrites the string (adds `/`, moves `#`).
|
|
8
|
+
const HTTP_URL = /^https?:\/\/([^/?#:]+)(?::\d+)?([^?#]*)(\?[^#]*)?/i;
|
|
9
|
+
|
|
10
|
+
type UrlParts = { host: string; path: string; query: string };
|
|
11
|
+
type Headers = { [key: string]: string };
|
|
12
|
+
type HeaderSource = { uri?: string; headers?: Headers };
|
|
13
|
+
|
|
14
|
+
/** Host, path and `?query` of an http(s) URL; undefined otherwise. */
|
|
15
|
+
const parseHttpUrl = (uri: string): UrlParts | undefined => {
|
|
16
|
+
const match = HTTP_URL.exec(uri);
|
|
17
|
+
if (!match) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
const [, host, path, query = ''] = match;
|
|
21
|
+
return { host: host.toLowerCase(), path, query };
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** Whether the `?query` part already has a `format` param. */
|
|
25
|
+
const hasFormatParam = (query: string): boolean => {
|
|
26
|
+
const params = query.replace(/^\?/, '').split('&');
|
|
27
|
+
return params.some(param => param.split('=')[0] === FORMAT_PARAM);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Whether `headers` has an Accept header, in any letter case. */
|
|
31
|
+
const hasAcceptHeader = (headers?: Headers): boolean => {
|
|
32
|
+
const names = Object.keys(headers ?? {});
|
|
33
|
+
return names.some(name => name.toLowerCase() === ACCEPT_HEADER);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Whether the MoMo CDN can serve `uri` as WebP: http(s) on a CDN host, not
|
|
38
|
+
* already WebP/GIF/SVG and without a `format` param of its own.
|
|
39
|
+
*/
|
|
40
|
+
const isWebpCandidate = (uri: string): boolean => {
|
|
41
|
+
const parts = parseHttpUrl(uri);
|
|
42
|
+
if (!parts || !WEBP_HOSTS.includes(parts.host)) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
return !SKIPPED_EXTENSION.test(parts.path) && !hasFormatParam(parts.query);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* `source` plus an Accept header asking the CDN for WebP; undefined when the
|
|
50
|
+
* uri isn't a candidate or the caller already sets Accept. The uri is kept.
|
|
51
|
+
*/
|
|
52
|
+
const getWebpSource = <T extends HeaderSource>(source: T): T | undefined => {
|
|
53
|
+
const { uri, headers } = source;
|
|
54
|
+
const isCandidate = typeof uri === 'string' && isWebpCandidate(uri);
|
|
55
|
+
if (!isCandidate || hasAcceptHeader(headers)) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
return { ...source, headers: { ...headers, Accept: WEBP_ACCEPT } };
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export { WEBP_ACCEPT, isWebpCandidate, getWebpSource };
|
package/Input/ErrorView.tsx
CHANGED
package/Input/InputMoneyView.tsx
CHANGED
|
@@ -53,8 +53,8 @@ type InputMoneyViewProps = Pick<
|
|
|
53
53
|
};
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
|
-
* Presentational body of InputMoney: bordered field, currency suffix (
|
|
57
|
-
*
|
|
56
|
+
* Presentational body of InputMoney: bordered field, currency suffix (kept
|
|
57
|
+
* after a placeholder that ends with it) and the clear/trailing icons.
|
|
58
58
|
*/
|
|
59
59
|
export const InputMoneyView = ({
|
|
60
60
|
size,
|
|
@@ -101,8 +101,13 @@ export const InputMoneyView = ({
|
|
|
101
101
|
fontSize: scaledFontSize,
|
|
102
102
|
fontFamily: inputFontFamily,
|
|
103
103
|
};
|
|
104
|
-
const
|
|
104
|
+
const hasCurrencyInPlaceholder = placeholder.endsWith(currency);
|
|
105
|
+
const shouldShowCurrency = !!displayValue || hasCurrencyInPlaceholder;
|
|
105
106
|
const shouldShowClearIcon = !!(showClearIcon && focused && displayValue);
|
|
107
|
+
let inputPlaceholder = placeholder;
|
|
108
|
+
if (hasCurrencyInPlaceholder) {
|
|
109
|
+
inputPlaceholder = placeholder.slice(0, -currency.length);
|
|
110
|
+
}
|
|
106
111
|
return (
|
|
107
112
|
<View
|
|
108
113
|
style={[
|
|
@@ -143,15 +148,18 @@ export const InputMoneyView = ({
|
|
|
143
148
|
onChangeText={onChangeText}
|
|
144
149
|
onFocus={onFocus}
|
|
145
150
|
onBlur={onBlur}
|
|
146
|
-
placeholder={
|
|
151
|
+
placeholder={inputPlaceholder}
|
|
147
152
|
selectionColor={theme.colors.primary}
|
|
148
153
|
placeholderTextColor={placeholderColor}
|
|
149
154
|
keyboardType={'numeric'}
|
|
150
155
|
maxLength={undefined}
|
|
151
156
|
/>
|
|
152
|
-
{
|
|
157
|
+
{shouldShowCurrency && (
|
|
153
158
|
<Text
|
|
154
|
-
style={[
|
|
159
|
+
style={[
|
|
160
|
+
textStyle,
|
|
161
|
+
{ color: displayValue ? textColor : placeholderColor },
|
|
162
|
+
]}
|
|
155
163
|
allowFontScaling={false}
|
|
156
164
|
>
|
|
157
165
|
{currency}
|
package/package.json
CHANGED
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
import React, { createRef, type Ref } from 'react';
|
|
2
|
-
import { act, fireEvent, render, screen } from '@testing-library/react-native';
|
|
3
|
-
import { type InputMoneyProps, type InputRef } from '../index';
|
|
4
|
-
import InputMoney from '../InputMoney';
|
|
5
|
-
|
|
6
|
-
jest.mock('../../Application', () => ({
|
|
7
|
-
useComponentId: (name: string, label?: string) => ({
|
|
8
|
-
componentId: label ?? name,
|
|
9
|
-
}),
|
|
10
|
-
}));
|
|
11
|
-
|
|
12
|
-
jest.mock('react-native-fast-image', () => {
|
|
13
|
-
const { View } = require('react-native');
|
|
14
|
-
const FastImage = (props: object) => <View {...props} />;
|
|
15
|
-
FastImage.resizeMode = {};
|
|
16
|
-
FastImage.priority = {};
|
|
17
|
-
return FastImage;
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
jest.mock('react-native-reanimated', () =>
|
|
21
|
-
require('react-native-reanimated/mock'),
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
const ID = 'money';
|
|
25
|
-
const LCG_MODULUS = 4294967296;
|
|
26
|
-
const LCG_SHIFT = 256;
|
|
27
|
-
|
|
28
|
-
type MoneyProps = Partial<InputMoneyProps> & { ref?: Ref<InputRef> };
|
|
29
|
-
|
|
30
|
-
const renderMoney = (props: MoneyProps = {}) => {
|
|
31
|
-
const onChangeText = jest.fn();
|
|
32
|
-
const utils = render(
|
|
33
|
-
<InputMoney
|
|
34
|
-
accessibilityLabel={ID}
|
|
35
|
-
onChangeText={onChangeText}
|
|
36
|
-
{...props}
|
|
37
|
-
/>,
|
|
38
|
-
);
|
|
39
|
-
return { ...utils, onChangeText, input: screen.getByLabelText(ID) };
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const expectedDigits = (raw: string, maxLength?: number) => {
|
|
43
|
-
let d = raw.replace(/\D/g, '').replace(/^0+(?=\d)/, '');
|
|
44
|
-
if (maxLength !== undefined) {
|
|
45
|
-
d = d.slice(0, maxLength);
|
|
46
|
-
}
|
|
47
|
-
return d;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const group = (d: string) => d.replace(/\B(?=(\d{3})+(?!\d))/g, '.');
|
|
51
|
-
|
|
52
|
-
const createLcg = (seed: number) => {
|
|
53
|
-
let state = seed;
|
|
54
|
-
return (max: number) => {
|
|
55
|
-
state = (state * 1664525 + 1013904223) % LCG_MODULUS;
|
|
56
|
-
return Math.floor(state / LCG_SHIFT) % max;
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const lcg = createLcg(42);
|
|
61
|
-
|
|
62
|
-
const nextNumber = (minLength: number, maxLength: number) => {
|
|
63
|
-
const length = minLength + lcg(maxLength - minLength + 1);
|
|
64
|
-
let digits = String(1 + lcg(9));
|
|
65
|
-
while (digits.length < length) {
|
|
66
|
-
digits += String(lcg(10));
|
|
67
|
-
}
|
|
68
|
-
return digits;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const makeNumbers = (count: number, minLength: number, maxLength: number) =>
|
|
72
|
-
Array.from({ length: count }, () => nextNumber(minLength, maxLength));
|
|
73
|
-
|
|
74
|
-
const CHAR_NUMBERS = makeNumbers(100, 1, 12);
|
|
75
|
-
const BACKSPACE_NUMBERS = makeNumbers(50, 2, 12);
|
|
76
|
-
|
|
77
|
-
const lastArg = (fn: jest.Mock) => fn.mock.lastCall?.[0];
|
|
78
|
-
|
|
79
|
-
describe('InputMoney matrix: render', () => {
|
|
80
|
-
const CURRENCIES = ['đ', '$', '', ' VND'];
|
|
81
|
-
const PLACEHOLDERS = [undefined, '0đ', 'Nhập khối lượng'];
|
|
82
|
-
const VALUES = [
|
|
83
|
-
undefined,
|
|
84
|
-
'',
|
|
85
|
-
'0',
|
|
86
|
-
'7',
|
|
87
|
-
'00123',
|
|
88
|
-
'1000',
|
|
89
|
-
'1234567',
|
|
90
|
-
'999999999999',
|
|
91
|
-
'abc1.000',
|
|
92
|
-
'1,5',
|
|
93
|
-
];
|
|
94
|
-
const cases = CURRENCIES.flatMap(currency =>
|
|
95
|
-
PLACEHOLDERS.flatMap(placeholder =>
|
|
96
|
-
VALUES.map(value => ({ currency, placeholder, value })),
|
|
97
|
-
),
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
test.each(cases)(
|
|
101
|
-
'currency=$currency placeholder=$placeholder value=$value',
|
|
102
|
-
({ currency, placeholder, value }) => {
|
|
103
|
-
const { input } = renderMoney({ currency, placeholder, value });
|
|
104
|
-
const display = group(expectedDigits(value ?? ''));
|
|
105
|
-
|
|
106
|
-
expect(input.props.placeholder).toBe(placeholder ?? '0đ');
|
|
107
|
-
expect(input.props.value).toBe(display);
|
|
108
|
-
if (currency) {
|
|
109
|
-
const found = screen.queryByText(currency);
|
|
110
|
-
expect(found === null).toBe(display === '');
|
|
111
|
-
} else {
|
|
112
|
-
expect(screen.queryByText('đ')).toBeNull();
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
describe('InputMoney matrix: typing', () => {
|
|
119
|
-
const TYPED = [
|
|
120
|
-
'1',
|
|
121
|
-
'12',
|
|
122
|
-
'123',
|
|
123
|
-
'1234',
|
|
124
|
-
'12345',
|
|
125
|
-
'123456789',
|
|
126
|
-
'0',
|
|
127
|
-
'0005',
|
|
128
|
-
'12a3',
|
|
129
|
-
'abc',
|
|
130
|
-
'1.234',
|
|
131
|
-
'1.2345',
|
|
132
|
-
'-500',
|
|
133
|
-
' 42 ',
|
|
134
|
-
'999.999.999',
|
|
135
|
-
];
|
|
136
|
-
const cases = TYPED.flatMap(typed =>
|
|
137
|
-
[undefined, 1, 3, 6].flatMap(maxLength =>
|
|
138
|
-
['đ', ''].map(currency => ({ typed, maxLength, currency })),
|
|
139
|
-
),
|
|
140
|
-
);
|
|
141
|
-
|
|
142
|
-
test.each(cases)(
|
|
143
|
-
'types "$typed" maxLength=$maxLength currency="$currency"',
|
|
144
|
-
({ typed, maxLength, currency }) => {
|
|
145
|
-
const { input, onChangeText } = renderMoney({ maxLength, currency });
|
|
146
|
-
|
|
147
|
-
fireEvent.changeText(input, typed);
|
|
148
|
-
|
|
149
|
-
const digits = expectedDigits(typed, maxLength);
|
|
150
|
-
expect(onChangeText).toHaveBeenLastCalledWith(digits);
|
|
151
|
-
expect(screen.getByLabelText(ID).props.value).toBe(group(digits));
|
|
152
|
-
},
|
|
153
|
-
);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
describe('InputMoney matrix: char-by-char typing', () => {
|
|
157
|
-
test.each(CHAR_NUMBERS)('types digits of %s one by one', number => {
|
|
158
|
-
const { input, onChangeText } = renderMoney();
|
|
159
|
-
let display = '';
|
|
160
|
-
|
|
161
|
-
for (const char of number) {
|
|
162
|
-
fireEvent.changeText(input, display + char);
|
|
163
|
-
display = screen.getByLabelText(ID).props.value;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
expect(lastArg(onChangeText)).toBe(number);
|
|
167
|
-
expect(display).toBe(group(number));
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
describe('InputMoney matrix: backspace', () => {
|
|
172
|
-
test.each(BACKSPACE_NUMBERS)('deletes %s down to empty', number => {
|
|
173
|
-
const { input, onChangeText } = renderMoney();
|
|
174
|
-
fireEvent.changeText(input, number);
|
|
175
|
-
|
|
176
|
-
for (let k = 1; k <= number.length; k += 1) {
|
|
177
|
-
const display = screen.getByLabelText(ID).props.value;
|
|
178
|
-
fireEvent.changeText(input, display.slice(0, -1));
|
|
179
|
-
|
|
180
|
-
const rest = number.slice(0, number.length - k);
|
|
181
|
-
expect(screen.getByLabelText(ID).props.value).toBe(group(rest));
|
|
182
|
-
expect(lastArg(onChangeText)).toBe(rest);
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
describe('InputMoney matrix: clear icon', () => {
|
|
188
|
-
const cases = [undefined, true, false].flatMap(showClearIcon =>
|
|
189
|
-
[true, false].flatMap(isFocused =>
|
|
190
|
-
['', '5', '1000'].map(typed => ({ showClearIcon, isFocused, typed })),
|
|
191
|
-
),
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
test.each(cases)(
|
|
195
|
-
'showClearIcon=$showClearIcon isFocused=$isFocused typed="$typed"',
|
|
196
|
-
({ showClearIcon, isFocused, typed }) => {
|
|
197
|
-
const { input } = renderMoney({ showClearIcon });
|
|
198
|
-
|
|
199
|
-
if (isFocused) {
|
|
200
|
-
fireEvent(input, 'focus');
|
|
201
|
-
}
|
|
202
|
-
fireEvent.changeText(input, typed);
|
|
203
|
-
|
|
204
|
-
const isVisible = showClearIcon !== false && isFocused && typed !== '';
|
|
205
|
-
const icon = screen.queryByLabelText(`${ID}|clear-icon-touch`);
|
|
206
|
-
expect(icon !== null).toBe(isVisible);
|
|
207
|
-
},
|
|
208
|
-
);
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
describe('InputMoney matrix: trailing', () => {
|
|
212
|
-
const TRAILING = [
|
|
213
|
-
undefined,
|
|
214
|
-
'Nhập tối đa',
|
|
215
|
-
'Một nhãn rất dài hơn 15 ký',
|
|
216
|
-
'Max',
|
|
217
|
-
];
|
|
218
|
-
const cases = TRAILING.flatMap(trailing =>
|
|
219
|
-
[false, true].flatMap(loading =>
|
|
220
|
-
[false, true].map(disabled => ({ trailing, loading, disabled })),
|
|
221
|
-
),
|
|
222
|
-
);
|
|
223
|
-
|
|
224
|
-
test.each(cases)(
|
|
225
|
-
'trailing=$trailing loading=$loading disabled=$disabled',
|
|
226
|
-
({ trailing, loading, disabled }) => {
|
|
227
|
-
renderMoney({ trailing, loading, disabled });
|
|
228
|
-
|
|
229
|
-
const node = screen.queryByLabelText(`${ID}|trailing-text`);
|
|
230
|
-
if (trailing && !loading) {
|
|
231
|
-
expect(node).toHaveTextContent(trailing.substring(0, 15));
|
|
232
|
-
} else {
|
|
233
|
-
expect(node).toBeNull();
|
|
234
|
-
}
|
|
235
|
-
},
|
|
236
|
-
);
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
describe('InputMoney matrix: floating label', () => {
|
|
240
|
-
const cases = [undefined, 'Khối lượng'].flatMap(floatingValue =>
|
|
241
|
-
[undefined, true, false].flatMap(required =>
|
|
242
|
-
[false, true].flatMap(disabled =>
|
|
243
|
-
[undefined, 'ic_info'].map(floatingIcon => ({
|
|
244
|
-
floatingValue,
|
|
245
|
-
required,
|
|
246
|
-
disabled,
|
|
247
|
-
floatingIcon,
|
|
248
|
-
})),
|
|
249
|
-
),
|
|
250
|
-
),
|
|
251
|
-
);
|
|
252
|
-
|
|
253
|
-
test.each(cases)(
|
|
254
|
-
'floatingValue=$floatingValue required=$required disabled=$disabled floatingIcon=$floatingIcon',
|
|
255
|
-
props => {
|
|
256
|
-
renderMoney(props);
|
|
257
|
-
|
|
258
|
-
const node = screen.queryByLabelText(`${ID}|floating-text`);
|
|
259
|
-
if (props.floatingValue) {
|
|
260
|
-
const suffix = props.required ? '*' : '';
|
|
261
|
-
expect(node).toHaveTextContent(`${props.floatingValue}${suffix}`);
|
|
262
|
-
} else {
|
|
263
|
-
expect(node).toBeNull();
|
|
264
|
-
}
|
|
265
|
-
},
|
|
266
|
-
);
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
describe('InputMoney matrix: error and hint', () => {
|
|
270
|
-
const cases = [undefined, '', 'Vượt sức mua'].flatMap(errorMessage =>
|
|
271
|
-
[undefined, '', 'Tối đa 5'].flatMap(hintText =>
|
|
272
|
-
[undefined, true, false].map(errorSpacing => ({
|
|
273
|
-
errorMessage,
|
|
274
|
-
hintText,
|
|
275
|
-
errorSpacing,
|
|
276
|
-
})),
|
|
277
|
-
),
|
|
278
|
-
);
|
|
279
|
-
|
|
280
|
-
test.each(cases)(
|
|
281
|
-
'errorMessage=$errorMessage hintText=$hintText errorSpacing=$errorSpacing',
|
|
282
|
-
({ errorMessage, hintText, errorSpacing }) => {
|
|
283
|
-
renderMoney({ errorMessage, hintText, errorSpacing });
|
|
284
|
-
|
|
285
|
-
const error = screen.queryByLabelText(`${ID}|error-text`);
|
|
286
|
-
const hint = screen.queryByLabelText(`${ID}|hint-text`);
|
|
287
|
-
if (errorMessage) {
|
|
288
|
-
expect(error).toHaveTextContent(errorMessage);
|
|
289
|
-
expect(hint).toBeNull();
|
|
290
|
-
} else if (hintText) {
|
|
291
|
-
expect(error).toBeNull();
|
|
292
|
-
expect(hint).toHaveTextContent(hintText);
|
|
293
|
-
} else {
|
|
294
|
-
expect(error).toBeNull();
|
|
295
|
-
expect(hint).toBeNull();
|
|
296
|
-
}
|
|
297
|
-
},
|
|
298
|
-
);
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
describe('InputMoney matrix: ref.setText', () => {
|
|
302
|
-
const STRINGS = [
|
|
303
|
-
'',
|
|
304
|
-
'0',
|
|
305
|
-
'5',
|
|
306
|
-
'42',
|
|
307
|
-
'999',
|
|
308
|
-
'1000',
|
|
309
|
-
'1.000',
|
|
310
|
-
'12.345.678',
|
|
311
|
-
'12$',
|
|
312
|
-
'1.000đ',
|
|
313
|
-
'$5',
|
|
314
|
-
'abc',
|
|
315
|
-
'a1b2c3',
|
|
316
|
-
'000',
|
|
317
|
-
'000123',
|
|
318
|
-
'0.001',
|
|
319
|
-
'00 5',
|
|
320
|
-
'123456789012345',
|
|
321
|
-
'999.999.999.999',
|
|
322
|
-
'1,5',
|
|
323
|
-
'-250',
|
|
324
|
-
' 77 ',
|
|
325
|
-
'10đ000',
|
|
326
|
-
'100000',
|
|
327
|
-
'7.0.0.0',
|
|
328
|
-
];
|
|
329
|
-
|
|
330
|
-
test.each(STRINGS)('setText("%s")', text => {
|
|
331
|
-
const ref = createRef<InputRef>();
|
|
332
|
-
const { input, onChangeText } = renderMoney({ ref });
|
|
333
|
-
|
|
334
|
-
act(() => ref.current?.setText(text));
|
|
335
|
-
|
|
336
|
-
const digits = expectedDigits(text);
|
|
337
|
-
expect(input.props.value).toBe(group(digits));
|
|
338
|
-
expect(lastArg(onChangeText)).toBe(digits);
|
|
339
|
-
});
|
|
340
|
-
});
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
import React, { createRef, type Ref } from 'react';
|
|
2
|
-
import { act, fireEvent, render, screen } from '@testing-library/react-native';
|
|
3
|
-
import { type InputMoneyProps, type InputRef } from '../index';
|
|
4
|
-
import InputMoney from '../InputMoney';
|
|
5
|
-
|
|
6
|
-
jest.mock('../../Application', () => ({
|
|
7
|
-
useComponentId: (name: string, label?: string) => ({
|
|
8
|
-
componentId: label ?? name,
|
|
9
|
-
}),
|
|
10
|
-
}));
|
|
11
|
-
|
|
12
|
-
jest.mock('react-native-fast-image', () => {
|
|
13
|
-
const { View } = require('react-native');
|
|
14
|
-
const FastImage = (props: object) => <View {...props} />;
|
|
15
|
-
FastImage.resizeMode = {};
|
|
16
|
-
FastImage.priority = {};
|
|
17
|
-
return FastImage;
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
jest.mock('react-native-reanimated', () =>
|
|
21
|
-
require('react-native-reanimated/mock'),
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
const ID = 'money';
|
|
25
|
-
|
|
26
|
-
type MoneyProps = Partial<InputMoneyProps> & { ref?: Ref<InputRef> };
|
|
27
|
-
|
|
28
|
-
const renderMoney = (props: MoneyProps = {}) => {
|
|
29
|
-
const onChangeText = jest.fn();
|
|
30
|
-
const utils = render(
|
|
31
|
-
<InputMoney
|
|
32
|
-
accessibilityLabel={ID}
|
|
33
|
-
onChangeText={onChangeText}
|
|
34
|
-
{...props}
|
|
35
|
-
/>,
|
|
36
|
-
);
|
|
37
|
-
return { ...utils, onChangeText, input: screen.getByLabelText(ID) };
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
describe('InputMoney: currency × placeholder × value', () => {
|
|
41
|
-
test.each`
|
|
42
|
-
currency | placeholder | value | display | suffix
|
|
43
|
-
${undefined} | ${undefined} | ${undefined} | ${''} | ${null}
|
|
44
|
-
${undefined} | ${undefined} | ${'1234567'} | ${'1.234.567'} | ${'đ'}
|
|
45
|
-
${'đ'} | ${'0đ'} | ${'0'} | ${'0'} | ${'đ'}
|
|
46
|
-
${'đ'} | ${'Nhập số tiền'} | ${'00123'} | ${'123'} | ${'đ'}
|
|
47
|
-
${'đ'} | ${'Nhập số tiền'} | ${'abc1.000'} | ${'1.000'} | ${'đ'}
|
|
48
|
-
${'$'} | ${'0$'} | ${'1000'} | ${'1.000'} | ${'$'}
|
|
49
|
-
${''} | ${'Nhập khối lượng'} | ${undefined} | ${''} | ${null}
|
|
50
|
-
${''} | ${'Nhập khối lượng'} | ${'500'} | ${'500'} | ${null}
|
|
51
|
-
`(
|
|
52
|
-
'currency=$currency placeholder=$placeholder value=$value → "$display" $suffix',
|
|
53
|
-
({ currency, placeholder, value, display, suffix }) => {
|
|
54
|
-
const { input } = renderMoney({ currency, placeholder, value });
|
|
55
|
-
|
|
56
|
-
expect(input.props.placeholder).toBe(placeholder ?? '0đ');
|
|
57
|
-
expect(input.props.value).toBe(display);
|
|
58
|
-
if (suffix) {
|
|
59
|
-
expect(screen.getByText(suffix)).toBeTruthy();
|
|
60
|
-
} else {
|
|
61
|
-
expect(screen.queryByText('đ')).toBeNull();
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
describe('InputMoney: typing × maxLength', () => {
|
|
68
|
-
test.each`
|
|
69
|
-
typed | maxLength | emitted | display
|
|
70
|
-
${'1234'} | ${undefined} | ${'1234'} | ${'1.234'}
|
|
71
|
-
${'0005'} | ${undefined} | ${'5'} | ${'5'}
|
|
72
|
-
${'12a3'} | ${undefined} | ${'123'} | ${'123'}
|
|
73
|
-
${'abc'} | ${undefined} | ${''} | ${''}
|
|
74
|
-
${'12345'} | ${3} | ${'123'} | ${'123'}
|
|
75
|
-
${'1.2345'} | ${4} | ${'1234'} | ${'1.234'}
|
|
76
|
-
${'1.2345'} | ${undefined} | ${'12345'} | ${'12.345'}
|
|
77
|
-
`(
|
|
78
|
-
'types "$typed" with maxLength=$maxLength → emits "$emitted", shows "$display"',
|
|
79
|
-
({ typed, maxLength, emitted, display }) => {
|
|
80
|
-
const { input, onChangeText } = renderMoney({ maxLength });
|
|
81
|
-
|
|
82
|
-
fireEvent.changeText(input, typed);
|
|
83
|
-
|
|
84
|
-
expect(onChangeText).toHaveBeenLastCalledWith(emitted);
|
|
85
|
-
expect(screen.getByLabelText(ID).props.value).toBe(display);
|
|
86
|
-
},
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
test('regroups when a digit is added to a formatted value', () => {
|
|
90
|
-
const { input, onChangeText } = renderMoney();
|
|
91
|
-
|
|
92
|
-
fireEvent.changeText(input, '1234');
|
|
93
|
-
fireEvent.changeText(input, '1.2345');
|
|
94
|
-
|
|
95
|
-
expect(onChangeText).toHaveBeenLastCalledWith('12345');
|
|
96
|
-
expect(input.props.value).toBe('12.345');
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test('deleting the separator removes the digit before it', () => {
|
|
100
|
-
const { input, onChangeText } = renderMoney();
|
|
101
|
-
|
|
102
|
-
fireEvent.changeText(input, '1.234');
|
|
103
|
-
fireEvent.changeText(input, '1234');
|
|
104
|
-
|
|
105
|
-
expect(onChangeText).toHaveBeenLastCalledWith('234');
|
|
106
|
-
expect(input.props.value).toBe('234');
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
test('clearing the text emits an empty value and hides the currency', () => {
|
|
110
|
-
const { input, onChangeText } = renderMoney();
|
|
111
|
-
|
|
112
|
-
fireEvent.changeText(input, '1000');
|
|
113
|
-
fireEvent.changeText(input, '');
|
|
114
|
-
|
|
115
|
-
expect(onChangeText).toHaveBeenLastCalledWith('');
|
|
116
|
-
expect(input.props.value).toBe('');
|
|
117
|
-
expect(screen.queryByText('đ')).toBeNull();
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
describe('InputMoney: showClearIcon × focus × value', () => {
|
|
122
|
-
const CLEAR = `${ID}|clear-icon-touch`;
|
|
123
|
-
|
|
124
|
-
test.each`
|
|
125
|
-
showClearIcon | focused | typed | visible
|
|
126
|
-
${undefined} | ${true} | ${'1000'} | ${true}
|
|
127
|
-
${true} | ${false} | ${'1000'} | ${false}
|
|
128
|
-
${true} | ${true} | ${''} | ${false}
|
|
129
|
-
${false} | ${true} | ${'1000'} | ${false}
|
|
130
|
-
`(
|
|
131
|
-
'showClearIcon=$showClearIcon focused=$focused typed="$typed" → visible=$visible',
|
|
132
|
-
({ showClearIcon, focused, typed, visible }) => {
|
|
133
|
-
const { input } = renderMoney({ showClearIcon });
|
|
134
|
-
|
|
135
|
-
if (focused) {
|
|
136
|
-
fireEvent(input, 'focus');
|
|
137
|
-
}
|
|
138
|
-
fireEvent.changeText(input, typed);
|
|
139
|
-
|
|
140
|
-
expect(!!screen.queryByLabelText(CLEAR)).toBe(visible);
|
|
141
|
-
},
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
test('pressing clear empties the value', () => {
|
|
145
|
-
const { input, onChangeText } = renderMoney();
|
|
146
|
-
|
|
147
|
-
fireEvent(input, 'focus');
|
|
148
|
-
fireEvent.changeText(input, '1000');
|
|
149
|
-
fireEvent.press(screen.getByLabelText(CLEAR));
|
|
150
|
-
|
|
151
|
-
expect(onChangeText).toHaveBeenLastCalledWith('');
|
|
152
|
-
expect(input.props.value).toBe('');
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
describe('InputMoney: trailing × loading', () => {
|
|
157
|
-
test.each`
|
|
158
|
-
trailing | loading | text
|
|
159
|
-
${'Nhập tối đa'} | ${false} | ${'Nhập tối đa'}
|
|
160
|
-
${'Nhập tối đa'} | ${true} | ${null}
|
|
161
|
-
${'Một nhãn rất dài hơn 15 ký'} | ${false} | ${'Một nhãn rất dà'}
|
|
162
|
-
${undefined} | ${false} | ${null}
|
|
163
|
-
`(
|
|
164
|
-
'trailing=$trailing loading=$loading → text $text',
|
|
165
|
-
({ trailing, loading, text }) => {
|
|
166
|
-
renderMoney({ trailing, loading });
|
|
167
|
-
|
|
168
|
-
const trailingText = screen.queryByLabelText(`${ID}|trailing-text`);
|
|
169
|
-
if (text) {
|
|
170
|
-
expect(trailingText).toHaveTextContent(text);
|
|
171
|
-
} else {
|
|
172
|
-
expect(trailingText).toBeNull();
|
|
173
|
-
}
|
|
174
|
-
},
|
|
175
|
-
);
|
|
176
|
-
|
|
177
|
-
test('pressing the trailing text calls onPressTrailing', () => {
|
|
178
|
-
const onPressTrailing = jest.fn();
|
|
179
|
-
renderMoney({ trailing: 'Nhập tối đa', onPressTrailing });
|
|
180
|
-
|
|
181
|
-
fireEvent.press(screen.getByLabelText(`${ID}|trailing-touch`));
|
|
182
|
-
|
|
183
|
-
expect(onPressTrailing).toHaveBeenCalledTimes(1);
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
describe('InputMoney: floatingValue × required', () => {
|
|
188
|
-
test.each`
|
|
189
|
-
floatingValue | required | label
|
|
190
|
-
${'Khối lượng'} | ${undefined} | ${'Khối lượng'}
|
|
191
|
-
${'Khối lượng'} | ${true} | ${'Khối lượng*'}
|
|
192
|
-
${undefined} | ${true} | ${null}
|
|
193
|
-
`(
|
|
194
|
-
'floatingValue=$floatingValue required=$required → label $label',
|
|
195
|
-
({ floatingValue, required, label }) => {
|
|
196
|
-
renderMoney({ floatingValue, required });
|
|
197
|
-
|
|
198
|
-
const floating = screen.queryByLabelText(`${ID}|floating-text`);
|
|
199
|
-
if (label) {
|
|
200
|
-
expect(floating).toHaveTextContent(label);
|
|
201
|
-
} else {
|
|
202
|
-
expect(floating).toBeNull();
|
|
203
|
-
}
|
|
204
|
-
},
|
|
205
|
-
);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
describe('InputMoney: errorMessage × hintText', () => {
|
|
209
|
-
test.each`
|
|
210
|
-
errorMessage | hintText | errorText | hint
|
|
211
|
-
${'Vượt sức mua'} | ${undefined} | ${'Vượt sức mua'} | ${null}
|
|
212
|
-
${'Vượt sức mua'} | ${'Tối đa 5'} | ${'Vượt sức mua'} | ${null}
|
|
213
|
-
${undefined} | ${'Tối đa 5'} | ${null} | ${'Tối đa 5'}
|
|
214
|
-
${undefined} | ${undefined} | ${null} | ${null}
|
|
215
|
-
`(
|
|
216
|
-
'errorMessage=$errorMessage hintText=$hintText',
|
|
217
|
-
({ errorMessage, hintText, errorText, hint }) => {
|
|
218
|
-
renderMoney({ errorMessage, hintText });
|
|
219
|
-
|
|
220
|
-
const error = screen.queryByLabelText(`${ID}|error-text`);
|
|
221
|
-
const hintView = screen.queryByLabelText(`${ID}|hint-text`);
|
|
222
|
-
if (errorText) {
|
|
223
|
-
expect(error).toHaveTextContent(errorText);
|
|
224
|
-
} else {
|
|
225
|
-
expect(error).toBeNull();
|
|
226
|
-
}
|
|
227
|
-
if (hint) {
|
|
228
|
-
expect(hintView).toHaveTextContent(hint);
|
|
229
|
-
} else {
|
|
230
|
-
expect(hintView).toBeNull();
|
|
231
|
-
}
|
|
232
|
-
},
|
|
233
|
-
);
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
describe('InputMoney: ref and callbacks', () => {
|
|
237
|
-
test('ref.setText keeps digits and ref.clear empties', () => {
|
|
238
|
-
const ref = createRef<InputRef>();
|
|
239
|
-
const { input, onChangeText } = renderMoney({ ref });
|
|
240
|
-
|
|
241
|
-
act(() => ref.current?.setText('12$'));
|
|
242
|
-
expect(input.props.value).toBe('12');
|
|
243
|
-
expect(onChangeText).toHaveBeenLastCalledWith('12');
|
|
244
|
-
|
|
245
|
-
act(() => ref.current?.clear());
|
|
246
|
-
expect(input.props.value).toBe('');
|
|
247
|
-
expect(onChangeText).toHaveBeenLastCalledWith('');
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
test('controlled value updates the display', () => {
|
|
251
|
-
const { input } = renderMoney({ value: '1000' });
|
|
252
|
-
expect(input.props.value).toBe('1.000');
|
|
253
|
-
|
|
254
|
-
screen.rerender(<InputMoney accessibilityLabel={ID} value="2500000" />);
|
|
255
|
-
|
|
256
|
-
expect(screen.getByLabelText(ID).props.value).toBe('2.500.000');
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
test('forwards focus and blur', () => {
|
|
260
|
-
const onFocus = jest.fn();
|
|
261
|
-
const onBlur = jest.fn();
|
|
262
|
-
const { input } = renderMoney({ onFocus, onBlur });
|
|
263
|
-
|
|
264
|
-
fireEvent(input, 'focus');
|
|
265
|
-
fireEvent(input, 'blur');
|
|
266
|
-
|
|
267
|
-
expect(onFocus).toHaveBeenCalledTimes(1);
|
|
268
|
-
expect(onBlur).toHaveBeenCalledTimes(1);
|
|
269
|
-
});
|
|
270
|
-
});
|