dictLabels.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import assert from 'node:assert/strict'
  2. import { test } from 'node:test'
  3. import {
  4. DICT_TYPES,
  5. dictLabel,
  6. hasDictLabels,
  7. resetDictLabelsForTest,
  8. setDictLabels,
  9. toDictLabels,
  10. } from '../src/modules/cockpit/domain/dictLabels.js'
  11. test('字典行归一化成 value→label,缺值行不收', () => {
  12. const map = toDictLabels([
  13. { dictValue: '0', dictLabel: '重大风险' },
  14. { dictValue: '1', dictLabel: '较大风险' },
  15. { dictValue: '', dictLabel: '空值丢掉' },
  16. { dict_value: '2', dict_label: '一般风险' },
  17. ])
  18. assert.deepEqual(map, { 0: '重大风险', 1: '较大风险', 2: '一般风险' })
  19. assert.equal(toDictLabels([]), null)
  20. assert.equal(toDictLabels(null), null)
  21. })
  22. test('注入后按类型翻译;没有这类字典或没有这个码都回空串', () => {
  23. resetDictLabelsForTest()
  24. assert.equal(dictLabel(DICT_TYPES.riskLevel, '1'), '')
  25. setDictLabels(DICT_TYPES.riskLevel, [{ dictValue: '1', dictLabel: '较大风险' }])
  26. assert.equal(dictLabel(DICT_TYPES.riskLevel, '1'), '较大风险')
  27. assert.equal(dictLabel(DICT_TYPES.riskLevel, 1), '较大风险')
  28. assert.equal(dictLabel(DICT_TYPES.riskLevel, '9'), '')
  29. assert.equal(dictLabel(DICT_TYPES.riskColor, '1'), '')
  30. assert.equal(hasDictLabels(DICT_TYPES.riskLevel), true)
  31. resetDictLabelsForTest()
  32. assert.equal(hasDictLabels(DICT_TYPES.riskLevel), false)
  33. })
  34. test('空表不覆盖已有字典:接口返回空时继续用上一份', () => {
  35. resetDictLabelsForTest()
  36. setDictLabels(DICT_TYPES.riskColor, { 0: '红', 1: '橙' })
  37. assert.equal(setDictLabels(DICT_TYPES.riskColor, []), false)
  38. assert.equal(dictLabel(DICT_TYPES.riskColor, '1'), '橙')
  39. })
  40. test('未知字典类型不接收', () => {
  41. resetDictLabelsForTest()
  42. assert.equal(setDictLabels('pump_station_type', { 0: '供水泵站' }), false)
  43. assert.equal(hasDictLabels('pump_station_type'), false)
  44. })