|
| 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