|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { isNavActive, navLinks, type NavRouteLike } from './nav' |
| 3 | + |
| 4 | +const COUNTS = { repositories: 12, issues: 34, pullRequests: 5 } |
| 5 | + |
| 6 | +describe('navLinks', () => { |
| 7 | + it('includes only routes that ask to be in the nav', () => { |
| 8 | + const routes: NavRouteLike[] = [ |
| 9 | + { path: '/', meta: { title: 'Repositories', nav: { order: 1 } } }, |
| 10 | + { path: '/release/board', meta: { title: 'Release' } }, |
| 11 | + ] |
| 12 | + expect(navLinks(routes).map((l) => l.to)).toEqual(['/']) |
| 13 | + }) |
| 14 | + |
| 15 | + // getRoutes() is sorted by matcher score, not by declaration order, so `order` is load-bearing |
| 16 | + it('sorts by the declared order rather than the input order', () => { |
| 17 | + const routes: NavRouteLike[] = [ |
| 18 | + { path: '/b', meta: { title: 'B', nav: { order: 2 } } }, |
| 19 | + { path: '/a', meta: { title: 'A', nav: { order: 1 } } }, |
| 20 | + ] |
| 21 | + expect(navLinks(routes).map((l) => l.label)).toEqual(['A', 'B']) |
| 22 | + }) |
| 23 | + |
| 24 | + it('falls back to the route title, so the two cannot drift', () => { |
| 25 | + const routes: NavRouteLike[] = [ |
| 26 | + { path: '/', meta: { title: 'Repositories', nav: { order: 1, label: 'Repos' } } }, |
| 27 | + { path: '/branches', meta: { title: 'Branches', nav: { order: 2 } } }, |
| 28 | + ] |
| 29 | + expect(navLinks(routes).map((l) => l.label)).toEqual(['Repos', 'Branches']) |
| 30 | + }) |
| 31 | + |
| 32 | + it('resolves a badge against the session counts', () => { |
| 33 | + const routes: NavRouteLike[] = [ |
| 34 | + { path: '/issues', meta: { title: 'Issues', nav: { order: 1, badge: 'issues' } } }, |
| 35 | + { path: '/branches', meta: { title: 'Branches', nav: { order: 2 } } }, |
| 36 | + ] |
| 37 | + const links = navLinks(routes, COUNTS) |
| 38 | + expect(links[0]!.badge).toBe(34) |
| 39 | + expect(links[1]!.badge).toBeUndefined() |
| 40 | + }) |
| 41 | + |
| 42 | + it('omits the badge entirely until the session has loaded', () => { |
| 43 | + const routes: NavRouteLike[] = [ |
| 44 | + { path: '/issues', meta: { title: 'Issues', nav: { order: 1, badge: 'issues' } } }, |
| 45 | + ] |
| 46 | + expect(navLinks(routes)[0]!.badge).toBeUndefined() |
| 47 | + }) |
| 48 | + |
| 49 | + it('shows a zero count rather than hiding the badge', () => { |
| 50 | + const routes: NavRouteLike[] = [ |
| 51 | + { path: '/pull-requests', meta: { title: 'PRs', nav: { order: 1, badge: 'pullRequests' } } }, |
| 52 | + ] |
| 53 | + expect(navLinks(routes, { ...COUNTS, pullRequests: 0 })[0]!.badge).toBe(0) |
| 54 | + }) |
| 55 | +}) |
| 56 | + |
| 57 | +describe('isNavActive', () => { |
| 58 | + it('matches the route itself and anything below it', () => { |
| 59 | + expect(isNavActive('/release', '/release')).toBe(true) |
| 60 | + expect(isNavActive('/release', '/release/board')).toBe(true) |
| 61 | + }) |
| 62 | + |
| 63 | + // the whole reason this is segment-aware rather than a prefix test |
| 64 | + it('does not let /release-notes activate /release', () => { |
| 65 | + expect(isNavActive('/release', '/release-notes')).toBe(false) |
| 66 | + }) |
| 67 | + |
| 68 | + it('treats the root as exact, so it does not activate on every page', () => { |
| 69 | + expect(isNavActive('/', '/')).toBe(true) |
| 70 | + expect(isNavActive('/', '/branches')).toBe(false) |
| 71 | + expect(isNavActive('/', '/issues')).toBe(false) |
| 72 | + }) |
| 73 | +}) |
0 commit comments