-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathuse-npm-registry.spec.ts
More file actions
55 lines (42 loc) · 1.48 KB
/
use-npm-registry.spec.ts
File metadata and controls
55 lines (42 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const mockDownloadResponse = {
downloads: 1234567,
start: '2024-01-01',
end: '2024-01-07',
package: 'vue',
}
describe('usePackageDownloads', () => {
let fetchSpy: ReturnType<typeof vi.fn>
beforeEach(() => {
fetchSpy = vi.fn().mockResolvedValue(mockDownloadResponse)
vi.stubGlobal('$fetch', fetchSpy)
})
afterEach(() => {
vi.unstubAllGlobals()
})
it('should fetch download stats for a package', async () => {
const { data, status } = usePackageDownloads('vue')
await vi.waitFor(() => {
expect(status.value).toBe('success')
})
expect(fetchSpy).toHaveBeenCalledWith('https://api.npmjs.org/downloads/point/last-week/vue')
expect(data.value?.downloads).toBe(1234567)
})
it('should use custom period when provided', async () => {
const { status } = usePackageDownloads('vue', 'last-month')
await vi.waitFor(() => {
expect(status.value).toBe('success')
})
expect(fetchSpy).toHaveBeenCalledWith('https://api.npmjs.org/downloads/point/last-month/vue')
})
it('should encode scoped package names', async () => {
fetchSpy.mockResolvedValue({ ...mockDownloadResponse, package: '@vue/core' })
const { status } = usePackageDownloads('@vue/core')
await vi.waitFor(() => {
expect(status.value).toBe('success')
})
expect(fetchSpy).toHaveBeenCalledWith(
'https://api.npmjs.org/downloads/point/last-week/@vue%2Fcore',
)
})
})