configCenterTemplate.test.mjs
2.1 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
import { describe, it, beforeEach } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const {
CONFIG_CENTER_BASE,
DYNAMIC_FORM_TEMPLATE_MODULE,
normalizeConfigToSchemaRoot,
fetchConfigCenterTemplate,
clearConfigCenterTemplateCache,
__setConfigCenterRequest
} = require('../config/configCenter.js')
describe('configCenterTemplate', () => {
beforeEach(() => {
clearConfigCenterTemplateCache()
__setConfigCenterRequest(null)
})
it('normalizeConfigToSchemaRoot converts indexed field map', () => {
const root = normalizeConfigToSchemaRoot({
0: { key: 'F1-1', type: 'radio', label: '今日出勤' },
1: { key: 'F1-2', type: 'numberInput', label: '延保退单数' }
})
assert.equal(root.version, '2.0.0')
assert.equal(root.list.length, 2)
assert.equal(root.list[0].key, 'F1-1')
})
it('normalizeConfigToSchemaRoot unwraps error wrapper data', () => {
const root = normalizeConfigToSchemaRoot({
code: '0',
data: {
0: { key: 'F2', type: 'radio', label: 'x' }
},
message: 'ok'
})
assert.equal(root.list[0].key, 'F2')
})
it('fetchConfigCenterTemplate requests staff-daily-template with env=test', async () => {
let capturedUrl = ''
__setConfigCenterRequest(options => {
capturedUrl = options.url
return Promise.resolve({
data: { 0: { key: 'F1', type: 'radio', label: 'a' } }
})
})
const res = await fetchConfigCenterTemplate(DYNAMIC_FORM_TEMPLATE_MODULE.staff)
assert.ok(capturedUrl.startsWith(CONFIG_CENTER_BASE))
assert.ok(capturedUrl.includes('moduleUrl=staff-daily-template'))
assert.ok(capturedUrl.includes('env=test'))
assert.equal(res.code, 0)
assert.ok(res.data['0'])
})
it('fetchConfigCenterTemplate uses memory cache', async () => {
let calls = 0
__setConfigCenterRequest(() => {
calls += 1
return Promise.resolve({ data: { 0: { key: 'F1', type: 'radio', label: 'a' } } })
})
await fetchConfigCenterTemplate(DYNAMIC_FORM_TEMPLATE_MODULE.store)
await fetchConfigCenterTemplate(DYNAMIC_FORM_TEMPLATE_MODULE.store)
assert.equal(calls, 1)
})
})