|
| 1 | +/** |
| 2 | + * Copyright (c) HashiCorp, Inc. |
| 3 | + * SPDX-License-Identifier: MPL-2.0 |
| 4 | + */ |
| 5 | +import { describe, it, expect } from 'vitest' |
| 6 | +import { getFilteredNavItems } from '../get-filtered-nav-items' |
| 7 | +import { |
| 8 | + FilteredNavItem, |
| 9 | + NavItemWithMetaData, |
| 10 | + SubmenuNavItemWithMetaData, |
| 11 | + LinkNavItemWithMetaData, |
| 12 | +} from '../../types' |
| 13 | + |
| 14 | +type NavItemWithRoutes = Extract<NavItemWithMetaData | FilteredNavItem, { routes: unknown }> |
| 15 | + |
| 16 | +const isNavItemWithRoutes = ( |
| 17 | + item: NavItemWithMetaData | FilteredNavItem |
| 18 | +): item is NavItemWithRoutes => 'routes' in item |
| 19 | + |
| 20 | +const expectNavItemWithRoutes = ( |
| 21 | + item: NavItemWithMetaData | FilteredNavItem | undefined |
| 22 | +): NavItemWithRoutes => { |
| 23 | + if (!item) { |
| 24 | + throw new Error('Expected nav item with routes but received undefined') |
| 25 | + } |
| 26 | + |
| 27 | + if (isNavItemWithRoutes(item)) { |
| 28 | + return item |
| 29 | + } |
| 30 | + |
| 31 | + throw new Error('Expected nav item with routes') |
| 32 | +} |
| 33 | + |
| 34 | +describe('getFilteredNavItems', () => { |
| 35 | + it('returns all items when filter value is empty', () => { |
| 36 | + const items: NavItemWithMetaData[] = [ |
| 37 | + { title: 'Item 1', path: '/item-1' }, |
| 38 | + { title: 'Item 2', path: '/item-2' }, |
| 39 | + ] as LinkNavItemWithMetaData[] |
| 40 | + |
| 41 | + const result = getFilteredNavItems(items, '') |
| 42 | + |
| 43 | + expect(result).toEqual(items) |
| 44 | + }) |
| 45 | + |
| 46 | + it('filters items by title (case insensitive)', () => { |
| 47 | + const items: NavItemWithMetaData[] = [ |
| 48 | + { title: 'Getting Started', path: '/getting-started' }, |
| 49 | + { title: 'Advanced Topics', path: '/advanced' }, |
| 50 | + ] as LinkNavItemWithMetaData[] |
| 51 | + |
| 52 | + const result = getFilteredNavItems(items, 'getting') |
| 53 | + |
| 54 | + expect(result).toHaveLength(1) |
| 55 | + expect(result[0]).toMatchObject({ title: 'Getting Started', matchesFilter: true }) |
| 56 | + }) |
| 57 | + |
| 58 | + it('filters items by alias', () => { |
| 59 | + const items: NavItemWithMetaData[] = [ |
| 60 | + { title: 'Documentation', path: '/docs', alias: 'guide' }, |
| 61 | + { title: 'API Reference', path: '/api' }, |
| 62 | + ] as LinkNavItemWithMetaData[] |
| 63 | + |
| 64 | + const result = getFilteredNavItems(items, 'guide') |
| 65 | + |
| 66 | + expect(result).toHaveLength(1) |
| 67 | + expect(result[0]).toMatchObject({ title: 'Documentation', matchesFilter: true }) |
| 68 | + }) |
| 69 | + |
| 70 | + it('does not duplicate items when both title and alias match filter', () => { |
| 71 | + const items: NavItemWithMetaData[] = [ |
| 72 | + { |
| 73 | + title: 'Enable logs', |
| 74 | + path: 'deploy/manage/monitor/logs', |
| 75 | + alias: 'service logs, system logs, audit logging', |
| 76 | + }, |
| 77 | + ] as LinkNavItemWithMetaData[] |
| 78 | + |
| 79 | + const result = getFilteredNavItems(items, 'logs') |
| 80 | + |
| 81 | + expect(result).toHaveLength(1) |
| 82 | + expect(result[0]).toMatchObject({ title: 'Enable logs', matchesFilter: true }) |
| 83 | + }) |
| 84 | + |
| 85 | + it('skips items without title', () => { |
| 86 | + const items: NavItemWithMetaData[] = [ |
| 87 | + { heading: 'Section' }, |
| 88 | + { title: 'Valid Item', path: '/valid' }, |
| 89 | + ] as NavItemWithMetaData[] |
| 90 | + |
| 91 | + const result = getFilteredNavItems(items, 'section') |
| 92 | + |
| 93 | + expect(result).toHaveLength(0) |
| 94 | + }) |
| 95 | + |
| 96 | + it('recursively filters submenu items', () => { |
| 97 | + const items: NavItemWithMetaData[] = [ |
| 98 | + { |
| 99 | + title: 'Parent', |
| 100 | + routes: [ |
| 101 | + { title: 'Child Match', path: '/child-match' }, |
| 102 | + { title: 'Other Child', path: '/other' }, |
| 103 | + ], |
| 104 | + }, |
| 105 | + ] as SubmenuNavItemWithMetaData[] |
| 106 | + |
| 107 | + const result = getFilteredNavItems(items, 'child match') |
| 108 | + |
| 109 | + expect(result).toHaveLength(1) |
| 110 | + const parent = expectNavItemWithRoutes(result[0]) |
| 111 | + expect(parent).toMatchObject({ |
| 112 | + title: 'Parent', |
| 113 | + hasChildrenMatchingFilter: true, |
| 114 | + }) |
| 115 | + expect(parent.routes).toHaveLength(1) |
| 116 | + const child = parent.routes[0]! |
| 117 | + expect(child).toMatchObject({ |
| 118 | + title: 'Child Match', |
| 119 | + matchesFilter: true, |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + it('includes parent and all children when parent matches filter', () => { |
| 124 | + const items: NavItemWithMetaData[] = [ |
| 125 | + { |
| 126 | + title: 'Parent Match', |
| 127 | + routes: [ |
| 128 | + { title: 'Child 1', path: '/child-1' }, |
| 129 | + { title: 'Child 2', path: '/child-2' }, |
| 130 | + ], |
| 131 | + }, |
| 132 | + ] as SubmenuNavItemWithMetaData[] |
| 133 | + |
| 134 | + const result = getFilteredNavItems(items, 'parent') |
| 135 | + |
| 136 | + expect(result).toHaveLength(1) |
| 137 | + const parent = expectNavItemWithRoutes(result[0]) |
| 138 | + expect(parent).toMatchObject({ |
| 139 | + title: 'Parent Match', |
| 140 | + matchesFilter: true, |
| 141 | + }) |
| 142 | + expect(parent.routes).toHaveLength(2) |
| 143 | + }) |
| 144 | + |
| 145 | + it('excludes parent when no children match filter', () => { |
| 146 | + const items: NavItemWithMetaData[] = [ |
| 147 | + { |
| 148 | + title: 'Parent', |
| 149 | + routes: [ |
| 150 | + { title: 'Child 1', path: '/child-1' }, |
| 151 | + { title: 'Child 2', path: '/child-2' }, |
| 152 | + ], |
| 153 | + }, |
| 154 | + ] as SubmenuNavItemWithMetaData[] |
| 155 | + |
| 156 | + const result = getFilteredNavItems(items, 'nomatch') |
| 157 | + |
| 158 | + expect(result).toHaveLength(0) |
| 159 | + }) |
| 160 | + |
| 161 | + it('handles deeply nested submenu items', () => { |
| 162 | + const items: NavItemWithMetaData[] = [ |
| 163 | + { |
| 164 | + title: 'Level 1', |
| 165 | + routes: [ |
| 166 | + { |
| 167 | + title: 'Level 2', |
| 168 | + routes: [ |
| 169 | + { title: 'Deep Match', path: '/deep' }, |
| 170 | + ], |
| 171 | + }, |
| 172 | + ], |
| 173 | + }, |
| 174 | + ] as SubmenuNavItemWithMetaData[] |
| 175 | + |
| 176 | + const result = getFilteredNavItems(items, 'deep') |
| 177 | + |
| 178 | + expect(result).toHaveLength(1) |
| 179 | + const levelOne = expectNavItemWithRoutes(result[0]) |
| 180 | + const levelTwo = expectNavItemWithRoutes(levelOne.routes[0]) |
| 181 | + const deepMatch = levelTwo.routes[0]! |
| 182 | + expect(deepMatch).toMatchObject({ |
| 183 | + title: 'Deep Match', |
| 184 | + matchesFilter: true, |
| 185 | + }) |
| 186 | + }) |
| 187 | +}) |
0 commit comments