vin-template-only-validation.test.mjs
3.84 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
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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
function validateFixedField(validateForm, field, value, options = {}) {
return validateForm(
[field],
{},
{ [field.key]: value },
{},
options
)
}
function loadPageWithoutVinValidationService(relativePath) {
const source = readFileSync(new URL(relativePath, import.meta.url), 'utf8').replace(
/import\s*{([\s\S]*?)}\s*from\s*'~\/utils\/vinVaildate\.js'/,
"const {$1} = require('~/utils/vinVaildate.js')"
)
let definition
const fallbackModule = new Proxy(
{},
{
get: () => () => ({})
}
)
const requireForPage = specifier => {
if (specifier === '~/utils/vinVaildate.js') {
throw new Error('daily report pages must not load the VIN validation service')
}
return fallbackModule
}
new Function('require', 'Page', 'wx', source)(
requireForPage,
config => {
definition = config
},
{}
)
return definition
}
function loadInputComponentWithoutVinValidationService() {
const source = readFileSync(
new URL('../components/form-field-input/form-field-input.js', import.meta.url),
'utf8'
)
let definition
const requireForComponent = specifier => {
if (specifier === '~/utils/vinVaildate.js') {
throw new Error('input component must not load the VIN validation service')
}
return {}
}
new Function('require', 'Component', 'wx', source)(
requireForComponent,
config => {
definition = config
},
{}
)
return definition
}
describe('VIN template-only validation', async () => {
const { validateForm } = await import('../form-engine/validator.js')
it('accepts an optional store VIN when it satisfies the template maxLength rule', () => {
const field = {
key: 'F2-107-9',
type: 'input',
label: '客户车架号',
required: false,
prefixComponent: 'scan',
rules: { maxLength: 20 }
}
const result = validateFixedField(validateForm, field, 'SHORT', {
vinApiValidMap: { 'F2-107-9': false }
})
assert.equal(result.ok, true, JSON.stringify(result.errorMap))
})
it('rejects a store VIN longer than the template maxLength', () => {
const field = {
key: 'F2-107-9',
type: 'input',
label: '客户车架号',
required: false,
prefixComponent: 'scan',
rules: { maxLength: 20 }
}
const result = validateFixedField(validateForm, field, 'A'.repeat(21))
assert.equal(result.ok, false)
assert.equal(result.errorMap['F2-107-9'], '客户车架号不能超过20字')
})
it('uses only the negotiation template required rule for VIN', () => {
const field = {
key: 'F3-1-7',
type: 'input',
label: '车架号',
required: true,
prefixComponent: 'scan',
rules: {}
}
const empty = validateFixedField(validateForm, field, '')
const filled = validateFixedField(validateForm, field, 'SHORT', {
vinApiValidMap: { 'F3-1-7': false }
})
assert.equal(empty.ok, false)
assert.equal(empty.errorMap['F3-1-7'], '车架号不能为空')
assert.equal(filled.ok, true, JSON.stringify(filled.errorMap))
})
it('loads both daily report pages without the VIN validation service', () => {
assert.ok(loadPageWithoutVinValidationService('../pages/storeDailyReport/storeDailyReport.js'))
assert.ok(loadPageWithoutVinValidationService('../pages/negotiationDetail/negotiationDetail.js'))
})
it('keeps scan input length driven by the same template rule as a normal input', () => {
const component = loadInputComponentWithoutVinValidationService()
const context = {
data: structuredClone(component.data),
setData(patch) {
Object.assign(this.data, patch)
}
}
component.methods.observeField.call(context, {
prefixComponent: 'scan',
rules: {}
})
assert.equal(context.data.maxLength, 140)
component.methods.observeField.call(context, {
prefixComponent: 'scan',
rules: { maxLength: 20 }
})
assert.equal(context.data.maxLength, 20)
})
})