number-input-sanitize.test.mjs
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
describe('number input sanitize', async () => {
const {
isNonNegativeIntegerInput,
sanitizeNonNegativeIntegerInput,
normalizeNonNegativeIntegerDisplay,
coerceNonNegativeIntegerInputValue,
isDecimalInput,
sanitizeDecimalInput,
normalizeDecimalDisplay,
coerceDecimalInputValue
} = await import('../form-engine/field-input-limits.js')
const intField = { type: 'numberInput', rules: { min: 0, integer: true } }
const decField = { type: 'numberInput', rules: { min: 0, decimal: true } }
it('IT-NUM-INPUT-001: sanitize strips non-digits', () => {
assert.equal(sanitizeNonNegativeIntegerInput('12a-3.5', intField), '1235')
})
it('IT-NUM-INPUT-002: min:0 integer keeps zero', () => {
assert.equal(isNonNegativeIntegerInput({ min: 0, integer: true }), true)
assert.equal(sanitizeNonNegativeIntegerInput('0', intField), '0')
assert.equal(coerceNonNegativeIntegerInputValue('0'), 0)
})
it('IT-NUM-INPUT-003: decimal mode does not use non-negative integer flag', () => {
assert.equal(isNonNegativeIntegerInput({ min: 0, decimal: true }), false)
assert.equal(sanitizeNonNegativeIntegerInput('1.2', intField), '12')
})
it('IT-NUM-INPUT-004: negative min skips non-negative integer mode', () => {
assert.equal(isNonNegativeIntegerInput({ min: -1, integer: true }), false)
})
it('IT-NUM-INPUT-005: numberInput without rules.integer defaults to non-negative integer', () => {
const bareField = { type: 'numberInput', key: 'F-COUNT', rules: { min: 0 } }
assert.equal(isNonNegativeIntegerInput(bareField), true)
assert.equal(isDecimalInput(bareField), false)
assert.equal(sanitizeNonNegativeIntegerInput('-1.5a', bareField), '15')
})
it('normalizeNonNegativeIntegerDisplay removes leading zeros', () => {
assert.equal(normalizeNonNegativeIntegerDisplay('012'), '12')
assert.equal(normalizeNonNegativeIntegerDisplay('0'), '0')
assert.equal(normalizeNonNegativeIntegerDisplay(''), '')
})
it('IT-NUM-DEC-001: sanitizeDecimalInput strips illegal chars and keeps one dot', () => {
assert.equal(sanitizeDecimalInput('12a-3.45x', decField), '123.45')
})
it('IT-NUM-DEC-002: sanitizeDecimalInput caps fraction at two digits', () => {
assert.equal(sanitizeDecimalInput('1.234', decField), '1.23')
})
it('IT-NUM-DEC-003: min:0 decimal keeps zero and one decimal place', () => {
assert.equal(isDecimalInput({ min: 0, decimal: true }), true)
assert.equal(sanitizeDecimalInput('0', decField), '0')
assert.equal(sanitizeDecimalInput('0.5', decField), '0.5')
assert.equal(coerceDecimalInputValue('0.5'), 0.5)
})
it('IT-NUM-DEC-004: normalizeDecimalDisplay removes trailing dot', () => {
assert.equal(normalizeDecimalDisplay('12.'), '12')
})
it('IT-NUM-DEC-005: merged childrenRules shape uses decimal input mode', () => {
const field = {
type: 'numberInput',
key: 'F1-3',
rules: { min: 0, decimal: true }
}
assert.equal(isDecimalInput(field), true)
assert.equal(isNonNegativeIntegerInput(field), false)
})
it('IT-NUM-DEC-006: sanitizeDecimalInput strips leading zeros on integer part', () => {
assert.equal(sanitizeDecimalInput('007', decField), '7')
assert.equal(normalizeDecimalDisplay('007'), '7')
})
it('IT-NUM-DEC-007: sanitizeDecimalInput normalizes 00.5 to 0.5', () => {
assert.equal(sanitizeDecimalInput('00.5', decField), '0.5')
assert.equal(normalizeDecimalDisplay('00.5'), '0.5')
})
it('IT-NUM-DEC-008: lone dot becomes 0. on input and 0 on blur', () => {
assert.equal(sanitizeDecimalInput('.', decField), '0.')
assert.equal(normalizeDecimalDisplay('.'), '0')
assert.equal(normalizeDecimalDisplay('0.'), '0')
})
it('IT-NUM-DEC-009: leading dot becomes 0.5', () => {
assert.equal(sanitizeDecimalInput('.5', decField), '0.5')
assert.equal(normalizeDecimalDisplay('.5'), '0.5')
})
it('IT-NUM-DEC-010: sanitizeDecimalInput strips leading zeros before decimal', () => {
assert.equal(sanitizeDecimalInput('01.23', decField), '1.23')
assert.equal(normalizeDecimalDisplay('01.23'), '1.23')
})
it('IT-NUM-DEC-011: trailing dot kept on input, stripped on blur', () => {
assert.equal(sanitizeDecimalInput('12.', decField), '12.')
assert.equal(normalizeDecimalDisplay('12.'), '12')
})
it('IT-NUM-DEC-012: sanitizeDecimalInput respects maxLength', () => {
const longField = {
type: 'numberInput',
rules: { min: 0, decimal: true, maxLength: 5 }
}
assert.equal(sanitizeDecimalInput('123456.78', longField), '12345')
})
it('IT-NUM-INPUT-006: validator rejects decimal on default integer numberInput', async () => {
const { validateForm } = await import('../form-engine/validator.js')
const list = [
{
key: 'F-COUNT',
type: 'numberInput',
label: '数量',
rules: { min: 0 }
}
]
const result = validateForm(list, {}, { 'F-COUNT': 1.5 })
assert.equal(result.ok, false)
assert.ok(
result.errors.some((e) => String(e.message).includes('必须为整数'))
)
})
})