@momo-kits/foundation 0.169.16-beta.17 → 0.169.16-beta.19
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/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 (shown
|
|
57
|
+
* once there is a value) and the clear/trailing icons.
|
|
58
58
|
*/
|
|
59
59
|
export const InputMoneyView = ({
|
|
60
60
|
size,
|
|
@@ -101,15 +101,8 @@ export const InputMoneyView = ({
|
|
|
101
101
|
fontSize: scaledFontSize,
|
|
102
102
|
fontFamily: inputFontFamily,
|
|
103
103
|
};
|
|
104
|
-
const
|
|
105
|
-
const endsWithCurrency = placeholder.endsWith(currency);
|
|
106
|
-
const hasCurrencyInPlaceholder = hasCurrency && endsWithCurrency;
|
|
107
|
-
const shouldShowCurrency = !!displayValue || hasCurrencyInPlaceholder;
|
|
104
|
+
const hasValue = displayValue.length > 0;
|
|
108
105
|
const shouldShowClearIcon = !!(showClearIcon && focused && displayValue);
|
|
109
|
-
let inputPlaceholder = placeholder;
|
|
110
|
-
if (hasCurrencyInPlaceholder) {
|
|
111
|
-
inputPlaceholder = placeholder.slice(0, -currency.length);
|
|
112
|
-
}
|
|
113
106
|
return (
|
|
114
107
|
<View
|
|
115
108
|
style={[
|
|
@@ -150,18 +143,15 @@ export const InputMoneyView = ({
|
|
|
150
143
|
onChangeText={onChangeText}
|
|
151
144
|
onFocus={onFocus}
|
|
152
145
|
onBlur={onBlur}
|
|
153
|
-
placeholder={
|
|
146
|
+
placeholder={placeholder}
|
|
154
147
|
selectionColor={theme.colors.primary}
|
|
155
148
|
placeholderTextColor={placeholderColor}
|
|
156
149
|
keyboardType={'numeric'}
|
|
157
150
|
maxLength={undefined}
|
|
158
151
|
/>
|
|
159
|
-
{
|
|
152
|
+
{hasValue && (
|
|
160
153
|
<Text
|
|
161
|
-
style={[
|
|
162
|
-
textStyle,
|
|
163
|
-
{ color: displayValue ? textColor : placeholderColor },
|
|
164
|
-
]}
|
|
154
|
+
style={[textStyle, { color: textColor }]}
|
|
165
155
|
allowFontScaling={false}
|
|
166
156
|
>
|
|
167
157
|
{currency}
|
|
@@ -0,0 +1,340 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,270 @@
|
|
|
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
|
+
});
|