-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-debounce.ts
More file actions
executable file
·147 lines (124 loc) · 4.83 KB
/
Copy pathtest-debounce.ts
File metadata and controls
executable file
·147 lines (124 loc) · 4.83 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
/**
* Test script to verify per-field debounce configuration
* Checks that schema debounce values are correctly propagated to field descriptors
*/
type Descriptor = {
id: number
name: string
label: string
type: string
debounceDelay?: number
}
const { f } = require('./dist/formular-dev.cjs.js')
// Helper to access private schemaToDescriptors
function schemaToDescriptors(schema, defaultValues) {
const descriptors = []
let id = 1
for (const key in schema.shape) {
if (Object.prototype.hasOwnProperty.call(schema.shape, key)) {
const fieldSchema = schema.shape[key]
const defaultValue = defaultValues?.[key]
// Determine input type from schema
let inputType = 'text'
const protoName = Object.getPrototypeOf(fieldSchema).constructor.name
if (protoName === 'NumberSchema') {
inputType = 'number'
} else if (protoName === 'BooleanSchema') {
inputType = 'checkbox'
} else if (protoName === 'DateSchema') {
inputType = 'date'
} else if (protoName === 'StringSchema') {
inputType = 'text'
}
// Extract debounce delay from schema if set
const debounceDelay = fieldSchema._debounce
descriptors.push({
id,
name: key,
label: key.charAt(0).toUpperCase() + key.slice(1),
type: inputType,
debounceDelay
})
id++
}
}
return descriptors
}
// Test 1: String field with custom debounce
const testSchema1 = f.object({
username: f.string().min(3).max(20).nonempty().debounce(200),
email: f.string().email().nonempty(),
bio: f.string().min(10).max(200).optional()
})
console.log('\n✅ TEST 1: String schema with custom debounce')
const descriptors1 = (
schemaToDescriptors as unknown as (schema: unknown, defaultValues: unknown) => Descriptor[]
)(testSchema1, {})
const usernameField = descriptors1.find((d: Descriptor) => d.name === 'username')
const emailField = descriptors1.find((d: Descriptor) => d.name === 'email')
if (usernameField?.debounceDelay === 200) {
console.log(' ✓ username field has debounceDelay: 200ms')
} else {
console.log(
` ✗ FAILED: username debounceDelay is ${usernameField?.debounceDelay}, expected 200`
)
}
if (emailField?.debounceDelay === undefined) {
console.log(' ✓ email field has no debounceDelay (uses default)')
} else {
console.log(
` ✗ FAILED: email debounceDelay should be undefined, got ${emailField?.debounceDelay}`
)
}
// Test 2: Number field with custom debounce
const testSchema2 = f.object({
age: f.number().min(0).max(120).debounce(300),
score: f.number().min(0).max(100)
})
console.log('\n✅ TEST 2: Number schema with custom debounce')
const descriptors2 = (
schemaToDescriptors as unknown as (schema: unknown, defaultValues: unknown) => Descriptor[]
)(testSchema2, {})
const ageField = descriptors2.find((d: Descriptor) => d.name === 'age')
const scoreField = descriptors2.find((d: Descriptor) => d.name === 'score')
if (ageField?.debounceDelay === 300) {
console.log(' ✓ age field has debounceDelay: 300ms')
} else {
console.log(` ✗ FAILED: age debounceDelay is ${ageField?.debounceDelay}, expected 300`)
}
if (scoreField?.debounceDelay === undefined) {
console.log(' ✓ score field has no debounceDelay (uses default)')
} else {
console.log(
` ✗ FAILED: score debounceDelay should be undefined, got ${scoreField?.debounceDelay}`
)
}
// Test 3: Check _debounce internal property
const stringSchema = f.string().debounce(500)
const numberSchema = f.number().debounce(600)
console.log('\n✅ TEST 3: Schema internal _debounce property')
if ((stringSchema as { _debounce?: number })._debounce === 500) {
console.log(' ✓ string schema._debounce is 500ms')
} else {
console.log(
` ✗ FAILED: string schema._debounce is ${(stringSchema as { _debounce?: number })._debounce}, expected 500`
)
}
if ((numberSchema as { _debounce?: number })._debounce === 600) {
console.log(' ✓ number schema._debounce is 600ms')
} else {
console.log(
` ✗ FAILED: number schema._debounce is ${(numberSchema as { _debounce?: number })._debounce}, expected 600`
)
}
// Test 4: Method chaining
const chainedSchema = f.string().min(3).debounce(250).max(20).nonempty()
console.log('\n✅ TEST 4: Method chaining with debounce')
if ((chainedSchema as { _debounce?: number })._debounce === 250) {
console.log(' ✓ debounce works correctly in method chain')
} else {
console.log(
` ✗ FAILED: chained schema._debounce is ${(chainedSchema as { _debounce?: number })._debounce}, expected 250`
)
}
console.log('\n✅ All tests completed!\n')