Skip to content

Commit 25da402

Browse files
committed
test: add component tests for OgImagePackage
1 parent 79d0e4c commit 25da402

File tree

2 files changed

+163
-3
lines changed

2 files changed

+163
-3
lines changed

app/components/OgImage/Package.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ try {
147147
</span>
148148

149149
<!-- License (if any) -->
150-
<span v-if="pkg?.license" class="flex items-center gap-2">
150+
<span v-if="pkg?.license" class="flex items-center gap-2" data-testid="license">
151151
<svg
152152
viewBox="0 0 32 32"
153153
:fill="primaryColor"
@@ -172,7 +172,7 @@ try {
172172
</span>
173173

174174
<!-- Stars (if any) -->
175-
<span v-if="formattedStars" class="flex items-center gap-2">
175+
<span v-if="formattedStars" class="flex items-center gap-2" data-testid="stars">
176176
<svg
177177
xmlns="http://www.w3.org/2000/svg"
178178
viewBox="0 0 32 32"
@@ -191,7 +191,7 @@ try {
191191
</span>
192192

193193
<!-- Likes (if any) -->
194-
<span v-if="likes.totalLikes > 0" class="flex items-center gap-2">
194+
<span v-if="likes.totalLikes > 0" class="flex items-center gap-2" data-testid="likes">
195195
<svg
196196
width="32"
197197
height="32"
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import { mockNuxtImport, mountSuspended, registerEndpoint } from '@nuxt/test-utils/runtime'
2+
import { describe, expect, it, vi, beforeEach } from 'vitest'
3+
4+
const { mockUseResolvedVersion, mockUsePackageDownloads, mockUsePackage, mockUseRepoMeta } =
5+
vi.hoisted(() => ({
6+
mockUseResolvedVersion: vi.fn(),
7+
mockUsePackageDownloads: vi.fn(),
8+
mockUsePackage: vi.fn(),
9+
mockUseRepoMeta: vi.fn(),
10+
}))
11+
12+
mockNuxtImport('useResolvedVersion', () => mockUseResolvedVersion)
13+
mockNuxtImport('usePackageDownloads', () => mockUsePackageDownloads)
14+
mockNuxtImport('usePackage', () => mockUsePackage)
15+
mockNuxtImport('useRepoMeta', () => mockUseRepoMeta)
16+
mockNuxtImport('normalizeGitUrl', () => () => 'https://github.com/test/repo')
17+
18+
import OgImagePackage from '~/components/OgImage/Package.vue'
19+
20+
describe('OgImagePackage', () => {
21+
const baseProps = {
22+
name: 'test-package',
23+
version: '1.0.0',
24+
}
25+
26+
function setupMocks(
27+
overrides: {
28+
stars?: number
29+
totalLikes?: number
30+
downloads?: number | null
31+
license?: string | null
32+
packageName?: string
33+
} = {},
34+
) {
35+
const {
36+
stars = 0,
37+
totalLikes = 0,
38+
downloads = 1000,
39+
license = 'MIT',
40+
packageName = 'test-package',
41+
} = overrides
42+
43+
mockUseResolvedVersion.mockReturnValue({
44+
data: ref('1.0.0'),
45+
status: ref('success'),
46+
error: ref(null),
47+
})
48+
49+
mockUsePackageDownloads.mockReturnValue({
50+
data: downloads != null ? ref({ downloads }) : ref(null),
51+
refresh: vi.fn().mockResolvedValue(undefined),
52+
})
53+
54+
mockUsePackage.mockReturnValue({
55+
data: ref({
56+
name: packageName,
57+
license,
58+
requestedVersion: {
59+
repository: { url: 'git+https://github.com/test/repo.git' },
60+
},
61+
}),
62+
refresh: vi.fn().mockResolvedValue(undefined),
63+
})
64+
65+
mockUseRepoMeta.mockReturnValue({
66+
stars: computed(() => stars),
67+
refresh: vi.fn().mockResolvedValue(undefined),
68+
})
69+
70+
// Mock the likes API endpoint used by useFetch
71+
registerEndpoint(`/api/social/likes/${packageName}`, () => ({
72+
totalLikes,
73+
userHasLiked: false,
74+
}))
75+
}
76+
77+
beforeEach(() => {
78+
mockUseResolvedVersion.mockReset()
79+
mockUsePackageDownloads.mockReset()
80+
mockUsePackage.mockReset()
81+
mockUseRepoMeta.mockReset()
82+
})
83+
84+
it('renders the package name and version', async () => {
85+
setupMocks({ packageName: 'vue' })
86+
87+
const component = await mountSuspended(OgImagePackage, {
88+
props: { ...baseProps, name: 'vue' },
89+
})
90+
91+
expect(component.text()).toContain('vue')
92+
expect(component.text()).toContain('1.0.0')
93+
})
94+
95+
describe('license', () => {
96+
it('renders the license when present', async () => {
97+
setupMocks({ license: 'MIT' })
98+
99+
const component = await mountSuspended(OgImagePackage, {
100+
props: baseProps,
101+
})
102+
103+
expect(component.text()).toContain('MIT')
104+
})
105+
106+
it('hides the license section when license is missing', async () => {
107+
setupMocks({ license: null })
108+
109+
const component = await mountSuspended(OgImagePackage, {
110+
props: baseProps,
111+
})
112+
113+
expect(component.find('[data-testid="license"]').exists()).toBe(false)
114+
})
115+
})
116+
117+
describe('stars', () => {
118+
it('hides stars section when count is 0', async () => {
119+
setupMocks({ stars: 0 })
120+
121+
const component = await mountSuspended(OgImagePackage, {
122+
props: baseProps,
123+
})
124+
125+
expect(component.find('[data-testid="stars"]').exists()).toBe(false)
126+
})
127+
128+
it('shows formatted stars when count is positive', async () => {
129+
setupMocks({ stars: 45200 })
130+
131+
const component = await mountSuspended(OgImagePackage, {
132+
props: baseProps,
133+
})
134+
135+
expect(component.text()).toContain('45.2K')
136+
})
137+
})
138+
139+
describe('likes', () => {
140+
it('hides likes section when totalLikes is 0', async () => {
141+
setupMocks({ totalLikes: 0 })
142+
143+
const component = await mountSuspended(OgImagePackage, {
144+
props: baseProps,
145+
})
146+
147+
expect(component.find('[data-testid="likes"]').exists()).toBe(false)
148+
})
149+
150+
it('shows likes section when totalLikes is positive', async () => {
151+
setupMocks({ totalLikes: 42 })
152+
153+
const component = await mountSuspended(OgImagePackage, {
154+
props: baseProps,
155+
})
156+
157+
expect(component.text()).toContain('42')
158+
})
159+
})
160+
})

0 commit comments

Comments
 (0)