|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +const mockDownloadResponse = { |
| 4 | + downloads: 1234567, |
| 5 | + start: '2024-01-01', |
| 6 | + end: '2024-01-07', |
| 7 | + package: 'vue', |
| 8 | +} |
| 9 | +describe('usePackageDownloads', () => { |
| 10 | + let fetchSpy: ReturnType<typeof vi.fn> |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + fetchSpy = vi.fn().mockResolvedValue(mockDownloadResponse) |
| 14 | + vi.stubGlobal('$fetch', fetchSpy) |
| 15 | + }) |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + vi.unstubAllGlobals() |
| 19 | + }) |
| 20 | + |
| 21 | + it('should fetch download stats for a package', async () => { |
| 22 | + const { data, status } = usePackageDownloads('vue') |
| 23 | + |
| 24 | + await vi.waitFor(() => { |
| 25 | + expect(status.value).toBe('success') |
| 26 | + }) |
| 27 | + |
| 28 | + expect(fetchSpy).toHaveBeenCalledWith('https://api.npmjs.org/downloads/point/last-week/vue') |
| 29 | + expect(data.value?.downloads).toBe(1234567) |
| 30 | + }) |
| 31 | + |
| 32 | + it('should use custom period when provided', async () => { |
| 33 | + const { status } = usePackageDownloads('vue', 'last-month') |
| 34 | + |
| 35 | + await vi.waitFor(() => { |
| 36 | + expect(status.value).toBe('success') |
| 37 | + }) |
| 38 | + |
| 39 | + expect(fetchSpy).toHaveBeenCalledWith('https://api.npmjs.org/downloads/point/last-month/vue') |
| 40 | + }) |
| 41 | + |
| 42 | + it('should encode scoped package names', async () => { |
| 43 | + fetchSpy.mockResolvedValue({ ...mockDownloadResponse, package: '@vue/core' }) |
| 44 | + |
| 45 | + const { status } = usePackageDownloads('@vue/core') |
| 46 | + |
| 47 | + await vi.waitFor(() => { |
| 48 | + expect(status.value).toBe('success') |
| 49 | + }) |
| 50 | + |
| 51 | + expect(fetchSpy).toHaveBeenCalledWith( |
| 52 | + 'https://api.npmjs.org/downloads/point/last-week/@vue%2Fcore', |
| 53 | + ) |
| 54 | + }) |
| 55 | +}) |
0 commit comments