SubMenu.utils.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { describe, expect, it } from 'vitest'
  2. import { convertSectionsToProductMenu } from './SubMenu.utils'
  3. describe('convertSectionsToProductMenu', () => {
  4. it('converts sections with heading and links to ProductMenuGroup format', () => {
  5. const sections = [
  6. {
  7. key: 'configuration',
  8. heading: 'Configuration',
  9. links: [
  10. { key: 'general', label: 'General', href: '/org/foo/general' },
  11. { key: 'security', label: 'Security', href: '/org/foo/security' },
  12. ],
  13. },
  14. ]
  15. const result = convertSectionsToProductMenu(sections)
  16. expect(result).toEqual([
  17. {
  18. key: 'configuration',
  19. title: 'Configuration',
  20. items: [
  21. { key: 'general', name: 'General', url: '/org/foo/general' },
  22. { key: 'security', name: 'Security', url: '/org/foo/security' },
  23. ],
  24. },
  25. ])
  26. })
  27. it('uses # for undefined href', () => {
  28. const sections = [
  29. {
  30. key: 'config',
  31. links: [{ key: 'item', label: 'Item' }],
  32. },
  33. ]
  34. const result = convertSectionsToProductMenu(sections)
  35. expect(result[0].items[0].url).toBe('#')
  36. })
  37. it('handles empty sections array', () => {
  38. const result = convertSectionsToProductMenu([])
  39. expect(result).toEqual([])
  40. })
  41. it('handles section with empty links', () => {
  42. const sections = [
  43. {
  44. key: 'empty',
  45. heading: 'Empty',
  46. links: [],
  47. },
  48. ]
  49. const result = convertSectionsToProductMenu(sections)
  50. expect(result).toEqual([{ key: 'empty', title: 'Empty', items: [] }])
  51. })
  52. it('handles section without heading', () => {
  53. const sections = [
  54. {
  55. key: 'no-heading',
  56. links: [{ key: 'a', label: 'A', href: '/a' }],
  57. },
  58. ]
  59. const result = convertSectionsToProductMenu(sections)
  60. expect(result[0].title).toBeUndefined()
  61. expect(result[0].items).toHaveLength(1)
  62. })
  63. it('converts multiple sections', () => {
  64. const sections = [
  65. { key: 'a', heading: 'A', links: [{ key: 'a1', label: 'A1', href: '/a1' }] },
  66. { key: 'b', heading: 'B', links: [{ key: 'b1', label: 'B1', href: '/b1' }] },
  67. ]
  68. const result = convertSectionsToProductMenu(sections)
  69. expect(result).toHaveLength(2)
  70. expect(result[0].key).toBe('a')
  71. expect(result[1].key).toBe('b')
  72. })
  73. })