Skip to content

Commit 53d504b

Browse files
niveshdandyanclaude
andcommitted
test: add Readme component test for hash link handling
Tests the hash link click handler to ensure: - Hash links are intercepted and scrollToAnchor is called - IDs are lowercased to match heading slugs - User-content prefixed hash links work correctly Co-Authored-By: Claude (claude-opus-4-5) <noreply@anthropic.com>
1 parent 99ebc08 commit 53d504b

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
import { mountSuspended } from '@nuxt/test-utils/runtime'
3+
import Readme from '~/components/Readme.vue'
4+
5+
// Mock scrollToAnchor
6+
vi.mock('~/utils/scrollToAnchor', () => ({
7+
scrollToAnchor: vi.fn(),
8+
}))
9+
10+
import { scrollToAnchor } from '~/utils/scrollToAnchor'
11+
12+
describe('Readme', () => {
13+
describe('rendering', () => {
14+
it('renders the provided HTML content', async () => {
15+
const component = await mountSuspended(Readme, {
16+
props: { html: '<p>Hello world</p>' },
17+
})
18+
expect(component.html()).toContain('Hello world')
19+
})
20+
})
21+
22+
describe('hash link click handling', () => {
23+
it('intercepts hash link clicks and calls scrollToAnchor with lowercase ID', async () => {
24+
const component = await mountSuspended(Readme, {
25+
props: { html: '<a href="#Installation">Installation</a>' },
26+
})
27+
28+
const link = component.find('a')
29+
await link.trigger('click')
30+
31+
expect(scrollToAnchor).toHaveBeenCalledWith('installation')
32+
})
33+
34+
it('handles user-content prefixed hash links', async () => {
35+
vi.mocked(scrollToAnchor).mockClear()
36+
const component = await mountSuspended(Readme, {
37+
props: { html: '<a href="#user-content-getting-started">Getting Started</a>' },
38+
})
39+
40+
const link = component.find('a')
41+
await link.trigger('click')
42+
43+
expect(scrollToAnchor).toHaveBeenCalledWith('user-content-getting-started')
44+
})
45+
})
46+
})

0 commit comments

Comments
 (0)