fieldOptionsService.test.mjs
3.18 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
/**
* fieldOptionsService 进程内缓存 — useCache 开关
*/
import { describe, it, afterEach } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const { NEGOTIATION_DAILY_API_SWITCH } = require('../config/apiSwitch.js')
const { fetchFieldOptions, clearFieldOptionsCache } = require('../api/fieldOptionsService.js')
const { __setOrderListApi, __setEmployeeListApi } = require('../api/fieldOptionProviders.js')
const ORDER_LIST_PARAMS = {
optionsKey: 'orderList',
reportType: 'negotiation',
merchantId: 'shop-neg',
staffUuid: 'staff-uuid-1',
reportDate: '2026-06-02',
saleDate: '2026-06-15',
carBrandTypeFlag: '2',
groupBrandIds: [3067],
sceneList: [{ baseSceneCode: 110201 }]
}
const STORE_STAFF_LIST_PARAMS = {
optionsKey: 'storeStaffList',
reportType: 'store',
yqMerchantId: 'yq-shop-1',
merchantId: 'shop-1',
staffUuid: 'staff-uuid-1',
reportDate: '2026-06-02'
}
describe('fieldOptionsService', () => {
afterEach(() => {
clearFieldOptionsCache()
__setOrderListApi(null)
__setEmployeeListApi(null)
})
it('IT-NEG-012: useCache false always hits adapter (orderList popup refresh)', async () => {
NEGOTIATION_DAILY_API_SWITCH.fieldOptions = 'real'
let callCount = 0
__setOrderListApi({
getGoodsOrderOutputValueList: async () => {
callCount += 1
return {
code: 0,
message: 'ok',
data: [{ orderCode: `order-${callCount}`, productName: '延保订单' }]
}
}
})
const first = await fetchFieldOptions(ORDER_LIST_PARAMS, { useCache: false })
const second = await fetchFieldOptions(ORDER_LIST_PARAMS, { useCache: false })
assert.equal(callCount, 2)
assert.equal(first.data[0].value, 'order-1')
assert.equal(second.data[0].value, 'order-2')
})
it('IT-NEG-012b: useCache true reuses cached orderList result', async () => {
NEGOTIATION_DAILY_API_SWITCH.fieldOptions = 'real'
let callCount = 0
__setOrderListApi({
getGoodsOrderOutputValueList: async () => {
callCount += 1
return {
code: 0,
message: 'ok',
data: [{ orderCode: 'order-cached', productName: '延保订单' }]
}
}
})
const first = await fetchFieldOptions(ORDER_LIST_PARAMS, { useCache: true })
const second = await fetchFieldOptions(ORDER_LIST_PARAMS, { useCache: true })
assert.equal(callCount, 1)
assert.equal(first.data[0].value, 'order-cached')
assert.equal(second.data[0].value, 'order-cached')
})
it('IT-FIELD-OPT-009: useCache false always hits adapter (storeStaffList popup refresh)', async () => {
const { STORE_DAILY_API_SWITCH } = require('../config/apiSwitch.js')
STORE_DAILY_API_SWITCH.fieldOptions = 'real'
let callCount = 0
__setEmployeeListApi({
getEmployeeList: async () => {
callCount += 1
return {
code: 0,
message: 'ok',
data: [{ staffName: `员工${callCount}`, uuid: `u-${callCount}` }]
}
}
})
const first = await fetchFieldOptions(STORE_STAFF_LIST_PARAMS, { useCache: false })
const second = await fetchFieldOptions(STORE_STAFF_LIST_PARAMS, { useCache: false })
assert.equal(callCount, 2)
assert.equal(first.data[0].value, 'u-1')
assert.equal(second.data[0].value, 'u-2')
})
})