gaoluyang
2026-06-24 c0cb161bb52ce0fbdce5c66ec391d107c75e2452
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
import { beforeEach, describe, expect, it, vi } from 'vitest';
 
import { FormApi } from '../src/form-api';
 
describe('formApi', () => {
  let formApi: FormApi;
 
  beforeEach(() => {
    formApi = new FormApi();
  });
 
  it('should initialize with default state', () => {
    expect(formApi.state).toEqual(
      expect.objectContaining({
        actionWrapperClass: '',
        collapsed: false,
        collapsedRows: 1,
        commonConfig: {},
        handleReset: undefined,
        handleSubmit: undefined,
        layout: 'horizontal',
        resetButtonOptions: {},
        schema: [],
        showCollapseButton: false,
        showDefaultActions: true,
        submitButtonOptions: {},
        wrapperClass: 'grid-cols-1',
      }),
    );
    expect(formApi.isMounted).toBe(false);
  });
 
  it('should mount form actions', async () => {
    const formActions: any = {
      meta: {},
      resetForm: vi.fn(),
      setFieldValue: vi.fn(),
      setValues: vi.fn(),
      submitForm: vi.fn(),
      validate: vi.fn(),
      values: { name: 'test' },
    };
 
    await formApi.mount(formActions);
    expect(formApi.isMounted).toBe(true);
    expect(formApi.form).toEqual(formActions);
    expect(formApi.getFieldComponentRef('name')).toBeUndefined();
  });
 
  it('should get values from form', async () => {
    const formActions: any = {
      meta: {},
      values: { name: 'test' },
    };
 
    await formApi.mount(formActions, new Map());
    const values = await formApi.getValues();
    expect(values).toEqual({ name: 'test' });
  });
 
  it('should format schema values when getting values', async () => {
    formApi.setState({
      schema: [
        {
          component: 'range-picker',
          fieldName: 'filters.range',
          valueFormat: (value, setValue) => {
            setValue('filters.startTime', value?.[0]);
            setValue('filters.endTime', value?.[1]);
          },
        },
      ],
    });
 
    const formActions: any = {
      meta: {},
      values: {
        filters: {
          range: [1_710_000_000_000, 1_720_000_000_000],
        },
      },
    };
    const originalValuesSnapshot = structuredClone(formActions.values);
 
    await formApi.mount(formActions, new Map());
 
    expect(formApi.getLatestSubmissionValues()).toEqual({
      filters: {
        endTime: 1_720_000_000_000,
        startTime: 1_710_000_000_000,
      },
    });
 
    const values = await formApi.getValues();
    expect(values).toEqual({
      filters: {
        endTime: 1_720_000_000_000,
        startTime: 1_710_000_000_000,
      },
    });
    expect(formActions.values).toEqual(originalValuesSnapshot);
  });
 
  it('should set field value', async () => {
    const setFieldValueMock = vi.fn();
    const formActions: any = {
      meta: {},
      setFieldValue: setFieldValueMock,
      values: { name: 'test' },
    };
 
    await formApi.mount(formActions, new Map());
    await formApi.setFieldValue('name', 'new value');
    expect(setFieldValueMock).toHaveBeenCalledWith(
      'name',
      'new value',
      undefined,
    );
  });
 
  it('should reset form', async () => {
    const resetFormMock = vi.fn();
    const formActions: any = {
      meta: {},
      resetForm: resetFormMock,
      values: { name: 'test' },
    };
 
    await formApi.mount(formActions, new Map());
    await formApi.resetForm();
    expect(resetFormMock).toHaveBeenCalled();
  });
 
  it('should call handleSubmit on submit', async () => {
    const handleSubmitMock = vi.fn();
    const formActions: any = {
      meta: {},
      submitForm: vi.fn().mockResolvedValue(true),
      values: { name: 'test' },
    };
 
    const state = {
      handleSubmit: handleSubmitMock,
    };
 
    formApi.setState(state);
    await formApi.mount(formActions, new Map());
 
    const result = await formApi.submitForm();
    expect(formActions.submitForm).toHaveBeenCalled();
    expect(handleSubmitMock).toHaveBeenCalledWith({ name: 'test' });
    expect(result).toEqual({ name: 'test' });
  });
 
  it('should unmount form and reset state', () => {
    formApi.unmount();
    expect(formApi.isMounted).toBe(false);
  });
 
  it('should clear component refs on unmount before mounting again', async () => {
    const formActions: any = {
      meta: {},
      resetForm: vi.fn(),
      values: { name: 'test' },
    };
    const staleMap = new Map<string, unknown>([
      [
        'name',
        {
          $: {
            type: { name: 'MockComponent' },
          },
          $el: {},
        },
      ],
    ]);
 
    await formApi.mount(formActions, staleMap);
    expect(formApi.getFieldComponentRef('name')).toEqual({
      $: {
        type: { name: 'MockComponent' },
      },
      $el: {},
    });
 
    formApi.unmount();
    expect(formApi.getFieldComponentRef('name')).toBeUndefined();
 
    await formApi.mount(formActions);
    expect(formApi.getFieldComponentRef('name')).toBeUndefined();
  });
 
  it('should validate form', async () => {
    const validateMock = vi.fn().mockResolvedValue(true);
    const formActions: any = {
      meta: {},
      validate: validateMock,
    };
 
    await formApi.mount(formActions, new Map());
    const isValid = await formApi.validate();
    expect(validateMock).toHaveBeenCalled();
    expect(isValid).toBe(true);
  });
});
 
describe('updateSchema', () => {
  let instance: FormApi;
 
  beforeEach(() => {
    instance = new FormApi();
    instance.state = {
      schema: [
        { component: 'text', fieldName: 'name' },
        { component: 'number', fieldName: 'age', label: 'Age' },
      ],
    };
  });
 
  it('should update the schema correctly when fieldName matches', () => {
    const newSchema = [
      { component: 'text', fieldName: 'name' },
      { component: 'number', fieldName: 'age', label: 'Age' },
    ];
 
    instance.updateSchema(newSchema);
 
    expect(instance.state?.schema?.[0]?.component).toBe('text');
    expect(instance.state?.schema?.[1]?.label).toBe('Age');
  });
 
  it('should log an error if fieldName is missing in some items', () => {
    const newSchema: any[] = [
      { component: 'textarea', fieldName: 'name' },
      { component: 'number' },
    ];
 
    const consoleErrorSpy = vi
      .spyOn(console, 'error')
      .mockImplementation(() => {});
 
    instance.updateSchema(newSchema);
 
    expect(consoleErrorSpy).toHaveBeenCalledWith(
      'All items in the schema array must have a valid `fieldName` property to be updated',
    );
  });
 
  it('should not update schema if fieldName does not match', () => {
    const newSchema = [{ component: 'textarea', fieldName: 'unknown' }];
 
    instance.updateSchema(newSchema);
 
    expect(instance.state?.schema?.[0]?.component).toBe('text');
    expect(instance.state?.schema?.[1]?.component).toBe('number');
  });
 
  it('should not update schema if updatedMap is empty', () => {
    const newSchema: any[] = [{ component: 'textarea' }];
 
    instance.updateSchema(newSchema);
 
    expect(instance.state?.schema?.[0]?.component).toBe('text');
    expect(instance.state?.schema?.[1]?.component).toBe('number');
  });
});