oxform-core 0.1.0 → 0.1.1
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/dist/index.cjs +751 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +443 -0
- package/dist/index.d.ts +443 -0
- package/dist/index.js +747 -0
- package/dist/index.js.map +1 -0
- package/package.json +20 -15
- package/export/index.ts +0 -7
- package/export/schema.ts +0 -1
- package/src/field-api.constants.ts +0 -15
- package/src/field-api.ts +0 -139
- package/src/form-api.ts +0 -84
- package/src/form-api.types.ts +0 -148
- package/src/form-array-field-api.ts +0 -233
- package/src/form-context-api.ts +0 -232
- package/src/form-field-api.ts +0 -174
- package/src/more-types.ts +0 -178
- package/src/tests/array/append.spec.ts +0 -138
- package/src/tests/array/insert.spec.ts +0 -182
- package/src/tests/array/move.spec.ts +0 -175
- package/src/tests/array/prepend.spec.ts +0 -138
- package/src/tests/array/remove.spec.ts +0 -174
- package/src/tests/array/swap.spec.ts +0 -152
- package/src/tests/array/update.spec.ts +0 -148
- package/src/tests/field/change.spec.ts +0 -226
- package/src/tests/field/reset.spec.ts +0 -617
- package/src/tests/field/set-errors.spec.ts +0 -254
- package/src/tests/field-api/field-api.spec.ts +0 -341
- package/src/tests/form-api/reset.spec.ts +0 -535
- package/src/tests/form-api/submit.spec.ts +0 -409
- package/src/types.ts +0 -5
- package/src/utils/get.ts +0 -5
- package/src/utils/testing/sleep.ts +0 -1
- package/src/utils/testing/tests.ts +0 -18
- package/src/utils/update.ts +0 -6
- package/src/utils/validate.ts +0 -8
- package/tsconfig.json +0 -3
- package/tsdown.config.ts +0 -10
- package/vitest.config.ts +0 -3
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { FormApi } from '#form-api';
|
|
2
|
-
import type { FormOptions } from '#form-api.types';
|
|
3
|
-
import { sleep } from '#utils/testing/sleep';
|
|
4
|
-
import { describe, expect, it } from 'vitest';
|
|
5
|
-
import z from 'zod';
|
|
6
|
-
|
|
7
|
-
const schema = z.object({
|
|
8
|
-
string: z.string(),
|
|
9
|
-
optional: z.string().optional(),
|
|
10
|
-
nullable: z.string().nullable(),
|
|
11
|
-
nullish: z.string().nullish(),
|
|
12
|
-
number: z.number(),
|
|
13
|
-
boolean: z.boolean(),
|
|
14
|
-
array: z.string().array(),
|
|
15
|
-
object: z.object({
|
|
16
|
-
nested: z.object({
|
|
17
|
-
deep: z.string(),
|
|
18
|
-
}),
|
|
19
|
-
}),
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
const defaultValues = {
|
|
23
|
-
string: 'string',
|
|
24
|
-
optional: undefined,
|
|
25
|
-
nullable: null,
|
|
26
|
-
nullish: null,
|
|
27
|
-
number: 1,
|
|
28
|
-
boolean: true,
|
|
29
|
-
array: ['array'],
|
|
30
|
-
object: {
|
|
31
|
-
nested: {
|
|
32
|
-
deep: 'deep',
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const setup = (options?: Partial<FormOptions<typeof schema>>) => {
|
|
38
|
-
const form = new FormApi({
|
|
39
|
-
schema,
|
|
40
|
-
defaultValues,
|
|
41
|
-
...options,
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
form['~mount']();
|
|
45
|
-
|
|
46
|
-
return { form };
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
describe('value updates', () => {
|
|
50
|
-
it('should update string field value', () => {
|
|
51
|
-
const { form } = setup();
|
|
52
|
-
|
|
53
|
-
form.field.change('string', 'updated');
|
|
54
|
-
|
|
55
|
-
expect(form.field.get('string')).toBe('updated');
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('should update optional field value', () => {
|
|
59
|
-
const { form } = setup();
|
|
60
|
-
|
|
61
|
-
form.field.change('optional', 'updated');
|
|
62
|
-
|
|
63
|
-
expect(form.field.get('optional')).toBe('updated');
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('should update nullable field value', () => {
|
|
67
|
-
const { form } = setup();
|
|
68
|
-
|
|
69
|
-
form.field.change('nullable', 'updated');
|
|
70
|
-
|
|
71
|
-
expect(form.field.get('nullable')).toBe('updated');
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('should update nullish field value', () => {
|
|
75
|
-
const { form } = setup();
|
|
76
|
-
|
|
77
|
-
form.field.change('nullish', 'updated');
|
|
78
|
-
|
|
79
|
-
expect(form.field.get('nullish')).toBe('updated');
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('should update number field value', () => {
|
|
83
|
-
const { form } = setup();
|
|
84
|
-
|
|
85
|
-
form.field.change('number', 2);
|
|
86
|
-
|
|
87
|
-
expect(form.field.get('number')).toBe(2);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('should update boolean field value', () => {
|
|
91
|
-
const { form } = setup();
|
|
92
|
-
|
|
93
|
-
form.field.change('boolean', false);
|
|
94
|
-
|
|
95
|
-
expect(form.field.get('boolean')).toBe(false);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('should update array field value', () => {
|
|
99
|
-
const { form } = setup();
|
|
100
|
-
|
|
101
|
-
const newArray = ['new1', 'new2'];
|
|
102
|
-
form.field.change('array', newArray);
|
|
103
|
-
|
|
104
|
-
expect(form.field.get('array')).toStrictEqual(newArray);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('should update an array index field value', () => {
|
|
108
|
-
const { form } = setup();
|
|
109
|
-
|
|
110
|
-
form.field.change('array.2', 'updated');
|
|
111
|
-
|
|
112
|
-
expect(form.field.get('array.2')).toEqual('updated');
|
|
113
|
-
expect(form.field.get('array')).toEqual(['array', undefined, 'updated']);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it('should update object field value', () => {
|
|
117
|
-
const { form } = setup();
|
|
118
|
-
|
|
119
|
-
const newObject = {
|
|
120
|
-
nested: {
|
|
121
|
-
deep: 'new',
|
|
122
|
-
},
|
|
123
|
-
};
|
|
124
|
-
form.field.change('object', newObject);
|
|
125
|
-
|
|
126
|
-
expect(form.field.get('object')).toStrictEqual(newObject);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it('should update nested field value', () => {
|
|
130
|
-
const { form } = setup();
|
|
131
|
-
|
|
132
|
-
form.field.change('object.nested.deep', 'updated');
|
|
133
|
-
|
|
134
|
-
expect(form.field.get('object.nested.deep')).toBe('updated');
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it('should allow passing an updater function', () => {
|
|
138
|
-
const { form } = setup();
|
|
139
|
-
|
|
140
|
-
form.field.change('string', value => value + ' updated');
|
|
141
|
-
|
|
142
|
-
expect(form.field.get('string')).toBe('string updated');
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
describe('should update field metadata', () => {
|
|
147
|
-
it('on first change', () => {
|
|
148
|
-
const { form } = setup();
|
|
149
|
-
|
|
150
|
-
form.field.change('string', 'updated');
|
|
151
|
-
|
|
152
|
-
expect(form.field.meta('string').dirty).toBe(true);
|
|
153
|
-
expect(form.field.meta('string').touched).toBe(true);
|
|
154
|
-
expect(form.field.meta('string').blurred).toBe(false);
|
|
155
|
-
expect(form.field.meta('string').pristine).toBe(false);
|
|
156
|
-
expect(form.field.meta('string').default).toBe(false);
|
|
157
|
-
expect(form.field.meta('string').valid).toBe(true);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it('on second change', () => {
|
|
161
|
-
const { form } = setup();
|
|
162
|
-
|
|
163
|
-
form.field.change('string', 'updated');
|
|
164
|
-
form.field.change('string', 'updated again');
|
|
165
|
-
|
|
166
|
-
expect(form.field.meta('string').dirty).toBe(true);
|
|
167
|
-
expect(form.field.meta('string').touched).toBe(true);
|
|
168
|
-
expect(form.field.meta('string').blurred).toBe(false);
|
|
169
|
-
expect(form.field.meta('string').pristine).toBe(false);
|
|
170
|
-
expect(form.field.meta('string').default).toBe(false);
|
|
171
|
-
expect(form.field.meta('string').valid).toBe(true);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it('should not mark fields as dirty if should.dirty is false', () => {
|
|
175
|
-
const { form } = setup();
|
|
176
|
-
|
|
177
|
-
form.field.change('string', 'updated', { should: { dirty: false } });
|
|
178
|
-
|
|
179
|
-
expect(form.field.meta('string').dirty).toBe(false);
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
it('should not mark fields as touched if should.touch is false', () => {
|
|
183
|
-
const { form } = setup();
|
|
184
|
-
|
|
185
|
-
form.field.change('string', 'updated', { should: { touch: false } });
|
|
186
|
-
|
|
187
|
-
expect(form.field.meta('string').touched).toBe(false);
|
|
188
|
-
});
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
describe('if form.options.validate.change is provided ', () => {
|
|
192
|
-
it('should validate on update by default', async () => {
|
|
193
|
-
const { form } = setup({ validate: { change: schema } });
|
|
194
|
-
|
|
195
|
-
form.field.change('string', 2 as any);
|
|
196
|
-
|
|
197
|
-
await sleep(0);
|
|
198
|
-
|
|
199
|
-
expect(form.field.get('string')).toBe(2);
|
|
200
|
-
expect(form.field.meta('string').valid).toBe(false);
|
|
201
|
-
expect(form.field.errors('string')).toHaveLength(1);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('should skip validation if should.validate is false', async () => {
|
|
205
|
-
const { form } = setup({ validate: { change: schema } });
|
|
206
|
-
|
|
207
|
-
form.field.change('string', 2 as any, { should: { validate: false } });
|
|
208
|
-
|
|
209
|
-
expect(form.field.get('string')).toBe(2);
|
|
210
|
-
expect(form.field.meta('string').valid).toBe(true);
|
|
211
|
-
expect(form.field.errors('string')).toHaveLength(0);
|
|
212
|
-
});
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
describe('if form.options.validate.change is not provided ', () => {
|
|
216
|
-
it('should not validate on update by default', async () => {
|
|
217
|
-
const { form } = setup();
|
|
218
|
-
|
|
219
|
-
form.field.change('string', 2 as any);
|
|
220
|
-
await sleep(0);
|
|
221
|
-
|
|
222
|
-
expect(form.field.get('string')).toBe(2);
|
|
223
|
-
expect(form.field.meta('string').valid).toBe(true);
|
|
224
|
-
expect(form.field.errors('string')).toHaveLength(0);
|
|
225
|
-
});
|
|
226
|
-
});
|