[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 74 lines 2.3 kB view raw
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' 2import { clearClientCaches } from '~/composables/useCharts' 3 4const mockLikesResponse = [ 5 { day: '2025-03-01', likes: 5 }, 6 { day: '2025-03-02', likes: 3 }, 7 { day: '2025-03-03', likes: 8 }, 8 { day: '2025-03-04', likes: 2 }, 9 { day: '2025-03-05', likes: 10 }, 10 { day: '2025-03-06', likes: 1 }, 11 { day: '2025-03-07', likes: 4 }, 12 { day: '2025-03-08', likes: 6 }, 13 { day: '2025-03-09', likes: 7 }, 14 { day: '2025-03-10', likes: 3 }, 15] 16 17describe('useCharts – fetchPackageLikesEvolution', () => { 18 let fetchSpy: ReturnType<typeof vi.fn> 19 20 beforeEach(() => { 21 fetchSpy = vi.fn().mockResolvedValue(mockLikesResponse) 22 vi.stubGlobal('$fetch', fetchSpy) 23 24 // Clear any client-side caches between tests 25 clearClientCaches() 26 }) 27 28 afterEach(() => { 29 vi.unstubAllGlobals() 30 }) 31 32 it('propagates API errors', async () => { 33 fetchSpy.mockRejectedValueOnce(new Error('Network error')) 34 35 const { fetchPackageLikesEvolution } = useCharts() 36 37 await expect( 38 fetchPackageLikesEvolution('fail-pkg', { 39 granularity: 'day', 40 startDate: '2025-03-01', 41 endDate: '2025-03-10', 42 }), 43 ).rejects.toThrow('Network error') 44 }) 45 46 it('returns daily points with value field mapped from likes', async () => { 47 const { fetchPackageLikesEvolution } = useCharts() 48 49 const result = await fetchPackageLikesEvolution('vue', { 50 granularity: 'day', 51 startDate: '2025-03-01', 52 endDate: '2025-03-10', 53 }) 54 55 expect(result.length).toBeGreaterThan(0) 56 expect(result[0]).toHaveProperty('value') 57 expect(result[0]).toHaveProperty('day') 58 expect(result[0]).toHaveProperty('timestamp') 59 expect(result[0]).not.toHaveProperty('downloads') 60 expect(result[0]).not.toHaveProperty('likes') 61 }) 62 63 it('uses client-side promise caching for repeated calls', async () => { 64 const { fetchPackageLikesEvolution } = useCharts() 65 66 const options = { granularity: 'day' as const, startDate: '2025-03-01', endDate: '2025-03-10' } 67 68 await fetchPackageLikesEvolution('vue', options) 69 await fetchPackageLikesEvolution('vue', options) 70 71 // Should only fetch once thanks to client-side promise caching 72 expect(fetchSpy).toHaveBeenCalledTimes(1) 73 }) 74})