刘光辉
9 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
import { describe, expect, it } from 'vitest';
 
import { getFormDataOptionLabel, getFormDataOptions } from './formDataOptions';
 
describe('getFormDataOptions', () => {
  it('reads options from the configured child-table field', () => {
    const formData = {
      archiveBoxes: [{ code: 'BOX-001', name: 'A' }],
      archiveItems: [{ code: 'ITEM-001' }],
    };
 
    expect(getFormDataOptions({ dataType: 'formData', formDataSource: 'archiveBoxes' }, formData)).toEqual([{ code: 'BOX-001', name: 'A' }]);
  });
 
  it('returns an empty list when the source is missing or is not a child-table array', () => {
    expect(getFormDataOptions({ dataType: 'formData', formDataSource: 'archiveBoxes' }, {})).toEqual([]);
    expect(getFormDataOptions({ dataType: 'formData', formDataSource: 'archiveBoxes' }, { archiveBoxes: {} })).toEqual([]);
    expect(getFormDataOptions({ dataType: 'static', formDataSource: 'archiveBoxes' }, { archiveBoxes: [] })).toEqual([]);
  });
 
  it('does not expose the source rows as mutable option objects', () => {
    const formData = { archiveBoxes: [{ code: 'BOX-001' }] };
    const options = getFormDataOptions({ dataType: 'formData', formDataSource: 'archiveBoxes' }, formData);
 
    options[0].code = 'CHANGED';
 
    expect(formData.archiveBoxes[0].code).toBe('BOX-001');
  });
 
  it('resolves stored values to the configured display field and keeps unmatched values', () => {
    const config = {
      dataType: 'formData',
      formDataSource: 'archiveBoxes',
      props: { value: 'id', label: 'code' },
    };
    const formData = { archiveBoxes: [{ id: 1, code: 'BOX-001' }] };
 
    expect(getFormDataOptionLabel(config, formData, '1')).toBe('BOX-001');
    expect(getFormDataOptionLabel(config, formData, ['1', '2'])).toEqual(['BOX-001', '2']);
    expect(getFormDataOptionLabel(config, formData, '2')).toBe('2');
    expect(getFormDataOptionLabel({ __config__: config, props: config.props }, formData, '1')).toBe('BOX-001');
  });
});