MobileMenuContent.utils.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { describe, expect, it } from 'vitest'
  2. import { getSectionKeyFromPathname, resolveSectionDisplay } from './MobileMenuContent.utils'
  3. describe('getSectionKeyFromPathname', () => {
  4. it('returns section key for project section paths', () => {
  5. expect(getSectionKeyFromPathname('/project/abc123/database/schemas')).toBe('database')
  6. expect(getSectionKeyFromPathname('/project/abc123/auth/users')).toBe('auth')
  7. expect(getSectionKeyFromPathname('/project/abc123/settings/general')).toBe('settings')
  8. expect(getSectionKeyFromPathname('/project/abc123/integrations')).toBe('integrations')
  9. expect(getSectionKeyFromPathname('/project/abc123/realtime/inspector')).toBe('realtime')
  10. expect(getSectionKeyFromPathname('/project/abc123/functions')).toBe('functions')
  11. expect(getSectionKeyFromPathname('/project/abc123/logs/explorer')).toBe('logs')
  12. expect(getSectionKeyFromPathname('/project/abc123/advisors/security')).toBe('advisors')
  13. })
  14. it('returns null for project home (no section segment)', () => {
  15. expect(getSectionKeyFromPathname('/project/abc123')).toBeNull()
  16. expect(getSectionKeyFromPathname('/project/abc123/')).toBeNull()
  17. })
  18. it('returns null when pathname does not contain "project"', () => {
  19. expect(getSectionKeyFromPathname('/org/my-org')).toBeNull()
  20. expect(getSectionKeyFromPathname('/account/me')).toBeNull()
  21. expect(getSectionKeyFromPathname('/')).toBeNull()
  22. expect(getSectionKeyFromPathname('')).toBeNull()
  23. })
  24. it('returns null when project segment is missing or dynamic', () => {
  25. expect(getSectionKeyFromPathname('/project')).toBeNull()
  26. expect(getSectionKeyFromPathname('/project/')).toBeNull()
  27. expect(getSectionKeyFromPathname('/project/[ref]/database/schemas')).toBeNull()
  28. })
  29. it('returns first segment after project ref as section key', () => {
  30. expect(getSectionKeyFromPathname('/project/foo/bar/baz')).toBe('bar')
  31. })
  32. })
  33. describe('resolveSectionDisplay', () => {
  34. const routes = [
  35. { key: 'database', label: 'Database' },
  36. { key: 'auth', label: 'Authentication' },
  37. { key: 'settings', label: 'Settings' },
  38. ]
  39. it('returns nulls when viewLevel is top', () => {
  40. const result = resolveSectionDisplay({
  41. viewLevel: 'top',
  42. selectedSectionKey: 'database',
  43. currentSectionKey: 'auth',
  44. currentProduct: 'Authentication',
  45. routes,
  46. })
  47. expect(result).toEqual({ sectionKey: null, sectionLabel: null })
  48. })
  49. it('uses selectedSectionKey when set', () => {
  50. const result = resolveSectionDisplay({
  51. viewLevel: 'section',
  52. selectedSectionKey: 'database',
  53. currentSectionKey: 'auth',
  54. currentProduct: 'Authentication',
  55. routes,
  56. })
  57. expect(result).toEqual({ sectionKey: 'database', sectionLabel: 'Database' })
  58. })
  59. it('falls back to currentSectionKey when selectedSectionKey is null', () => {
  60. const result = resolveSectionDisplay({
  61. viewLevel: 'section',
  62. selectedSectionKey: null,
  63. currentSectionKey: 'auth',
  64. currentProduct: 'Authentication',
  65. routes,
  66. })
  67. expect(result).toEqual({ sectionKey: 'auth', sectionLabel: 'Authentication' })
  68. })
  69. it('uses currentProduct as label when sectionKey matches currentSectionKey', () => {
  70. const result = resolveSectionDisplay({
  71. viewLevel: 'section',
  72. selectedSectionKey: 'auth',
  73. currentSectionKey: 'auth',
  74. currentProduct: 'Auth Users',
  75. routes,
  76. })
  77. expect(result).toEqual({ sectionKey: 'auth', sectionLabel: 'Auth Users' })
  78. })
  79. it('falls back to sectionKey itself when no matching route', () => {
  80. const result = resolveSectionDisplay({
  81. viewLevel: 'section',
  82. selectedSectionKey: 'unknown',
  83. currentSectionKey: null,
  84. currentProduct: '',
  85. routes,
  86. })
  87. expect(result).toEqual({ sectionKey: 'unknown', sectionLabel: 'unknown' })
  88. })
  89. it('returns nulls when in section view but both keys are null', () => {
  90. const result = resolveSectionDisplay({
  91. viewLevel: 'section',
  92. selectedSectionKey: null,
  93. currentSectionKey: null,
  94. currentProduct: '',
  95. routes,
  96. })
  97. expect(result).toEqual({ sectionKey: null, sectionLabel: null })
  98. })
  99. })