forked from
npmx.dev/npmx.dev
[READ-ONLY]
a fast, modern browser for the npm registry
1import { mockNuxtImport, mountSuspended } from '@nuxt/test-utils/runtime'
2import { defineComponent, h } from 'vue'
3import { describe, expect, it, vi } from 'vitest'
4
5const { mockFetchPackageDownloadEvolution } = vi.hoisted(() => ({
6 mockFetchPackageDownloadEvolution: vi.fn(),
7}))
8
9mockNuxtImport('useCharts', () => {
10 return () => ({
11 fetchPackageDownloadEvolution: (...args: unknown[]) =>
12 mockFetchPackageDownloadEvolution(...args),
13 })
14})
15
16vi.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
26import PackageWeeklyDownloadStats from '~/components/Package/WeeklyDownloadStats.vue'
27
28describe('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()).toContain('Weekly Downloads')
43 expect(component.text()).toContain('No data available')
44 })
45
46 it('shows the section when weekly downloads exist', async () => {
47 mockFetchPackageDownloadEvolution.mockReset()
48 mockFetchPackageDownloadEvolution.mockResolvedValue([
49 {
50 weekStart: '2026-01-01',
51 weekEnd: '2026-01-07',
52 timestampStart: 1767225600000,
53 timestampEnd: 1767744000000,
54 value: 42,
55 },
56 ])
57
58 const component = await mountSuspended(PackageWeeklyDownloadStats, {
59 props: baseProps,
60 })
61
62 expect(component.text()).toContain('Weekly Downloads')
63 expect(component.text()).not.toContain('No data available')
64 })
65})