[READ-ONLY] a fast, modern browser for the npm registry
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 59 lines 1.8 kB view raw
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' 2 3const mockDownloadResponse = { 4 downloads: 1234567, 5 start: '2024-01-01', 6 end: '2024-01-07', 7 package: 'vue', 8} 9describe('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 // Check that fetch was called with the correct URL (first argument) 29 expect(fetchSpy).toHaveBeenCalled() 30 expect(fetchSpy.mock.calls[0]?.[0]).toBe('/downloads/point/last-week/vue') 31 expect(data.value?.downloads).toBe(1234567) 32 }) 33 34 it('should use custom period when provided', async () => { 35 const { status } = usePackageDownloads('vue', 'last-month') 36 37 await vi.waitFor(() => { 38 expect(status.value).toBe('success') 39 }) 40 41 // Check that fetch was called with the correct URL (first argument) 42 expect(fetchSpy).toHaveBeenCalled() 43 expect(fetchSpy.mock.calls[0]?.[0]).toBe('/downloads/point/last-month/vue') 44 }) 45 46 it('should encode scoped package names', async () => { 47 fetchSpy.mockResolvedValue({ ...mockDownloadResponse, package: '@vue/core' }) 48 49 const { status } = usePackageDownloads('@vue/core') 50 51 await vi.waitFor(() => { 52 expect(status.value).toBe('success') 53 }) 54 55 // Check that fetch was called with the correct URL (first argument) 56 expect(fetchSpy).toHaveBeenCalled() 57 expect(fetchSpy.mock.calls[0]?.[0]).toBe('/downloads/point/last-week/@vue%2Fcore') 58 }) 59})