Skip to content

Commit c64c0b8

Browse files
committed
Add unit tests
1 parent 3c71ac4 commit c64c0b8

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { mockNuxtImport, mountSuspended } from '@nuxt/test-utils/runtime'
2+
import { defineComponent, h } from 'vue'
3+
import { describe, expect, it, vi } from 'vitest'
4+
5+
const { mockFetchPackageDownloadEvolution } = vi.hoisted(() => ({
6+
mockFetchPackageDownloadEvolution: vi.fn(),
7+
}))
8+
9+
mockNuxtImport('useCharts', () => {
10+
return () => ({
11+
fetchPackageDownloadEvolution: (...args: unknown[]) =>
12+
mockFetchPackageDownloadEvolution(...args),
13+
})
14+
})
15+
16+
vi.mock('vue-data-ui/vue-ui-sparkline', () => ({
17+
VueUiSparkline: defineComponent({
18+
name: 'VueUiSparkline',
19+
inheritAttrs: false,
20+
setup(_, { attrs, slots }) {
21+
return () => h('div', { class: attrs.class }, slots.default?.() ?? [])
22+
},
23+
}),
24+
}))
25+
26+
import PackageWeeklyDownloadStats from '~/components/Package/WeeklyDownloadStats.vue'
27+
28+
describe('PackageWeeklyDownloadStats', () => {
29+
const baseProps = {
30+
packageName: 'test-package',
31+
createdIso: '2026-02-05T00:00:00.000Z',
32+
}
33+
34+
it('hides the section when weekly downloads are empty', async () => {
35+
mockFetchPackageDownloadEvolution.mockReset()
36+
mockFetchPackageDownloadEvolution.mockResolvedValue([])
37+
38+
const component = await mountSuspended(PackageWeeklyDownloadStats, {
39+
props: baseProps,
40+
})
41+
42+
expect(component.text()).not.toContain('Weekly Downloads')
43+
})
44+
45+
it('shows the section when weekly downloads exist', async () => {
46+
mockFetchPackageDownloadEvolution.mockReset()
47+
mockFetchPackageDownloadEvolution.mockResolvedValue([
48+
{
49+
weekStart: '2026-01-01',
50+
weekEnd: '2026-01-07',
51+
timestampStart: 1767225600000,
52+
timestampEnd: 1767744000000,
53+
downloads: 42,
54+
},
55+
])
56+
57+
const component = await mountSuspended(PackageWeeklyDownloadStats, {
58+
props: baseProps,
59+
})
60+
61+
expect(component.text()).toContain('Weekly Downloads')
62+
})
63+
})

0 commit comments

Comments
 (0)