@pyreon/form 0.12.6 → 0.12.8
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/package.json +6 -6
- package/src/tests/integration.test.tsx +144 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyreon/form",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.8",
|
|
4
4
|
"description": "Signal-based form management for Pyreon",
|
|
5
5
|
"homepage": "https://github.com/pyreon/pyreon/tree/main/packages/form#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -47,12 +47,12 @@
|
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@happy-dom/global-registrator": "^20.8.9",
|
|
50
|
-
"@pyreon/core": "^0.12.
|
|
51
|
-
"@pyreon/reactivity": "^0.12.
|
|
52
|
-
"@pyreon/runtime-dom": "^0.12.
|
|
50
|
+
"@pyreon/core": "^0.12.8",
|
|
51
|
+
"@pyreon/reactivity": "^0.12.8",
|
|
52
|
+
"@pyreon/runtime-dom": "^0.12.8"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"@pyreon/core": "^0.12.
|
|
56
|
-
"@pyreon/reactivity": "^0.12.
|
|
55
|
+
"@pyreon/core": "^0.12.8",
|
|
56
|
+
"@pyreon/reactivity": "^0.12.8"
|
|
57
57
|
}
|
|
58
58
|
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { mount } from '@pyreon/runtime-dom'
|
|
2
|
+
import { useField, useForm } from '../index'
|
|
3
|
+
|
|
4
|
+
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
5
|
+
|
|
6
|
+
function Capture<T>({ fn }: { fn: () => T }) {
|
|
7
|
+
fn()
|
|
8
|
+
return null
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function mountWith<T>(fn: () => T): { result: T; unmount: () => void } {
|
|
12
|
+
let result: T | undefined
|
|
13
|
+
const el = document.createElement('div')
|
|
14
|
+
document.body.appendChild(el)
|
|
15
|
+
const unmount = mount(
|
|
16
|
+
<Capture
|
|
17
|
+
fn={() => {
|
|
18
|
+
result = fn()
|
|
19
|
+
}}
|
|
20
|
+
/>,
|
|
21
|
+
el,
|
|
22
|
+
)
|
|
23
|
+
return {
|
|
24
|
+
result: result!,
|
|
25
|
+
unmount: () => {
|
|
26
|
+
unmount()
|
|
27
|
+
el.remove()
|
|
28
|
+
},
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type SignupForm = {
|
|
33
|
+
username: string
|
|
34
|
+
email: string
|
|
35
|
+
password: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ─── Integration tests ───────────────────────────────────────────────────────
|
|
39
|
+
|
|
40
|
+
describe('Form integration', () => {
|
|
41
|
+
it('useForm with initialValues provides correct field values', () => {
|
|
42
|
+
const { result: form, unmount } = mountWith(() =>
|
|
43
|
+
useForm<SignupForm>({
|
|
44
|
+
initialValues: { username: 'alice', email: 'alice@test.com', password: 'secret' },
|
|
45
|
+
onSubmit: () => {
|
|
46
|
+
/* noop */
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
expect(form.fields.username.value()).toBe('alice')
|
|
52
|
+
expect(form.fields.email.value()).toBe('alice@test.com')
|
|
53
|
+
expect(form.fields.password.value()).toBe('secret')
|
|
54
|
+
expect(form.isDirty()).toBe(false)
|
|
55
|
+
expect(form.isValid()).toBe(true)
|
|
56
|
+
unmount()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('useField tracks form state for a specific field', () => {
|
|
60
|
+
const { result, unmount } = mountWith(() => {
|
|
61
|
+
const form = useForm<SignupForm>({
|
|
62
|
+
initialValues: { username: '', email: '', password: '' },
|
|
63
|
+
onSubmit: () => {
|
|
64
|
+
/* noop */
|
|
65
|
+
},
|
|
66
|
+
})
|
|
67
|
+
const field = useField(form, 'email')
|
|
68
|
+
return { form, field }
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
expect(result.field.value()).toBe('')
|
|
72
|
+
result.form.fields.email.setValue('test@example.com')
|
|
73
|
+
expect(result.field.value()).toBe('test@example.com')
|
|
74
|
+
unmount()
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('handleSubmit calls onSubmit with current values', async () => {
|
|
78
|
+
let submitted: SignupForm | undefined
|
|
79
|
+
const { result: form, unmount } = mountWith(() =>
|
|
80
|
+
useForm<SignupForm>({
|
|
81
|
+
initialValues: { username: 'bob', email: 'bob@test.com', password: '12345678' },
|
|
82
|
+
onSubmit: (values) => {
|
|
83
|
+
submitted = values
|
|
84
|
+
},
|
|
85
|
+
}),
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
await form.handleSubmit()
|
|
89
|
+
expect(submitted).toEqual({
|
|
90
|
+
username: 'bob',
|
|
91
|
+
email: 'bob@test.com',
|
|
92
|
+
password: '12345678',
|
|
93
|
+
})
|
|
94
|
+
expect(form.submitCount()).toBe(1)
|
|
95
|
+
unmount()
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('validation error is accessible on field', async () => {
|
|
99
|
+
const { result: form, unmount } = mountWith(() =>
|
|
100
|
+
useForm<SignupForm>({
|
|
101
|
+
initialValues: { username: '', email: '', password: '' },
|
|
102
|
+
validators: {
|
|
103
|
+
username: (v) => (!v ? 'Username is required' : undefined),
|
|
104
|
+
email: (v) => (!v ? 'Email is required' : undefined),
|
|
105
|
+
},
|
|
106
|
+
onSubmit: () => {
|
|
107
|
+
/* noop */
|
|
108
|
+
},
|
|
109
|
+
}),
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
await form.handleSubmit()
|
|
113
|
+
expect(form.fields.username.error()).toBe('Username is required')
|
|
114
|
+
expect(form.fields.email.error()).toBe('Email is required')
|
|
115
|
+
expect(form.isValid()).toBe(false)
|
|
116
|
+
unmount()
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
it('reset restores fields to initial values', async () => {
|
|
120
|
+
const { result: form, unmount } = mountWith(() =>
|
|
121
|
+
useForm<SignupForm>({
|
|
122
|
+
initialValues: { username: '', email: '', password: '' },
|
|
123
|
+
validators: {
|
|
124
|
+
username: (v) => (!v ? 'Required' : undefined),
|
|
125
|
+
},
|
|
126
|
+
onSubmit: () => {
|
|
127
|
+
/* noop */
|
|
128
|
+
},
|
|
129
|
+
}),
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
form.fields.username.setValue('changed')
|
|
133
|
+
form.fields.username.setTouched()
|
|
134
|
+
await form.handleSubmit()
|
|
135
|
+
|
|
136
|
+
form.reset()
|
|
137
|
+
expect(form.fields.username.value()).toBe('')
|
|
138
|
+
expect(form.fields.username.error()).toBeUndefined()
|
|
139
|
+
expect(form.fields.username.touched()).toBe(false)
|
|
140
|
+
expect(form.fields.username.dirty()).toBe(false)
|
|
141
|
+
expect(form.submitCount()).toBe(0)
|
|
142
|
+
unmount()
|
|
143
|
+
})
|
|
144
|
+
})
|