forked from CredenceOrg/Credence-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-amount-input.html
More file actions
310 lines (277 loc) · 10.6 KB
/
Copy pathtest-amount-input.html
File metadata and controls
310 lines (277 loc) · 10.6 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AmountInput Test Page</title>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 2rem;
line-height: 1.5;
}
.test-section {
border: 1px solid #ccc;
padding: 1.5rem;
margin-bottom: 2rem;
border-radius: 8px;
}
.test-section h2 {
margin-top: 0;
}
.result {
margin: 0.5rem 0;
padding: 0.5rem;
border-radius: 4px;
}
.pass {
background-color: #d4edda;
color: #155724;
}
.fail {
background-color: #f8d7da;
color: #721c24;
}
.input-group {
margin: 1rem 0;
}
input {
padding: 0.5rem;
font-size: 1rem;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 0.5rem 1rem;
margin-right: 0.5rem;
cursor: pointer;
background: #007bff;
color: white;
border: none;
border-radius: 4px;
}
button:hover {
background: #0056b3;
}
code {
background: #f4f4f4;
padding: 0.2rem 0.4rem;
border-radius: 3px;
}
</style>
</head>
<body>
<h1>AmountInput USDC Test Suite</h1>
<div class="test-section">
<h2>Utility Function Tests</h2>
<p>Tests for sanitizeUSDCInput, formatUSDC, and normalizeUSDC functions</p>
<button onclick="runUtilityTests()">Run Utility Tests</button>
<div id="utility-results"></div>
</div>
<div class="test-section">
<h2>Manual Input Sanitization Test</h2>
<p>Enter various inputs to test sanitization behavior</p>
<div class="input-group">
<input type="text" id="test-input" placeholder="Enter value to sanitize" />
<button onclick="testSanitization()">Test Sanitization</button>
</div>
<div id="sanitization-result"></div>
</div>
<div class="test-section">
<h2>Format Test</h2>
<p>Enter a number to test formatting</p>
<div class="input-group">
<input type="text" id="format-input" placeholder="Enter value to format" />
<button onclick="testFormatting()">Test Formatting</button>
</div>
<div id="format-result"></div>
</div>
<div class="test-section">
<h2>Normalize Test</h2>
<p>Enter a value to test normalization</p>
<div class="input-group">
<input type="text" id="normalize-input" placeholder="Enter value to normalize" />
<button onclick="testNormalization()">Test Normalization</button>
</div>
<div id="normalize-result"></div>
</div>
<div class="test-section">
<h2>Test Cases Reference</h2>
<h3>Sanitization Test Cases:</h3>
<ul>
<li><code>123.45</code> → <code>123.45</code> (valid decimal)</li>
<li><code>abc123</code> → <code>123</code> (letters removed)</li>
<li><code>12.345</code> → <code>12.34</code> (fraction truncated to 2 decimals)</li>
<li><code>00123</code> → <code>123</code> (leading zeros removed)</li>
<li><code>0.5</code> → <code>0.5</code> (leading zero kept for decimal)</li>
<li><code>12.34.56</code> → <code>12.34</code> (multiple decimals handled)</li>
<li><code>$100.00</code> → <code>100.00</code> (currency symbol removed)</li>
<li><code>1,000.50</code> → <code>1000.50</code> (comma removed)</li>
</ul>
<h3>Format Test Cases:</h3>
<ul>
<li><code>1234.56</code> → <code>1,234.56</code> (thousands separator)</li>
<li><code>100</code> → <code>100.00</code> (add decimal places)</li>
<li><code>1234567.89</code> → <code>1,234,567.89</code> (multiple separators)</li>
<li><code>0.5</code> → <code>0.50</code> (single decimal to two decimals)</li>
</ul>
<h3>Normalize Test Cases:</h3>
<ul>
<li><code>123.456</code> → <code>123.46</code> (round to 2 decimals)</li>
<li><code>123.4</code> → <code>123.40</code> (pad to 2 decimals)</li>
<li><code>1,234.56</code> → <code>1234.56</code> (remove comma)</li>
<li><code>-100</code> → <code>0.00</code> (negative clamped to 0)</li>
</ul>
<h3>Max Button Behavior:</h3>
<ul>
<li>Max button sets value to balance.toFixed(2)</li>
<li>Max button disabled when balance ≤ 0</li>
</ul>
<h3>Preset Button Behavior:</h3>
<ul>
<li>Preset buttons set value to preset.toFixed(2)</li>
<li>Preset buttons disabled when preset > balance</li>
<li>Default presets: [100, 500, 1000]</li>
</ul>
</div>
<script>
// Copy of the utility functions from AmountInput.tsx
function normalizeUSDC(rawValue) {
const trimmed = rawValue.trim()
if (!trimmed) return ''
const normalized = trimmed.replace(/,/g, '')
const numericValue = Number(normalized)
if (!Number.isFinite(numericValue)) return ''
const clamped = Math.max(0, numericValue)
return clamped.toFixed(2)
}
function formatUSDC(rawValue) {
const trimmed = rawValue.trim()
if (!trimmed) return ''
const normalized = trimmed.replace(/,/g, '')
const numericValue = Number(normalized)
if (!Number.isFinite(numericValue)) return rawValue
const numberFormatter = new Intl.NumberFormat(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})
return numberFormatter.format(numericValue)
}
function sanitizeUSDCInput(nextValue) {
const cleaned = nextValue.replace(/[^\d.]/g, '')
const [whole = '', fraction = ''] = cleaned.split('.')
const trimmedWhole = whole.replace(/^0+(?=\d)/, '')
const trimmedFraction = fraction.slice(0, 2)
if (cleaned.includes('.')) return `${trimmedWhole || '0'}.${trimmedFraction}`
return trimmedWhole
}
function runUtilityTests() {
const resultsDiv = document.getElementById('utility-results')
resultsDiv.innerHTML = ''
let passCount = 0
let failCount = 0
// Sanitize tests
const sanitizeTests = [
{ input: '123.45', expected: '123.45', description: 'Valid decimal' },
{ input: 'abc123', expected: '123', description: 'Letters removed' },
{ input: '12.345', expected: '12.34', description: 'Fraction truncated to 2 decimals' },
{ input: '00123', expected: '123', description: 'Leading zeros removed' },
{ input: '0.5', expected: '0.5', description: 'Leading zero kept for decimal' },
{ input: '12.34.56', expected: '12.34', description: 'Multiple decimals handled' },
{ input: '$100.00', expected: '100.00', description: 'Currency symbol removed' },
{ input: '1,000.50', expected: '1000.50', description: 'Comma removed' },
{ input: '', expected: '', description: 'Empty string' },
{ input: '0', expected: '0', description: 'Single zero' },
]
resultsDiv.innerHTML += '<h3>sanitizeUSDCInput Tests</h3>'
sanitizeTests.forEach(({ input, expected, description }) => {
const result = sanitizeUSDCInput(input)
const passed = result === expected
if (passed) passCount++
else failCount++
resultsDiv.innerHTML += `
<div class="result ${passed ? 'pass' : 'fail'}">
${passed ? '✓' : '✗'} ${description}: "${input}" → "${result}" (expected: "${expected}")
</div>
`
})
// Format tests
const formatTests = [
{ input: '1234.56', expected: '1,234.56', description: 'Thousands separator' },
{ input: '100', expected: '100.00', description: 'Add decimal places' },
{ input: '1234567.89', expected: '1,234,567.89', description: 'Multiple thousands separators' },
{ input: '0.5', expected: '0.50', description: 'Single decimal to two decimals' },
{ input: '1,234.56', expected: '1,234.56', description: 'Already formatted' },
{ input: '', expected: '', description: 'Empty string' },
]
resultsDiv.innerHTML += '<h3>formatUSDC Tests</h3>'
formatTests.forEach(({ input, expected, description }) => {
const result = formatUSDC(input)
const passed = result === expected
if (passed) passCount++
else failCount++
resultsDiv.innerHTML += `
<div class="result ${passed ? 'pass' : 'fail'}">
${passed ? '✓' : '✗'} ${description}: "${input}" → "${result}" (expected: "${expected}")
</div>
`
})
// Normalize tests
const normalizeTests = [
{ input: '123.456', expected: '123.46', description: 'Round to 2 decimals' },
{ input: '123.4', expected: '123.40', description: 'Pad to 2 decimals' },
{ input: '1,234.56', expected: '1234.56', description: 'Remove comma' },
{ input: '-100', expected: '0.00', description: 'Negative values clamped to 0' },
{ input: '0', expected: '0.00', description: 'Zero formatted' },
{ input: '', expected: '', description: 'Empty string' },
]
resultsDiv.innerHTML += '<h3>normalizeUSDC Tests</h3>'
normalizeTests.forEach(({ input, expected, description }) => {
const result = normalizeUSDC(input)
const passed = result === expected
if (passed) passCount++
else failCount++
resultsDiv.innerHTML += `
<div class="result ${passed ? 'pass' : 'fail'}">
${passed ? '✓' : '✗'} ${description}: "${input}" → "${result}" (expected: "${expected}")
</div>
`
})
resultsDiv.innerHTML += `<h3>Summary: ${passCount} passed, ${failCount} failed</h3>`
}
function testSanitization() {
const input = document.getElementById('test-input').value
const result = sanitizeUSDCInput(input)
document.getElementById('sanitization-result').innerHTML = `
<div class="result">
Input: "${input}" → Sanitized: "${result}"
</div>
`
}
function testFormatting() {
const input = document.getElementById('format-input').value
const result = formatUSDC(input)
document.getElementById('format-result').innerHTML = `
<div class="result">
Input: "${input}" → Formatted: "${result}"
</div>
`
}
function testNormalization() {
const input = document.getElementById('normalize-input').value
const result = normalizeUSDC(input)
document.getElementById('normalize-result').innerHTML = `
<div class="result">
Input: "${input}" → Normalized: "${result}"
</div>
`
}
// Auto-run tests on page load
window.onload = function() {
runUtilityTests()
}
</script>
</body>
</html>