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