OrgMenuContent.utils.test.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. getOrgActiveRoute,
  4. getOrgSectionKeyFromPathname,
  5. isOrgMenuActive,
  6. type OrgNavItem,
  7. } from './OrgMenuContent.utils'
  8. const mockItem = (key: string): OrgNavItem => ({
  9. key,
  10. label: key,
  11. href: `/org/foo/${key}`,
  12. icon: null,
  13. })
  14. describe('getOrgActiveRoute', () => {
  15. it('returns segment after org slug', () => {
  16. expect(getOrgActiveRoute('/org/my-org/team')).toBe('team')
  17. expect(getOrgActiveRoute('/org/my-org/integrations')).toBe('integrations')
  18. expect(getOrgActiveRoute('/org/my-org/settings/general')).toBe('settings')
  19. })
  20. it('returns undefined for org home', () => {
  21. expect(getOrgActiveRoute('/org/my-org')).toBeUndefined()
  22. expect(getOrgActiveRoute('/org/my-org/')).toBeUndefined()
  23. })
  24. it('returns undefined when path too short', () => {
  25. expect(getOrgActiveRoute('/org')).toBeUndefined()
  26. expect(getOrgActiveRoute('/')).toBeUndefined()
  27. expect(getOrgActiveRoute('')).toBeUndefined()
  28. })
  29. it('returns undefined when org segment not found', () => {
  30. expect(getOrgActiveRoute('/project/ref/database')).toBeUndefined()
  31. })
  32. })
  33. describe('isOrgMenuActive', () => {
  34. it('first item (index 0) is active when activeRoute is undefined', () => {
  35. expect(isOrgMenuActive(mockItem('projects'), 0, '/org/foo', undefined)).toBe(true)
  36. })
  37. it('first item is not active when activeRoute is defined', () => {
  38. expect(isOrgMenuActive(mockItem('projects'), 0, '/org/foo/team', 'team')).toBe(false)
  39. })
  40. it('item is active when activeRoute matches item key', () => {
  41. expect(isOrgMenuActive(mockItem('team'), 1, '/org/foo/team', 'team')).toBe(true)
  42. expect(
  43. isOrgMenuActive(mockItem('integrations'), 2, '/org/foo/integrations', 'integrations')
  44. ).toBe(true)
  45. })
  46. it('settings item is active when pathname includes settings sub-routes', () => {
  47. expect(isOrgMenuActive(mockItem('settings'), 5, '/org/foo/settings/general', 'settings')).toBe(
  48. true
  49. )
  50. expect(isOrgMenuActive(mockItem('settings'), 5, '/org/foo/general', undefined)).toBe(true)
  51. expect(isOrgMenuActive(mockItem('settings'), 5, '/org/foo/apps', undefined)).toBe(true)
  52. expect(isOrgMenuActive(mockItem('settings'), 5, '/org/foo/audit', undefined)).toBe(true)
  53. expect(isOrgMenuActive(mockItem('settings'), 5, '/org/foo/documents', undefined)).toBe(true)
  54. expect(isOrgMenuActive(mockItem('settings'), 5, '/org/foo/security', undefined)).toBe(true)
  55. expect(isOrgMenuActive(mockItem('settings'), 5, '/org/foo/sso', undefined)).toBe(true)
  56. })
  57. it('settings item is not active when pathname has no settings sub-route', () => {
  58. expect(isOrgMenuActive(mockItem('settings'), 5, '/org/foo/team', 'team')).toBe(false)
  59. })
  60. it('item is not active when activeRoute does not match', () => {
  61. expect(isOrgMenuActive(mockItem('team'), 1, '/org/foo/team', 'usage')).toBe(false)
  62. })
  63. })
  64. describe('getOrgSectionKeyFromPathname', () => {
  65. it('returns settings for org settings sub-routes', () => {
  66. expect(getOrgSectionKeyFromPathname('general')).toBe('settings')
  67. expect(getOrgSectionKeyFromPathname('security')).toBe('settings')
  68. expect(getOrgSectionKeyFromPathname('sso')).toBe('settings')
  69. expect(getOrgSectionKeyFromPathname('apps')).toBe('settings')
  70. expect(getOrgSectionKeyFromPathname('audit')).toBe('settings')
  71. expect(getOrgSectionKeyFromPathname('documents')).toBe('settings')
  72. })
  73. it('returns null for non-settings routes', () => {
  74. expect(getOrgSectionKeyFromPathname('team')).toBeNull()
  75. expect(getOrgSectionKeyFromPathname('integrations')).toBeNull()
  76. expect(getOrgSectionKeyFromPathname('billing')).toBeNull()
  77. expect(getOrgSectionKeyFromPathname('usage')).toBeNull()
  78. })
  79. it('returns null when activeRoute is undefined', () => {
  80. expect(getOrgSectionKeyFromPathname(undefined)).toBeNull()
  81. })
  82. })