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