刘光辉
8 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
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
import { describe, expect, it } from 'vitest';
 
import { buildDraftFields } from '../../src/domain/draft';
 
const field = (tag: string, type: any, currentValue: boolean | string) => ({ alias: tag, currentValue, internalId: `id-${tag}`, options: [], tag, type });
 
describe('buildDraftFields', () => {
  it('fills only empty standard fields and always applies checkbox data', () => {
    const fields = buildDraftFields(
      [field('eln.field.empty', 'text', ''), field('eln.field.kept', 'text', 'document'), field('eln.field.ok', 'checkbox', true)],
      {
        fields: {
          'eln.field.empty': { type: 'text', value: 'api' },
          'eln.field.kept': { type: 'text', value: 'api' },
          'eln.field.ok': { type: 'checkbox', value: false },
        },
        repeatGroups: [],
      },
    );
    expect(fields.map((item) => item.draftValue)).toEqual(['api', 'document', false]);
  });
 
  it('keeps document fields when API fields are empty', () => {
    expect(buildDraftFields([field('eln.field.name', 'text', 'A')], { fields: {}, repeatGroups: [] })).toHaveLength(1);
  });
 
  it('restores record current state into both a primary field and its readonly reference', () => {
    const fields = buildDraftFields(
      [field('eln.field.name', 'text', 'stale primary'), field('eln.ref.name.instance_1', 'text', 'stale reference')],
      {
        fields: { 'eln.field.name': { type: 'text', value: 'authoritative value' } },
        repeatGroups: [],
      },
      { preferData: true },
    );
 
    expect(fields.map((item) => item.draftValue)).toEqual(['authoritative value', 'authoritative value']);
  });
 
  it('carries cross-template readonly metadata into the primary draft field', () => {
    const fields = buildDraftFields(
      [field('eln.field.source_result', 'text', '旧值')],
      {
        fields: {
          'eln.field.source_result': {
            readOnly: true,
            type: 'text',
            value: '快照值',
            valueSource: 'CROSS_TEMPLATE',
          },
        },
        repeatGroups: [],
      },
      { preferData: true },
    );
 
    expect(fields[0]).toMatchObject({
      draftValue: '快照值',
      readOnly: true,
      valueSource: 'CROSS_TEMPLATE',
    });
  });
 
  it('maps a dotted reference tag back to its dotted primary field', () => {
    const fields = buildDraftFields(
      [field('eln.ref.inspection.result.instance_1', 'text', 'stale reference')],
      {
        fields: { 'eln.field.inspection.result': { type: 'text', value: 'authoritative value' } },
        repeatGroups: [],
      },
      { preferData: true },
    );
 
    expect(fields[0]?.draftValue).toBe('authoritative value');
  });
 
  it('inherits repeat column required metadata without copying another row value', () => {
    const fields = buildDraftFields(
      [field('eln.repeat.results.row-2.value', 'text', '')],
      {
        fields: {
          'eln.repeat.results.row-1.value': { required: true, type: 'text', value: '第一行' },
        },
        repeatGroups: [],
      },
    );
 
    expect(fields[0]).toMatchObject({ draftValue: '', required: true, requiredKnown: true });
  });
 
  it('carries checkbox groups to editable primary and repeated fields but not references', () => {
    const fields = buildDraftFields(
      [
        field('eln.field.passed', 'checkbox', false),
        field('eln.ref.passed.instance-1', 'checkbox', false),
        field('eln.repeat.results.row-2.qualified', 'checkbox', false),
      ],
      {
        fields: {
          'eln.field.passed': {
            checkboxGroupCode: 'inspection_result', checkboxGroupMaxSelected: 2, checkboxGroupMinSelected: 1, type: 'checkbox', value: false,
          },
          'eln.repeat.results.row-1.qualified': {
            checkboxGroupCode: 'row_result', checkboxGroupMaxSelected: null, checkboxGroupMinSelected: 1, type: 'checkbox', value: true,
          },
        },
        repeatGroups: [{
          activeRowCount: 0,
          checkboxGroups: { qualified: 'row_result' },
          groupCode: 'results',
          maxRows: 5,
          minRows: 0,
          rows: [],
        }],
      },
    );
 
    expect(fields[0]).toMatchObject({
      checkboxGroupCode: 'inspection_result', checkboxGroupMaxSelected: 2, checkboxGroupMinSelected: 1,
    });
    expect(fields[1]).not.toHaveProperty('checkboxGroupCode');
    expect(fields[2]).toMatchObject({
      checkboxGroupCode: 'row_result', checkboxGroupMaxSelected: null, checkboxGroupMinSelected: 1,
    });
  });
 
  it('can retain metadata while ignoring stale values during a document structure refresh', () => {
    const fields = buildDraftFields(
      [field('eln.field.confirmed', 'checkbox', true)],
      {
        fields: { 'eln.field.confirmed': { required: true, type: 'checkbox', value: false } },
        repeatGroups: [],
      },
      { ignoreDataValues: true },
    );
 
    expect(fields[0]).toMatchObject({ draftValue: true, required: true, requiredKnown: true });
  });
});