AccountLayout.utils.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { describe, expect, it } from 'vitest'
  2. import { getActiveKey, toSubMenuSections } from './AccountLayout.utils'
  3. describe('toSubMenuSections', () => {
  4. it('converts sections to SubMenuSection format', () => {
  5. const sections = [
  6. {
  7. key: 'account-settings',
  8. heading: 'Account Settings',
  9. links: [
  10. { key: 'preferences', label: 'Preferences', href: '/account/me', isActive: true },
  11. {
  12. key: 'access-tokens',
  13. label: 'Access Tokens',
  14. href: '/account/tokens',
  15. isActive: false,
  16. },
  17. ],
  18. },
  19. ]
  20. const result = toSubMenuSections(sections)
  21. expect(result).toHaveLength(1)
  22. expect(result[0]).toEqual({
  23. key: 'account-settings',
  24. heading: 'Account Settings',
  25. links: [
  26. { key: 'preferences', label: 'Preferences', href: '/account/me' },
  27. { key: 'access-tokens', label: 'Access Tokens', href: '/account/tokens' },
  28. ],
  29. })
  30. })
  31. it('returns empty array for non-array input', () => {
  32. expect(toSubMenuSections(null as any)).toEqual([])
  33. expect(toSubMenuSections(undefined as any)).toEqual([])
  34. })
  35. it('filters out invalid links', () => {
  36. const sections = [
  37. {
  38. key: 's1',
  39. links: [
  40. { key: 'a', label: 'A', href: '/a' },
  41. null,
  42. { key: '', label: 'B', href: '/b' },
  43. { key: 'c', label: null, href: '/c' } as any,
  44. ],
  45. },
  46. ]
  47. const result = toSubMenuSections(sections)
  48. expect(result[0].links).toHaveLength(1)
  49. expect(result[0].links[0]).toEqual({ key: 'a', label: 'A', href: '/a' })
  50. })
  51. it('handles missing optional fields', () => {
  52. const sections = [{ key: 's1', links: [{ key: 'a', label: 'A' }] }]
  53. const result = toSubMenuSections(sections)
  54. expect(result[0].links[0].href).toBeUndefined()
  55. expect(result[0].heading).toBeUndefined()
  56. })
  57. })
  58. describe('getActiveKey', () => {
  59. it('returns key of first active link', () => {
  60. const sections = [
  61. {
  62. key: 's1',
  63. links: [
  64. { key: 'a', label: 'A', isActive: false },
  65. { key: 'b', label: 'B', isActive: true },
  66. ],
  67. },
  68. ]
  69. expect(getActiveKey(sections)).toBe('b')
  70. })
  71. it('returns first active across sections', () => {
  72. const sections = [
  73. { key: 's1', links: [{ key: 'a', label: 'A', isActive: false }] },
  74. { key: 's2', links: [{ key: 'b', label: 'B', isActive: true }] },
  75. ]
  76. expect(getActiveKey(sections)).toBe('b')
  77. })
  78. it('returns undefined when no active link', () => {
  79. const sections = [{ key: 's1', links: [{ key: 'a', label: 'A', isActive: false }] }]
  80. expect(getActiveKey(sections)).toBeUndefined()
  81. })
  82. it('returns undefined for invalid input', () => {
  83. expect(getActiveKey(null as any)).toBeUndefined()
  84. expect(getActiveKey(undefined as any)).toBeUndefined()
  85. expect(getActiveKey([])).toBeUndefined()
  86. })
  87. it('handles missing links array', () => {
  88. const sections = [{ key: 's1', links: undefined }]
  89. expect(getActiveKey(sections as any)).toBeUndefined()
  90. })
  91. })