homeScope.test.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { normalizeRootTypeId, collectHomeScopes, resolveHomeScope } from '../src/utils/homeScope.js'
  4. const homeMenus = [
  5. {
  6. path: '/subSystem/waterSupply',
  7. children: [
  8. { path: 'home', name: 'WaterHome', query: '{"rootTypeId":1}' }
  9. ]
  10. },
  11. {
  12. path: '/subSystem/drainage',
  13. children: [
  14. { path: 'home', name: 'DrainageHome', query: '{"rootTypeId":2}' }
  15. ]
  16. }
  17. ]
  18. test('只接受四个设备大类,绝不回退到默认值', () => {
  19. assert.equal(normalizeRootTypeId('1'), 1)
  20. assert.equal(normalizeRootTypeId(4), 4)
  21. assert.equal(normalizeRootTypeId(undefined), undefined)
  22. assert.equal(normalizeRootTypeId(''), undefined)
  23. assert.equal(normalizeRootTypeId(0), undefined)
  24. assert.equal(normalizeRootTypeId(5), undefined)
  25. assert.equal(normalizeRootTypeId('abc'), undefined)
  26. })
  27. test('URL 带 rootTypeId 时按 URL 取范围,不会变成窨井', () => {
  28. const scope = resolveHomeScope({
  29. query: { rootTypeId: '2' },
  30. path: '/subSystem/drainage/home',
  31. menus: homeMenus
  32. })
  33. assert.equal(scope.rootTypeId, 2)
  34. assert.equal(scope.source, 'url')
  35. })
  36. test('URL 丢失参数时从 /getRouters 的菜单 query 恢复', () => {
  37. const scope = resolveHomeScope({
  38. query: {},
  39. path: '/subSystem/waterSupply/home',
  40. menus: homeMenus
  41. })
  42. assert.equal(scope.rootTypeId, 1)
  43. assert.equal(scope.source, 'menu')
  44. })
  45. test('菜单 query 是 JSON 字符串也能解析', () => {
  46. const scopes = collectHomeScopes(homeMenus)
  47. assert.deepEqual(scopes.map(item => item.rootTypeId), [1, 2])
  48. assert.equal(scopes[0].path, '/subSystem/waterSupply/home')
  49. })
  50. test('未配置 query 时不返回任何范围,页面应报错而不是查窨井数据', () => {
  51. const scope = resolveHomeScope({ query: {}, path: '/subSystem/gas/home', menus: [] })
  52. assert.equal(scope.rootTypeId, undefined)
  53. assert.equal(scope.source, undefined)
  54. })
  55. test('多个首页菜单共用同一路径时由 URL 参数区分,不猜测默认值', () => {
  56. const menus = [
  57. { path: '/home', query: '{"rootTypeId":3}' },
  58. { path: '/home', query: '{"rootTypeId":4}' }
  59. ]
  60. assert.equal(resolveHomeScope({ query: { rootTypeId: 3 }, path: '/home', menus }).rootTypeId, 3)
  61. assert.equal(resolveHomeScope({ query: {}, path: '/home', menus }).rootTypeId, undefined)
  62. })
  63. test('账号只有一个已授权首页菜单时可据此兜底', () => {
  64. const scope = resolveHomeScope({ query: {}, path: '/other', menus: [homeMenus[0]] })
  65. assert.equal(scope.rootTypeId, 1)
  66. assert.equal(scope.source, 'only-menu')
  67. })
  68. test('兼容 ?query={"rootTypeId":N} 形式的透传链接', () => {
  69. const scope = resolveHomeScope({
  70. query: { query: '{"rootTypeId":3}' },
  71. path: '/subSystem/gas/home',
  72. menus: []
  73. })
  74. assert.equal(scope.rootTypeId, 3)
  75. })