subform-max-rows.test.mjs
2.24 KB
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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createFormEngine } from '../form-engine/core.js'
import { resolveSubformMaxRows } from '../form-engine/subform-limits.js'
import { SUBFORM_MAX_ROWS } from '../config/formFieldLimits.js'
import { validateForm } from '../form-engine/validator.js'
import { normalizeSchemaRoot } from '../form-engine/schema-normalizer.js'
const rowTemplate = [
{ key: 'c1', label: '数量', type: 'numberInput', brandId: [] }
]
function makeSubformGroup(rules = {}) {
return {
key: 'G1',
type: 'subform',
label: '子表',
rules,
children: [rowTemplate],
origin_children: rowTemplate
}
}
describe('subform max rows', () => {
it('IT-SUBFORM-MAX-001: addGroupRow stops at 20 with added false', () => {
const schema = { list: [makeSubformGroup()] }
const engine = createFormEngine()
engine.init(schema, {})
for (let i = 0; i < SUBFORM_MAX_ROWS; i++) {
const viewState = engine.addGroupRow(schema.list[0])
assert.equal(viewState.added, true, `row ${i + 1} should add`)
}
assert.equal(engine.getGroupRows('G1').length, SUBFORM_MAX_ROWS)
const blocked = engine.addGroupRow(schema.list[0])
assert.equal(blocked.added, false)
assert.equal(engine.getGroupRows('G1').length, SUBFORM_MAX_ROWS)
})
it('IT-SUBFORM-MAX-002: resolveSubformMaxRows respects maxLength and caps maxRows', () => {
assert.equal(resolveSubformMaxRows(makeSubformGroup({ maxLength: 15 })), 15)
assert.equal(resolveSubformMaxRows(makeSubformGroup({ maxRows: 25 })), SUBFORM_MAX_ROWS)
assert.equal(resolveSubformMaxRows(makeSubformGroup()), SUBFORM_MAX_ROWS)
})
it('IT-SUBFORM-MAX-003: validateForm rejects more than max rows', () => {
const group = makeSubformGroup()
const list = [group]
const ctx = { brandIds: [], sceneList: [] }
const rows = Array.from({ length: SUBFORM_MAX_ROWS + 1 }, () => ({ c1: 1 }))
const result = validateForm(list, ctx, { G1: rows })
assert.ok(result.errorMap.G1)
assert.match(result.errorMap.G1, /最多20条/)
})
it('IT-SUBFORM-MAX-004: normalizeSchemaRoot injects rules.maxRows', () => {
const normalized = normalizeSchemaRoot({ list: [makeSubformGroup()] })
const group = normalized.list[0]
assert.equal(group.rules.maxRows, SUBFORM_MAX_ROWS)
})
})