|
| 1 | +import { mockNuxtImport, mountSuspended } from '@nuxt/test-utils/runtime' |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import type { VueWrapper } from '@vue/test-utils' |
| 4 | +import PackageHeader from '~/components/Package/Header.vue' |
| 5 | + |
| 6 | +const { mockUsePackageRoute } = vi.hoisted(() => ({ |
| 7 | + mockUsePackageRoute: vi.fn(), |
| 8 | +})) |
| 9 | + |
| 10 | +mockNuxtImport('usePackageRoute', () => mockUsePackageRoute) |
| 11 | + |
| 12 | +function setRoute({ |
| 13 | + requestedVersion = null as string | null, |
| 14 | + orgName = null as string | null, |
| 15 | +} = {}) { |
| 16 | + mockUsePackageRoute.mockReturnValue({ |
| 17 | + packageName: computed(() => 'vue'), |
| 18 | + requestedVersion: computed(() => requestedVersion), |
| 19 | + orgName: computed(() => orgName), |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +const baseProps = { |
| 24 | + pkg: { |
| 25 | + 'name': 'vue', |
| 26 | + 'dist-tags': {}, |
| 27 | + 'versions': {}, |
| 28 | + }, |
| 29 | + resolvedVersion: '3.5.0', |
| 30 | + displayVersion: { |
| 31 | + _id: '1234567890', |
| 32 | + _npmVersion: '3.5.0', |
| 33 | + name: 'vue', |
| 34 | + version: '3.5.0', |
| 35 | + dist: { |
| 36 | + shasum: '1234567890', |
| 37 | + signatures: [], |
| 38 | + tarball: 'https://npmx.dev/package/vue/tarball', |
| 39 | + }, |
| 40 | + }, |
| 41 | + latestVersion: { version: '3.5.0', tags: [] }, |
| 42 | + provenanceData: null, |
| 43 | + provenanceStatus: 'idle', |
| 44 | + page: 'docs' as const, |
| 45 | + versionUrlPattern: '/package/vue/v/{version}', |
| 46 | +} |
| 47 | + |
| 48 | +function mountHeader() { |
| 49 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 50 | + return mountSuspended(PackageHeader, { props: baseProps as any }) |
| 51 | +} |
| 52 | + |
| 53 | +describe('PackageHeader version display', () => { |
| 54 | + let wrapper: VueWrapper |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + mockUsePackageRoute.mockReset() |
| 58 | + }) |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + wrapper?.unmount() |
| 62 | + }) |
| 63 | + |
| 64 | + it('hides the resolved version in the title when the URL has no explicit version', async () => { |
| 65 | + setRoute({ requestedVersion: null }) |
| 66 | + |
| 67 | + wrapper = await mountHeader() |
| 68 | + |
| 69 | + // The <h1> title should show only the package name, not "@3.5.0" |
| 70 | + expect(wrapper.get('h1').text()).not.toContain('3.5.0') |
| 71 | + // The version copy button should not be rendered |
| 72 | + expect(wrapper.text()).not.toContain('Copy package version') |
| 73 | + }) |
| 74 | + |
| 75 | + it('shows the resolved version in the title when the URL has an explicit version', async () => { |
| 76 | + setRoute({ requestedVersion: '3.5.0' }) |
| 77 | + |
| 78 | + wrapper = await mountHeader() |
| 79 | + |
| 80 | + expect(wrapper.get('h1').text()).toContain('3.5.0') |
| 81 | + expect(wrapper.text()).toContain('Copy package version') |
| 82 | + }) |
| 83 | + |
| 84 | + it('renders separate copy buttons for the package name and the version', async () => { |
| 85 | + setRoute({ requestedVersion: '3.5.0' }) |
| 86 | + |
| 87 | + wrapper = await mountHeader() |
| 88 | + |
| 89 | + const copyButtonLabels = wrapper |
| 90 | + .findAll('button') |
| 91 | + .map(b => b.text()) |
| 92 | + .filter(text => text.includes('Copy package')) |
| 93 | + |
| 94 | + expect(copyButtonLabels.some(text => text.includes('Copy package name'))).toBe(true) |
| 95 | + expect(copyButtonLabels.some(text => text.includes('Copy package version'))).toBe(true) |
| 96 | + }) |
| 97 | + |
| 98 | + it('shows the resolved version for a dist-tag request (e.g. /v/latest)', async () => { |
| 99 | + // requestedVersion is the raw URL segment ("latest"); resolvedVersion is the concrete number |
| 100 | + setRoute({ requestedVersion: 'latest' }) |
| 101 | + |
| 102 | + wrapper = await mountHeader() |
| 103 | + |
| 104 | + expect(wrapper.get('h1').text()).toContain('3.5.0') |
| 105 | + }) |
| 106 | +}) |
0 commit comments