Skip to content

Commit 4a09e6d

Browse files
authored
test: add useNpmRegistry tests (#145)
1 parent c350721 commit 4a09e6d

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
})

vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default defineConfig({
2323
await defineVitestProject({
2424
test: {
2525
name: 'nuxt',
26-
include: ['test/nuxt/*.{test,spec}.ts'],
26+
include: ['test/nuxt/**/*.{test,spec}.ts'],
2727
environment: 'nuxt',
2828
environmentOptions: {
2929
nuxt: {

0 commit comments

Comments
 (0)