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