فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

test(q01): cover decimal precision and Persian/Arabic numeric input

davoodmehraban89 committed Oct 31, 2026 at 18:15 UTC d1d856593390565d244bda39f8f7a768e2944212
1 file changed +67
tests/number-input-precision.test.mjs new
+67
@@ -0,0 +1,67 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { NumberInput } from '../src/utils/NumberInput.js';
4 +
5 +function fakeInput(value, selectionStart = value.length) {
6 + return {
7 + value, selectionStart, selectionEnd: selectionStart,
8 + setSelectionRange(start, end) { this.selectionStart = start; this.selectionEnd = end; }
9 + };
10 +}
11 +
12 +test('formats fractional stock quantities without rounding', () => {
13 + assert.equal(NumberInput.format(2.375), '2.375');
14 + assert.equal(NumberInput.format('12345.125'), '12,345.125');
15 + assert.equal(NumberInput.format('9007199254740993'), '9,007,199,254,740,993');
16 + assert.equal(NumberInput.format(0), '0');
17 +});
18 +
19 +test('parses Persian and Arabic digits, grouping and decimal separator', () => {
20 + assert.equal(NumberInput.parse('۱۲٬۳۴۵٫۱۲۵'), 12345.125);
21 + assert.equal(NumberInput.parse('١٢٬٣٤٥٫١٢٥'), 12345.125);
22 + assert.equal(NumberInput.parse({value: '۲.۵'}), 2.5);
23 + assert.equal(NumberInput.parse('−۲٫۵'), -2.5);
24 + assert.equal(NumberInput.parse(''), 0);
25 + assert.equal(NumberInput.parse('not-a-number'), 0);
26 +});
27 +
28 +test('typing decimals does not turn quantity 2.5 into 25', () => {
29 + const el = fakeInput('2.5');
30 + NumberInput._formatLive(el);
31 + assert.equal(el.value, '2.5');
32 + assert.equal(NumberInput.parse(el), 2.5);
33 +});
34 +
35 +test('live Persian fractional input formats with grouping', () => {
36 + const el = fakeInput('۱۲۳۴۵٫۲۵');
37 + NumberInput._formatLive(el);
38 + assert.equal(el.value, '12,345.25');
39 + assert.equal(NumberInput.parse(el), 12345.25);
40 + assert.equal(el.selectionStart, el.value.length);
41 +});
42 +
43 +test('live editing maintains cursor position around grouped integers', () => {
44 + const el = fakeInput('12345.75', 3);
45 + NumberInput._formatLive(el);
46 + assert.equal(el.value, '12,345.75');
47 + assert.equal(el.selectionStart, 4);
48 +});
49 +
50 +test('typing a decimal point preserves the editable fractional position', () => {
51 + const el = fakeInput('2.');
52 + NumberInput._formatLive(el);
53 + assert.equal(el.value, '2.');
54 + assert.equal(el.selectionStart, 2);
55 +});
56 +
57 +test('initial conversion does not destroy fractional value', () => {
58 + const attributes = {};
59 + const el = {
60 + tagName: 'INPUT', type: 'number', dataset: {}, value: '2.375',
61 + getAttribute(key) { return attributes[key] || null; },
62 + setAttribute(key, value) { attributes[key] = value; }
63 + };
64 + NumberInput._markAndFormat(el);
65 + assert.equal(el.type, 'text');
66 + assert.equal(el.value, '2.375');
67 +});