|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | + |
| 4 | +import { getActiveNavLink } from '..'; |
| 5 | + |
| 6 | +const navItems = [ |
| 7 | + { link: '/about' }, |
| 8 | + { link: '/about/sponsors' }, |
| 9 | + { link: '/docs' }, |
| 10 | + { link: 'https://nodejs.org/learn' }, |
| 11 | +]; |
| 12 | + |
| 13 | +describe('getActiveNavLink', () => { |
| 14 | + it('picks the most specific sibling on its own pages', () => { |
| 15 | + assert.equal( |
| 16 | + getActiveNavLink(navItems, '/about/sponsors'), |
| 17 | + '/about/sponsors' |
| 18 | + ); |
| 19 | + }); |
| 20 | + |
| 21 | + it('keeps the parent active on its other sub-pages', () => { |
| 22 | + assert.equal( |
| 23 | + getActiveNavLink(navItems, '/about/governance/charter'), |
| 24 | + '/about' |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + it('matches the parent on its own index page', () => { |
| 29 | + assert.equal(getActiveNavLink(navItems, '/about'), '/about'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('does not match across word boundaries', () => { |
| 33 | + assert.equal(getActiveNavLink(navItems, '/about-us'), ''); |
| 34 | + }); |
| 35 | + |
| 36 | + it('never returns an external (non-internal) link', () => { |
| 37 | + assert.equal(getActiveNavLink(navItems, 'https://nodejs.org/learn'), ''); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns empty string when nothing matches', () => { |
| 41 | + assert.equal(getActiveNavLink(navItems, '/unknown'), ''); |
| 42 | + }); |
| 43 | +}); |
0 commit comments